Friday, March 10, 2017

KoT's Dynamic Content Blocker

My two scripts, Block GIF and AD Block, worked so well, so I combined them into one. I also added another transparent button for iFrame block. Being able to block or unlock on the fly and to save the preference to a site or sub-site, this method is perfect. Can be easily modified, improved, extended and expanded as well.

I am not against to ads, but I hate dynamic contents - too annoying and distractive, killing battery, slowing devices down and running them hot. Because all contents are blocked not removed, the original page format is retained and there is no up-down or left-right shifting.

See how well it works:



I also use The Great Suspender to unload tabs after 5 minutes. Now, there is no CPU consumption, how great is this!



The following script has been (on 1/4/2018) and will be updated. If you want to unblock an item, please let me know so I can find out the iframe for you and add it to the NOT_TO_REMOVE list.

You need to install Greasemonkey (Firefox) or Violentmonkey (Chrome, Opera and Yandex), add a new script and copy and paste the following lines.

// ==UserScript==
// @name         KoT's Content Blocker
// @namespace    http://www.kots.us
// @grant        none
// @exclude      https://xfinity.nnu.com/xfinitywifi/main
// @exclude      https://www.blogger.com/*
// @exclude      https://groups.google.com/forum/*
// @exclude      https://productforums.google.com/forum/*
// @exclude      https://postage.ebay.com/ws/eBayISAPI.dll*
// @exclude      https://ibdswebp41-ext.pb.com/images/USPS/HTMLFolders/HTML14/*
// @exclude      https://3c-lxa.mail.com/mail/client*
// ==/UserScript==

/* add your items not to be blocked */

var NOT_TO_REMOVE =
    ':not([src*=\'youtu\'])' +
    ':not([src*=\'dailymotion\'])' +
    ':not([src*=\'youku\'])' +
    ':not([src*=\'tudou\'])' +
    ':not([src*=\'powermv\'])' +
    ':not([src*=\'dramafever\'])' +
    ':not([src*=\'vimeo\'])' +
    ':not([src*=\'liveguidestationplayer\'])' +
    ':not([src*=\'RoutePositionET\'])' +
    ':not([src*=\'blogger.com\'])' +
    ':not([src*=\'chase.com\'])' +
    ':not([src*=\'/tob/live/usp-core\'])' +
    ':not([src*=\'vi.vipr.ebaydesc.com\'])' +
    ':not([src*=\'signin.ebay.com/ws/\'])' +
    ':not([iframe-src*=\'mail.com\'])' +
    ':not([src*=\'captcha\'])'
;

/* add your items to be blocked */

var TO_REMOVE = 'div[id^=\'carousel\']' +
    ',div[id^=\'Carousel\']' +
    ',iframe[src^=\'/news/index.php?act=getiframe\']' +
    ',iframe[src^=\'javascript\']' +
    ',div[class^=\'eis_slide\']' +
    ',div[class^=\'tout-sdk\']' +
    ',iframe[src*=\'tpc.googlesyndication.com\']' +
    ',iframe[src*=\'googleads\']' +
    ',iframe[src*=\'imasdk.googleapis.com\']'
;

/* add special items to be blocked */

function blockSpecialItems() {
  var m = document.getElementById('scrollWrap');
  if (m !== null) {
    m.id = 'scrollWrap_';
    m = document.getElementById('scrollMsg');
    if (m !== null) {
      m.parentNode.removeChild(m);
    }
  }
}

/* no need to change below */

function getCookie(w){
    cName = "";
    pCOOKIES = new Array();
    pCOOKIES = document.cookie.split('; ');
    for(bb = 0; bb < pCOOKIES.length; bb++){
        NmeVal  = new Array();
        NmeVal  = pCOOKIES[bb].split('=');
        if(NmeVal[0] == w){
            cName = unescape(NmeVal[1]);
        }
    }
    return cName;
}

function setCookie(name, value, expires, path, domain, secure){
    cookieStr = name + "=" + escape(value) + "; ";
  
    if(expires){
        expires = setExpiration(expires);
        cookieStr += "expires=" + expires + "; ";
    }
    if(path){
        cookieStr += "path=" + path + "; ";
    }
    if(domain){
        cookieStr += "domain=" + domain + "; ";
    }
    if(secure){
        cookieStr += "secure; ";
    }
  
    document.cookie = cookieStr;
}

function setExpiration(cookieLife){
    var today = new Date();
    var expr = new Date(today.getTime() +
                        cookieLife * 24 * 60 * 60 * 1000);
    return  expr.toGMTString();
}

function is_gif_image(i) {
  return /^(?!data:).*\.gif/i.test(i.src);
}

function freeze_gif(i) {

  hasGIF = true;
 
  var c = document.createElement('canvas');
  var w = c.width = i.width;
  var h = c.height = i.height;
  c.getContext('2d').drawImage(i, 0, 0, w, h);
  try {
    // if possible, retain all css aspects
    i.src = c.toDataURL("image/gif");
  } catch(e) {
    // cross-domain -- mimic original with all its tag attribs
    for (var j = 0, a; a = i.attributes[j]; j++)
      c.setAttribute(a.name, a.value);
    i.parentNode.replaceChild(c, i);
  }
}

function blockMarquee() {
  var m = document.getElementsByTagName('marquee');
  while( m.length > 0) {
    var p = document.createElement('div');
    p.innerHTML = m[0].innerHTML;
    var wth = (m[0].style.width == "") ? "350" : m[0].style.width;
    var stl = "clear: both; white-space: nowrap; overflow: hidden;" +
        "text-overflow: ellipsis; width:" + wth + ";";
    p.setAttribute('style', stl);
    m[0].parentNode.insertBefore(p, m[0]);
    m[0].parentNode.removeChild(m[0]);
  }
}

function blockContents(elements) {
  for (var i = 0; i < elements.length; i++) {
    hasIframe = true;
    var frm = document.createElement("iframe");
    frm.width = elements[i].width;
    frm.height = elements[i].height;
    frm.style.border = "none";
    frm.style.float = elements[i].style.float;
    frm.style.position = elements[i].style.position;
    frm.style.display = elements[i].style.display;
    frm.style.margin = elements[i].style.margin;
    elements[i].parentNode.replaceChild(frm, elements[i]);
  }
}

function createButton(label) {
  if(label == 'GiF') {
    var rt = '8%';
    var ck = 'blockGIF';
    var hs = hasGIF;
  }
  else {
    rt = '4%';
    ck = 'blockIframe';
    hs = hasIframe;
  }
  var btn = document.createElement( 'input' );
  with( btn ) {
    onclick = function() {
      if (getCookie(ck) !== 'No') {
        setCookie(ck, 'No', 3650);
      }
      else {
        setCookie(ck, '', -1);
        setCookie(ck, '', -1, '/');
      }
      location.reload();
    };
    setAttribute( 'value', label );
    setAttribute( 'type', 'button' );
    var stl = "font-size: 18px; position: fixed;bottom: 1%;" +
        "background-color: Transparent; background-repeat:" +
        "no-repeat; cursor: pointer; overflow: hidden;" +
        "right:" + rt + ";" + "bottom:10%;" + "color:";
    if (getCookie(ck) !== "No")
      if (hs)
        stl += "red;";
      else
        stl += "gray;";
    else
      stl += "green;";
   setAttribute( 'style', stl );
  }
  document.body.appendChild(btn);
}

function doBlock() {

  if (getCookie('blockIframe') !== 'No') {
    blockContents(document.querySelectorAll(NOT_TO_REMOVE));
    blockContents(document.querySelectorAll(TO_REMOVE));
  }
 
  //Firefox has a good addon: Toggle aninated GIFs
  //if (typeof InstallTrigger == 'undefined') {
    if (getCookie('blockGIF') !== 'No')
      [].slice.apply(document.images).filter(is_gif_image)
        .map(freeze_gif);
  //}
 
  if (window.top !== window.self)
    return;
 
  //if (typeof InstallTrigger == 'undefined')
    createButton ('GiF');
  createButton ('FrM');
 
}

NOT_TO_REMOVE = 'iframe:not([src])' + NOT_TO_REMOVE +
  ',iframe[src^=\'http\']' + NOT_TO_REMOVE;

doBlock();

if (window.top !== window.self)
  return;

var hasGIF = false;
var hasIframe = false;

blockSpecialItems();
blockMarquee();
 
//some items are delayed to show up
window.addEventListener ('loadend', function(){
  doBlock();
  window.setTimeout(doBlock, 4000);
}, true);

1 comment:

  1. เว็บสล็อตที่เป็นที่นิยมมากที่สุด การันตีด้วยจำนวนผู้เล่นที่เข้ามาใช้บริการมี เกมสล็อตฟรี มากมายให้เล่นไม่อั้น ด้วยมาตรฐานการให้บริการที่โดดเด่น อัปเดตระบบให้ทันสมัยมากขึ้นอยู่เสมอ
    รวมโปรสล็อตถอนไม่อั้น สุดยอดเว็บเกมสล็อตยอดนิยม ที่รวมเกมสล็อตจากค่ายดังไว้เยอะที่สุด ให้คุณได้ประทับใจไปกับรางวัลภายในเกมที่มีแจกแบบไม่อั้น มาพร้อมกับ โปรสล็อตสมาชิกใหม่100 ถอนไม่อั้น
    สล็อตแตกง่าย เกมสล็อตออนไลน์ เกมเดิมพันออนไลน์ยอดฮิตที่ได้รับความนิยม มีให้คุณได้เลือกเล่นมากมายมากกว่า 200 เกม

    ReplyDelete