Skip to content

Instantly share code, notes, and snippets.

@Griever
Created February 22, 2012 09:12
Show Gist options
  • Save Griever/1883519 to your computer and use it in GitHub Desktop.
Save Griever/1883519 to your computer and use it in GitHub Desktop.
Adblock のフィルタを urlfilter.ini に変換する
javascript:(function(){
/*
Adblock のフィルタを urlfilter.ini に変換する
ついてに広告カット CSS も作成
https://adblock-plus-japanese-filter.googlecode.com/hg/abp_jp.txt で実行
|| で始まる行は http, https の2行に、^ は "/*" に、$のある行はスルー。
CSS は -moz-document を使っている部分があるので Opera では前半のみ利用可能
*/
var text = "\n" + document.body.textContent + "\n";
var textcss = [];
var filtertext = text
.replace(/\n[\![].*|.*(?:\$|@@).*/g, "")
.replace(/.*##.*/g, function(str){ textcss.push(str); return ""; })
.replace(/\n\|\|(.*)/g, function(str, p1){ return "\n|http://" + p1 + "\n|https://" + p1; })
.replace(/[\r\n]+/g, "\n")
.replace(/^\s*|\s*$/g, "")
.replace(/\^/g, "/")
;
var exclude = filtertext.split(/[\r\n]+/).map(function(line, i, a) {
var str = line;
if (str[0] === "|") {
str = str.substr(1) + "*";
} else {
str = "*" + str + "*";
}
return str;
});
exclude =
['Opera Preferences version 2.1'
,'; Do not edit this file while Opera is running'
,'; This file is stored in UTF-8 encoding'
,''
,'[prefs]'
,'prioritize excludelist=1'
,''
,'[include]'
,'*'
,''
,'[exclude]'
,exclude.join("\n").replace(/\*+/g, "*")
].join("\n");
var cssdomains = { "ALL": [] };
textcss.forEach(function(line,i,a) {
if (line[0] === "~") return;
if (line.indexOf("##") === 0) {
cssdomains.ALL.push(line.substr(2));
return;
}
var aaa = line.split("##");
var dom = aaa[0];
if (!cssdomains[dom]) cssdomains[dom] = [];
cssdomains[dom].push(aaa[1]);
});
var cssres = ["@namespace url(http://www.w3.org/1999/xhtml);"];
if (cssdomains.ALL.length)
cssres.push(cssdomains.ALL.join(", ") + '\n { display: none !important; }');
delete cssdomains.ALL;
for(var n in cssdomains) {
var d = n.split(",").map(function(d){ return 'domain("'+ d +'")'}).join(",");
cssres.push('@-moz-document ' + d + '{\n '+ cssdomains[n].join(",") +'\n { display: none !important; }\n}');
}
window.open('data:text/html;charset=utf-8,' + encodeURIComponent('<frameset rows="*,*">'
+ '<frame src="data:text/plain;charset=utf-8,' + encodeURIComponent(exclude) + '">'
+ '<frame src="data:text/css;charset=utf-8,' + encodeURIComponent(cssres.join("\n\n")) + '">'
+ '</frameset>'));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment