Advertisement
Guest User

Untitled

a guest
Aug 14th, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function() {
  2.     "use strict";
  3.  
  4.     var imageUrl = 'http://upload.wikimedia.org/wikipedia/commons/thumb/a/a4/ICA_flag.svg/220px-ICA_flag.svg.png',
  5.         jsonpUrl = '//freegeoip.net/json/?callback=window.____PeskyRuskies____.jsonpCallback',
  6.         targetCountryCode = 'GB', // 'RU',
  7.         initialized = false,
  8.         scriptEl = null;
  9.  
  10.     function prependStyles(el, styles)
  11.     {
  12.         el.setAttribute('style', styles + el.getAttribute('style'));
  13.     }
  14.  
  15.     window.____PeskyRuskies____ = {
  16.         init: function() {
  17.             // shameless theft from jQuery
  18.             // https://github.com/jquery/jquery/blob/1.9-stable/speed/jquery-basis.js#L406
  19.             var self = this,
  20.                 cb = function() {
  21.                     self.initJsonpRequest();
  22.                 },
  23.                 scrollCheck = function() {
  24.                     if (!initialized) {
  25.                         try {
  26.                             document.documentElement.doScroll('left');
  27.                         } catch(e) {
  28.                             setTimeout(scrollCheck, 1);
  29.                             return;
  30.                         }
  31.  
  32.                         self.initJsonpRequest();
  33.                     }
  34.                 };
  35.  
  36.             if (document.readyState === "complete") {
  37.                 this.initJsonpRequest();
  38.             } else if (document.addEventListener) {
  39.                 document.addEventListener("DOMContentLoaded", cb, false);
  40.                 document.addEventListener("load", cb, false);
  41.             } else if (document.attachEvent) {
  42.                 document.attachEvent("onreadystatechange", cb);
  43.                 document.attachEvent("onload", cb);
  44.  
  45.                 try {
  46.                     if (document.documentElement.doScroll && window.frameElement === null) {
  47.                         scrollCheck();
  48.                     }
  49.                 } catch(e) {}
  50.             }
  51.         },
  52.         initJsonpRequest: function() {
  53.             if (!initialized) {
  54.                 scriptEl = document.createElement('script');
  55.                 scriptEl.type = 'text/javascript';
  56.                 scriptEl.src = jsonpUrl;
  57.                 document.body.appendChild(scriptEl);
  58.  
  59.                 initialized = true;
  60.             }
  61.         },
  62.         jsonpCallback: function(obj) {
  63.             if (scriptEl) {
  64.                 if (obj.country_code !== undefined) { // && obj.country_code.toUpperCase() === targetCountryCode) {
  65.                     prependStyles(
  66.                         document.body,
  67.                         'background-color:transparent;background-image:url(' + imageUrl + ');'
  68.                     );
  69.  
  70.                     if (document.querySelectorAll && Array.prototype.forEach) {
  71.                         Array.prototype.forEach.call(document.querySelectorAll('p, div, table, td, th, span, img'), function(el) {
  72.                             prependStyles(el, 'opacity:0.95;');
  73.                         });
  74.                     }
  75.                 }
  76.  
  77.                 // clean up after ourselves, it's not polite to leave a mess
  78.                 document.body.removeChild(scriptEl);
  79.                 scriptEl = null;
  80.                 delete window.____PeskyRuskies____;
  81.             }
  82.         }
  83.     };
  84.  
  85.     window.____PeskyRuskies____.init();
  86. }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement