tolikpunkoff

ip_popup.js

Sep 27th, 2019
199
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var defAddr='https://api.myip.com/';
  2. var curAddr='';
  3.  
  4. function isEmptyObject(obj)
  5. {
  6.     for (var i in obj)
  7.     {
  8.         if (obj.hasOwnProperty(i))
  9.         {
  10.             return false;
  11.         }
  12.     }
  13.     return true;
  14. }
  15.  
  16. function saveSettings()
  17. {
  18.     var scriptAddr = document.getElementById("script").value;
  19.     scriptAddr = scriptAddr.trim();
  20.    
  21.     if (scriptAddr != "")
  22.     {
  23.         if (window.confirm('Save script address ' + scriptAddr + '? Are you sure?'))
  24.         {
  25.             browser.storage.local.set({
  26.                 server_settings: {script_addr: scriptAddr}
  27.             });
  28.             window.alert('Settings saved!');
  29.         }
  30.         else
  31.         {
  32.             document.getElementById("script").value = curAddr;
  33.         }
  34.     }
  35. }
  36.  
  37. function onGot(item)
  38. {
  39.     if (isEmptyObject(item))
  40.     {
  41.         curAddr = defAddr;
  42.     }
  43.     else
  44.     {
  45.         if (item.server_settings.script_addr == '')
  46.         {
  47.             curAddr = defAddr;
  48.         }
  49.         else
  50.         {
  51.             curAddr = item.server_settings.script_addr;
  52.         }
  53.     }
  54.     document.getElementById("script").value = curAddr;
  55. }
  56.  
  57. function onGotError(error)
  58. {
  59.     window.alert(`Error: ${error}`);
  60. }
  61.  
  62. function loadSettings()
  63. {
  64.     var gettingItem = browser.storage.local.get('server_settings');
  65.     return gettingItem.then(onGot, onGotError);
  66. }
  67.  
  68. function doRequest() //main request function
  69. {
  70.     document.getElementById("flag").src = 'flags/1working.png';
  71.     document.getElementById("flag").alt = 'Working';
  72.     document.getElementById("flag").title = 'working...';
  73.    
  74.     $.get(curAddr)
  75.         .done (function (data) {
  76.             //data processing code here
  77.             var json = $.parseJSON(data);          
  78.             document.getElementById("ip").value = json.ip;
  79.             document.getElementById("country").value = json.country;
  80.            
  81.             if (json.cc.trim() == '') //no country code
  82.             {
  83.                 document.getElementById("flag").src = 'flags/3unknow.png';
  84.                 document.getElementById("flag").alt = 'Unknow';
  85.                 document.getElementById("flag").title = 'Unknow country code';
  86.             }
  87.             else //country code exist
  88.             {
  89.                 document.getElementById("flag").src = 'flags/'+json.cc.trim()+'.png';
  90.                 document.getElementById("flag").alt = json.cc;
  91.                 document.getElementById("flag").title = json.cc;
  92.             }
  93.         })
  94.         .fail(function () {
  95.             //error processing code here
  96.             document.getElementById("flag").src = 'flags/2error.png';
  97.             document.getElementById("flag").alt = 'Error';
  98.             document.getElementById("flag").title = 'Error';
  99.         });
  100. }
  101.  
  102. function bodyLoad()
  103. {
  104.     loadSettings()
  105.     .then(doRequest);  
  106. }
  107.  
  108. function restoreDefaults()
  109. {
  110.     if (window.confirm('Restore defaults?'))
  111.     {
  112.         curAddr = defAddr;
  113.         document.getElementById("script").value = defAddr;
  114.         browser.storage.local.set({
  115.                 server_settings: {script_addr: defAddr}
  116.             });
  117.     }
  118. }
  119.  
  120. document.getElementById("save").addEventListener("click", saveSettings); //скобки не забудь про сраные скобки
  121. document.getElementById("restore").addEventListener("click", restoreDefaults);
  122. document.addEventListener('DOMContentLoaded', bodyLoad);
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×