Advertisement
Guest User

Untitled

a guest
Dec 31st, 2015
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function inject() {
  2.    
  3.     var originalOpenWndFnKey = "originalOpenFunction";
  4.  
  5.             var originalWindowOpenFn    = window.open,
  6.                 originalCreateElementFn = document.createElement,
  7.                 originalCreateEventFn   = document.createEvent,
  8.                 windowsWithNames = {};
  9.             var timeSinceCreateAElement = 0;
  10.             var lastCreatedAElement = null;
  11.             var fullScreenOpenTime;
  12.             var parentOrigin = (window.location != window.parent.location) ? document.referrer: document.location;
  13.  
  14.             window[originalOpenWndFnKey] = window.open; // save the original open window as global param
  15.            
  16.             function newWindowOpenFn() {
  17.  
  18.                 var openWndArguments = arguments,
  19.                     useOriginalOpenWnd = true,
  20.                     generatedWindow = null;
  21.  
  22.                 function blockedWndNotification(openWndArguments) {
  23.                     parent.postMessage({ type: "blockedWindow", args: JSON.stringify(openWndArguments) }, parentOrigin);
  24.                 }
  25.  
  26.                 function getWindowName(openWndArguments) {
  27.                     var windowName = openWndArguments[1];
  28.                     if ((windowName != null) && (["_blank", "_parent", "_self", "_top"].indexOf(windowName) < 0)) {
  29.                         return windowName;
  30.                     }
  31.  
  32.                     return null;
  33.                 }
  34.  
  35.                 function copyMissingProperties(src, dest) {
  36.                     var prop;
  37.                     for(prop in src) {
  38.                         try {
  39.                             if (dest[prop] === undefined) {
  40.                                 dest[prop] = src[prop];
  41.                         }
  42.                         } catch (e) {}
  43.                     }
  44.                     return dest;
  45.                 }
  46.  
  47.                     // the element who registered to the event
  48.                     var capturingElement = null;
  49.                     if (window.event != null) {
  50.                         capturingElement = window.event.currentTarget;
  51.                     }
  52.  
  53.                     if (capturingElement == null) {
  54.                         var caller = openWndArguments.callee;
  55.                         while ((caller.arguments != null) && (caller.arguments.callee.caller != null)) {
  56.                             caller = caller.arguments.callee.caller;
  57.                         }
  58.                         if ((caller.arguments != null) && (caller.arguments.length > 0) && (caller.arguments[0].currentTarget != null)) {
  59.                             capturingElement = caller.arguments[0].currentTarget;
  60.                         }
  61.                     }
  62.  
  63.                 /////////////////////////////////////////////////////////////////////////////////
  64.                 // Blocked if a click on background element occurred (<body> or document)
  65.                 /////////////////////////////////////////////////////////////////////////////////
  66.  
  67.                     if ((capturingElement != null) && (
  68.                             (capturingElement instanceof Window) ||
  69.                             (capturingElement === document) ||
  70.                             (
  71.                                 (capturingElement.URL != null) && (capturingElement.body != null)
  72.                             ) ||
  73.                             (
  74.                                 (capturingElement.nodeName != null) && (
  75.                                     (capturingElement.nodeName.toLowerCase() == "body") ||
  76.                                     (capturingElement.nodeName.toLowerCase() == "#document")
  77.                                 )
  78.                             )
  79.                         )) {
  80.                             window.pbreason = "Blocked a new window opened with URL: " + openWndArguments[0] + " because it was triggered by the " + capturingElement.nodeName + " element";
  81.                             // console.info(window.pbreason);
  82.                             useOriginalOpenWnd = false;
  83.                     } else {
  84.                         useOriginalOpenWnd = true;
  85.                     }
  86.                 /////////////////////////////////////////////////////////////////////////////////
  87.  
  88.  
  89.  
  90.                 /////////////////////////////////////////////////////////////////////////////////
  91.                 // Block if a full screen was just initiated while opening this url.
  92.                 /////////////////////////////////////////////////////////////////////////////////
  93.  
  94.                     // console.info("fullscreen: " + ((new Date()).getTime() - fullScreenOpenTime));
  95.                     // console.info("webkitFullscreenElement: " + document.webkitFullscreenElement);
  96.                     var fullScreenElement = document.webkitFullscreenElement || document.mozFullscreenElement || document.fullscreenElement
  97.                     if ((((new Date()).getTime() - fullScreenOpenTime) < 1000) || ((isNaN(fullScreenOpenTime) && (isDocumentInFullScreenMode())))) {
  98.  
  99.                         window.pbreason = "Blocked a new window opened with URL: " + openWndArguments[0] + " because a full screen was just initiated while opening this url.";
  100.                         // console.info(window.pbreason);
  101.  
  102.                         /* JRA REMOVED
  103.                         if (window[script_params.fullScreenFnKey]) {
  104.                             window.clearTimeout(window[script_params.fullScreenFnKey]);
  105.                         }
  106.                         */
  107.  
  108.                         if (document.exitFullscreen) {
  109.                             document.exitFullscreen();
  110.                         }
  111.                         else if (document.mozCancelFullScreen) {
  112.                             document.mozCancelFullScreen();
  113.                         }
  114.                         else if (document.webkitCancelFullScreen) {
  115.                             document.webkitCancelFullScreen();
  116.                         }
  117.  
  118.                         useOriginalOpenWnd = false;
  119.                     }
  120.                 /////////////////////////////////////////////////////////////////////////////////
  121.  
  122.  
  123.                 if (useOriginalOpenWnd == true) {
  124.  
  125.                     // console.info("allowing new window to be opened with URL: " + openWndArguments[0]);
  126.  
  127.                     generatedWindow = originalWindowOpenFn.apply(this, openWndArguments);
  128.  
  129.                     // save the window by name, for latter use.
  130.                     var windowName = getWindowName(openWndArguments);
  131.                     if (windowName != null) {
  132.                         windowsWithNames[windowName] = generatedWindow;
  133.                     }
  134.  
  135.                     // 2nd line of defence: allow window to open but monitor carefully...
  136.  
  137.                     /////////////////////////////////////////////////////////////////////////////////
  138.                     // Kill window if a blur (remove focus) is called to that window
  139.                     /////////////////////////////////////////////////////////////////////////////////
  140.                     if (generatedWindow !== window) {
  141.                         var openTime = (new Date()).getTime();
  142.                         var originalWndBlurFn = generatedWindow.blur;
  143.                         generatedWindow.blur = function() {
  144.                             if (((new Date()).getTime() - openTime) < 1000 /* one second */) {
  145.                                 window.pbreason = "Blocked a new window opened with URL: " + openWndArguments[0] + " because a it was blured";
  146.                                 // console.info(window.pbreason);
  147.                                 generatedWindow.close();
  148.                                 blockedWndNotification(openWndArguments);
  149.                             } else {
  150.                                 // console.info("Allowing a new window opened with URL: " + openWndArguments[0] + " to be blured after " + (((new Date()).getTime() - openTime)) + " seconds");
  151.                                 originalWndBlurFn();
  152.                             }
  153.                         };
  154.                     }
  155.                     /////////////////////////////////////////////////////////////////////////////////
  156.  
  157.                 } else { // (useOriginalOpenWnd == false)
  158.  
  159.                         var location = {
  160.                             href: openWndArguments[0]
  161.                         };
  162.                         location.replace = function(url) {
  163.                             location.href = url;
  164.                         };
  165.  
  166.                         generatedWindow = {
  167.                             close:                      function() {return true;},
  168.                             test:                       function() {return true;},
  169.                             blur:                       function() {return true;},
  170.                             focus:                      function() {return true;},
  171.                             showModelessDialog:         function() {return true;},
  172.                             showModalDialog:            function() {return true;},
  173.                             prompt:                     function() {return true;},
  174.                             confirm:                    function() {return true;},
  175.                             alert:                      function() {return true;},
  176.                             moveTo:                     function() {return true;},
  177.                             moveBy:                     function() {return true;},
  178.                             resizeTo:                   function() {return true;},
  179.                             resizeBy:                   function() {return true;},
  180.                             scrollBy:                   function() {return true;},
  181.                             scrollTo:                   function() {return true;},
  182.                             getSelection:               function() {return true;},
  183.                             onunload:                   function() {return true;},
  184.                             print:                      function() {return true;},
  185.                             open:                       function() {return this;},
  186.                             opener:                     window,
  187.                             closed:                     false,
  188.                             innerHeight:                480,
  189.                             innerWidth:                 640,
  190.                             name:                       openWndArguments[1],
  191.                             location:                   location,
  192.                             document:                   {location: location}
  193.                         };
  194.  
  195.                     copyMissingProperties(window, generatedWindow);
  196.  
  197.                     generatedWindow.window = generatedWindow;
  198.  
  199.                     var windowName = getWindowName(openWndArguments);
  200.                     if (windowName != null) {
  201.                         try {
  202.                             // originalWindowOpenFn("", windowName).close();
  203.                             windowsWithNames[windowName].close();
  204.                             // console.info("Closed window with the following name: " + windowName);
  205.                         } catch (err) {
  206.                             // console.info("Couldn't close window with the following name: " + windowName);
  207.                         }
  208.                     }
  209.  
  210.                     setTimeout(function() {
  211.                         var url;
  212.                         if (!(generatedWindow.location instanceof Object)) {
  213.                             url = generatedWindow.location;
  214.                         } else if (!(generatedWindow.document.location instanceof Object)) {
  215.                             url = generatedWindow.document.location;
  216.                         } else if (location.href != null) {
  217.                             url = location.href;
  218.                         } else {
  219.                             url = openWndArguments[0];
  220.                         }
  221.                         openWndArguments[0] = url;
  222.                         blockedWndNotification(openWndArguments);
  223.                     }, 100);
  224.                 }
  225.  
  226.                 return generatedWindow;
  227.             }
  228.  
  229.  
  230.             /////////////////////////////////////////////////////////////////////////////////
  231.             // Replace the window open method with Poper Blocker's
  232.             /////////////////////////////////////////////////////////////////////////////////
  233.             window.open = function() {
  234.                 try {
  235.                     return newWindowOpenFn.apply(this, arguments);
  236.                 } catch(err) {
  237.                     return null;
  238.                 }
  239.             };
  240.             /////////////////////////////////////////////////////////////////////////////////
  241.  
  242.  
  243.  
  244.             //////////////////////////////////////////////////////////////////////////////////////////////////////////
  245.             // Monitor dynamic html element creation to prevent generating <a> elements with click dispatching event
  246.             //////////////////////////////////////////////////////////////////////////////////////////////////////////
  247.             document.createElement = function() {
  248.  
  249.                     var newElement = originalCreateElementFn.apply(document, arguments);
  250.  
  251.                     if (arguments[0] == "a" || arguments[0] == "A") {
  252.                        
  253.                         timeSinceCreateAElement = (new Date).getTime();
  254.  
  255.                         var originalDispatchEventFn = newElement.dispatchEvent;
  256.  
  257.                         newElement.dispatchEvent = function(event) {
  258.                             if (event.type != null && (("" + event.type).toLocaleLowerCase() == "click")) {
  259.                                 window.pbreason = "blocked due to an explicit dispatchEvent event with type 'click' on an 'a' tag";
  260.                                 // console.info(window.pbreason);
  261.                                 parent.postMessage({type:"blockedWindow", args: JSON.stringify({"0": newElement.href}) }, parentOrigin);
  262.                                 return true;
  263.                             }
  264.  
  265.                             return originalDispatchEventFn(event);
  266.                         };
  267.  
  268.                         lastCreatedAElement = newElement;
  269.  
  270.                     }
  271.  
  272.                     return newElement;
  273.             };
  274.             /////////////////////////////////////////////////////////////////////////////////
  275.  
  276.  
  277.  
  278.  
  279.             /////////////////////////////////////////////////////////////////////////////////
  280.             // Block artificial mouse click on frashly created <a> elements
  281.             /////////////////////////////////////////////////////////////////////////////////
  282.             document.createEvent = function() {
  283.                 try {
  284.                     if ((arguments[0].toLowerCase().indexOf("mouse") >= 0) && ((new Date).getTime() - timeSinceCreateAElement) <= 50) {
  285.                         window.pbreason = "Blocked because 'a' element was recently created and " + arguments[0] + " event was created shortly after";
  286.                         // console.info(window.pbreason);
  287.                         arguments[0] = lastCreatedAElement.href;
  288.                         parent.postMessage({ type: "blockedWindow", args: JSON.stringify({"0": lastCreatedAElement.href}) }, parentOrigin);
  289.                         return null;
  290.                     }
  291.                     return originalCreateEventFn.apply(document, arguments);
  292.                 } catch(err) {}
  293.             };
  294.             /////////////////////////////////////////////////////////////////////////////////
  295.  
  296.  
  297.  
  298.  
  299.  
  300.             /////////////////////////////////////////////////////////////////////////////////
  301.             // Monitor full screen requests
  302.             /////////////////////////////////////////////////////////////////////////////////
  303.             function onFullScreen(isInFullScreenMode) {
  304.                     if (isInFullScreenMode) {
  305.                         fullScreenOpenTime = (new Date()).getTime();
  306.                         // console.info("fullScreenOpenTime = " + fullScreenOpenTime);
  307.                     } else {
  308.                         fullScreenOpenTime = NaN;
  309.                     }
  310.             };
  311.             /////////////////////////////////////////////////////////////////////////////////
  312.  
  313.             function isDocumentInFullScreenMode() {
  314.                 // Note that the browser fullscreen (triggered by short keys) might
  315.                 // be considered different from content fullscreen when expecting a boolean
  316.                 return ((document.fullScreenElement && document.fullScreenElement !== null) ||    // alternative standard methods
  317.                     ((document.mozFullscreenElement != null) || (document.webkitFullscreenElement != null)));                   // current working methods
  318.             }
  319.  
  320.             document.addEventListener("fullscreenchange", function () {
  321.                 onFullScreen(document.fullscreen);
  322.             }, false);
  323.  
  324.             document.addEventListener("mozfullscreenchange", function () {
  325.                 onFullScreen(document.mozFullScreen);
  326.             }, false);
  327.  
  328.             document.addEventListener("webkitfullscreenchange", function () {
  329.                 onFullScreen(document.webkitIsFullScreen);
  330.             }, false);
  331.  
  332.         } inject()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement