広告削除用の関数を改造してみた

id:Cherenkov:20080905:広告削除方法模索(CSSで攻める) でつくった関数を改造してみた。

function addGlobalStyle(list){

    for(var i=0;i<list.id.length;i++){ list.id[i] = '#'+list.id[i];}
    for(var i=0;i<list.class.length;i++){ list.class[i] = '.'+list.class[i];}
    for(var i=0;i<list.name.length;i++){ list.name[i] = '[name="'+list.name[i]+'"]';}
    var removeList = list.id.concat(list.class,list.name);

    var head = document.getElementsByTagName('head')[0];
    if(!head){ return; }
    var style = document.createElement('style');
    style.type = 'text/css';
    style.innerHTML = removeList+'{display:none!important;}';
    head.appendChild(style);
}


//使う側
    var list = {};
    list.id = ['削除対象のid'];         <- ユーザが入力すべきところは
    list.class = ['削除対象のclass'];   <- この3箇所
    list.name = ['削除対象のname'];     <-

    addGlobalStyle(list);

前回よりも工夫したとこ

  • ふたつだった関数をひとつにまとめた。
  • addGlobalStyle関数に渡す引数をひとつにした。
  • 関数内部で引数の中の配列を展開するようにした。
  • 削除対象をカンマで繋げまとめてからdisplay:noneするようにした。
  • 使う側をオブジェクトにまとめた。

やっぱりデモ

engadget 用のブックマークレット。広告を消します。トップページ、個別のエントリーでも動作します。

javascript:function addGlobalStyle(list){for(var i=0;i<list.id.length;i++){list.id[i]='#'+list.id[i];}for(var i=0;i<list.class.length;i++){list.class[i]='.'+list.class[i];}for(var i=0;i<list.name.length;i++){list.name[i]='[name="'+list.name[i]+'"]';}var removeList=list.id.concat(list.class,list.name);var head=document.getElementsByTagName('head')[0];if(!head){return;}var style=document.createElement('style');style.type='text/css';style.innerHTML=removeList+'{display:none!important;}';head.appendChild(style);}(function(){var list={};list.id=['resources','bp4','bp3','bp5','bp9','grid','aus'];list.class=['topleader','bottomleader','medrect'];list.name=[];addGlobalStyle(list);var adtable=document.evaluate('//table[@border="0"][parent::div[@class="postbody"]]',document,null,7,null);if(adtable.snapshotItem(0)){adtable.snapshotItem(0).parentNode.removeChild(adtable.snapshotItem(0));}})();

デモにつかったソースコード

function addGlobalStyle(list){

    for(var i=0;i<list.id.length;i++){ list.id[i] = '#'+list.id[i];}
    for(var i=0;i<list.class.length;i++){ list.class[i] = '.'+list.class[i];}
    for(var i=0;i<list.name.length;i++){ list.name[i] = '[name="'+list.name[i]+'"]';}
    var removeList = list.id.concat(list.class,list.name);

    var head = document.getElementsByTagName('head')[0];
    if(!head){ return; }
    var style = document.createElement('style');
    style.type = 'text/css';
    style.innerHTML = removeList+'{display:none!important;}';
    head.appendChild(style);
}


(function(){

    var list = {};
    list.id = ['resources','bp4','bp3','bp5','bp9','grid','aus'];
    list.class = ['topleader','bottomleader','medrect'];
    list.name = [];  // <- 空の場合は[]。['']はダメ。

    addGlobalStyle(list);


//おまけ:addGlobalStyle関数で消せない広告用
    var adtable = document.evaluate('//table[@border="0"][parent::div[@class="postbody"]]', document, null, 7, null);
    if(adtable.snapshotItem(0)){
        adtable.snapshotItem(0).parentNode.removeChild(adtable.snapshotItem(0));
    }

})();

感想

あんまり進歩がなくて、さーせん。