Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // If url is relative, convert to absolute.
  2. function relativeToAbsoluteUrl(url) {
  3.     // Author: Tom Joseph of AdThwart
  4.    
  5.     if(!url)
  6.         return url;
  7.     // If URL is already absolute, don't mess with it
  8.     if(/^http/.test(url))
  9.         return url;
  10.     // Leading / means absolute path
  11.     if(url[0] == '/')
  12.         return document.location.protocol + "//" + document.location.host + url;
  13.  
  14.     // Remove filename and add relative URL to it
  15.     var base = document.baseURI.match(/.+\//);
  16.     if(!base) return document.baseURI + "/" + url;
  17.     return base[0] + url;
  18. }
  19.  
  20. // Return the ElementType element type of the given element.
  21. function typeForElement(el) {
  22.   // TODO: handle background images that aren't just the BODY.
  23.   switch (el.nodeName) {
  24.     case 'IMG': return ElementTypes.image;
  25.     case 'SCRIPT': return ElementTypes.script;
  26.     case 'OBJECT':
  27.     case 'EMBED': return ElementTypes.object;
  28.     case 'IFRAME': return ElementTypes.subdocument;
  29.     case 'LINK': return ElementTypes.stylesheet;
  30.     case 'BODY': return ElementTypes.background;
  31.     default: return ElementTypes.NONE;
  32.   }
  33. }
  34.  
  35. // Browser-agnostic canLoad function.
  36. // Returns false if data.url, data.elType, and data.pageDomain together
  37. // should not be blocked.
  38. function browser_canLoad(event, data) {
  39.   if (SAFARI) {
  40.     return safari.self.tab.canLoad(event, data);
  41.   } else {
  42.     // If we haven't yet asynchronously loaded our filters, store for later.
  43.     if (typeof _limited_to_domain == "undefined") {
  44.       event.mustBePurged = true;
  45.       LOADED_TOO_FAST.push({data:event});
  46.       return true;
  47.       // We don't need these locally, so delete them to save memory.
  48.       delete _limited_to_domain._selectorFilters;
  49.     }
  50.  
  51.     // every time browser_canLoad is called on this page, the pageDomain will
  52.     // be the same -- so we can just check _limited_to_domain which we
  53.     // calculated once.  This takes less memory than storing local_filterset
  54.     // on the page.
  55.     var isMatched = data.url && _limited_to_domain.matches(data.url, data.elType);
  56.     if (isMatched && event.mustBePurged)
  57.       log("Purging if possible " + data.url);
  58.     else if (isMatched)
  59.       log("CHROME TRUE BLOCK " + data.url);
  60.     return !isMatched;
  61.   }
  62. }
  63.  
  64. beforeLoadHandler = function(event) {
  65.   var el = event.target;
  66.   // Cancel the load if canLoad is false.
  67.   var elType = typeForElement(el);
  68.   var data = {
  69.     url: relativeToAbsoluteUrl(event.url),
  70.     elType: elType,
  71.     pageDomain: document.domain,
  72.     isTopFrame: (window == window.top)
  73.   };
  74.   if (false == browser_canLoad(event, data)) {
  75.     event.preventDefault();
  76.     if (elType & ElementTypes.background)
  77.       $(el).css("background-image", "none");
  78.     else if (!(elType & (ElementTypes.script | ElementTypes.stylesheet)))
  79.       $(el).remove();
  80.   }
  81. }
  82.  
  83. // Add style rules hiding the given list of selectors.
  84. function block_list_via_css(selectors) {
  85.   var d = document.documentElement;
  86.   // Setting this small chokes Chrome -- don't do it!  I set it back to
  87.   // 10000 from 100 on 1/10/2010 -- at some point you should just get rid
  88.   // of the while loop if you never use chunking again.
  89.   var chunksize = 10000;
  90.   while (selectors.length > 0) {
  91.     var css_chunk = document.createElement("style");
  92.     css_chunk.type = "text/css";
  93.     css_chunk.innerText += selectors.splice(0, chunksize).join(',') +
  94.                                " { visibility:hidden !important; " +
  95.                                "   display:none !important; }";
  96.     d.insertBefore(css_chunk, null);
  97.   }
  98. }
  99.  
  100. var BEFORELOAD_MALFUNCTION_DOMAINS = {"t.sina.com.cn": true, "prazsketramvaje.cz": true, "xnachat.com":true};
  101. var workaroundBeforeloadMalfunction = document.domain in BEFORELOAD_MALFUNCTION_DOMAINS;
  102.  
  103. function adblock_begin() {
  104.   if (!SAFARI)
  105.     LOADED_TOO_FAST = [];
  106.  
  107.   if(!workaroundBeforeloadMalfunction) {  
  108.     document.addEventListener("beforeload", beforeLoadHandler, true);
  109.   }
  110.  
  111.   var opts = { domain: document.domain, include_filters: true };
  112.   // The top frame should tell the background what domain it's on.  The
  113.   // subframes will be told what domain the top is on.
  114.   if (window == window.top)
  115.     opts.is_top_frame = true;
  116.      
  117.   extension_call('get_content_script_data', opts, function(data) {
  118.     var start = new Date();
  119.  
  120.     if (data.features.debug_logging.is_enabled) {
  121.       DEBUG = true;
  122.       log = function(text) { console.log(text); };
  123.     }
  124.     if (data.features.debug_time_logging.is_enabled)
  125.       time_log = function(text) { console.log(text); };
  126.  
  127.     if (data.page_is_whitelisted || data.adblock_is_paused) {
  128.       document.removeEventListener("beforeload", beforeLoadHandler, true);
  129.       delete LOADED_TOO_FAST;
  130.       return;
  131.     }
  132.  
  133.     //Chrome can't block resources immediately. Therefore all resources
  134.     //are cached first. Once the filters are loaded, simply remove them
  135.     if (!SAFARI) {
  136.       var local_filterset = FilterSet.fromText(data.filtertext);
  137.       _limited_to_domain = local_filterset.limitedToDomain(document.domain);
  138.  
  139.       for (var i=0; i < LOADED_TOO_FAST.length; i++)
  140.         beforeLoadHandler(LOADED_TOO_FAST[i].data);
  141.       delete LOADED_TOO_FAST;
  142.     }
  143.  
  144.     block_list_via_css(data.selectors);
  145.  
  146.     var end = new Date();
  147.     time_log("adblock_start run time: " + (end - start) + " ms || " +
  148.              document.location.href);
  149.   });
  150.  
  151. }
  152.  
  153. // Safari loads adblock on about:blank pages, which is a waste of RAM and cycles.
  154. if (document.location != 'about:blank')
  155.   adblock_begin();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement