Skip to content

Instantly share code, notes, and snippets.

@0-oo
Last active December 17, 2016 08:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0-oo/29e6a7270d58c715a68c to your computer and use it in GitHub Desktop.
Save 0-oo/29e6a7270d58c715a68c to your computer and use it in GitHub Desktop.
Greasemonkey/Tampermonkey: The search results filter/blocker for Google Search. See my comment to know how to use.
// ==UserScript==
// @name Google Search URL Filter
// @namespace http://0-oo.net/
// @description The search results filter/blocker for Google Search
// @homepage http://0-oo.net/log/category/greasemonkey/google-instant-url-filter/
// @version 0.4.1
// @include http*://www.google.tld/search*
// @include http*://www.google.tld/webhp*
// @include http*://www.google.tld/#*
// @include http*://www.google.tld/
// @grant GM_getValue
// @grant GM_setValue
// ==/UserScript==
//
// ( The MIT License )
//
(function() {
var SCRIPT_NAME = "Google Search URL Filter";
function setButtonStyle(btn, label, onclick) {
btn.style.padding = "2px";
btn.style.cursor = "pointer";
btn.textContent = label;
btn.addEventListener("click", onclick, false);
}
var btn = document.body.appendChild(document.createElement("button"));
btn.style.position = "absolute";
btn.style.top = 0;
btn.style.right = 0;
btn.style.zIndex = 1000;
// Edit URL list
setButtonStyle(btn, "URL Filter", function() {
var con = document.body.appendChild(document.createElement("div"));
con.style.position = "fixed";
con.style.top = 0;
con.style.right = 0;
con.style.zIndex = 1000;
con.style.padding = "1px 2px";
con.style.backgroundColor = "#ccc";
con.style.border = "solid 1px #666";
con.style.borderRadius = "10px";
con.style.textAlign = "center";
function append(name) {
return con.appendChild(document.createElement(name));
}
append("b").textContent = "[" + SCRIPT_NAME + "] URL list";
append("br");
var ta = append("textarea");
ta.cols = 50;
ta.rows = 25;
ta.value = GM_getValue("urls") || "";
append("br");
setButtonStyle(append("button"), "Cancel", function() {
document.body.removeChild(con);
});
var spacer = append("span");
spacer.textContent = " ";
spacer.style.padding = "0 50px"
setButtonStyle(append("button"), "Save", function() {
GM_setValue("urls", ta.value.trim());
location.reload();
});
});
var urls = GM_getValue("urls");
if (!urls) {
return;
}
urls = urls.replace(/([.?])/g, "\\$1").replace(/\*/g, ".*").replace(/\n/g, "|");
try {
var regex = new RegExp(urls);
} catch (e) {
alert(SCRIPT_NAME + ": Invalid URLs");
return;
}
setInterval(function() { // Watching the result page
var rso = document.getElementById("rso");
if (!rso || rso.filtered) {
return;
}
rso.filtered = true;
var results = rso.getElementsByClassName("rc")
for (var i = 0; i < results.length; i++) {
var result = results[i];
var link = result.getElementsByTagName("a")[0];
if (link && link.href.match(regex)) {
link.style.color = "#ccc";
var ems = link.getElementsByTagName("em"); // Keywords
for (var j = 0; j < ems.length; j++) {
ems[j].style.color = "inherit";
}
var spans = result.getElementsByTagName("span"); // e.g. "[PDF]"
for (var k = 0; k < spans.length; k++) {
spans[k].style.color = "#ccc";
}
var div = result.getElementsByClassName("s")[0];
if (div) {
div.style.display = "none";
}
}
}
// Hide ads
var ads = document.getElementsByClassName("ads-ad");
for (var j = 0; j < ads.length; j++) {
ads[j].style.display = "none";
}
}, 100);
})();
@0-oo
Copy link
Author

0-oo commented Sep 3, 2015

This script is the search results filter/blocker for Google search results.
Google検索の検索結果から、表示したくないURLを非表示(グレイアウト)にします。

This script add the "URL Filter" button to the top right of Google search results page.
Click and edit the list of URLs (or domains) that you want to hide.
このスクリプトをインストールすると、Google検索結果の右上に「URL Filter」ボタンが表示されます。
ボタンをクリックすると、非表示にするURL(またはドメインでもOK)を登録できます。

You can register multiple URLs with line breaks. (One URL per line.)
非表示にしたいURLを複数登録する場合は、URLごとに改行してください。

You can use "" as a wildcard character. (ex. .example./hoge)
URLには、"
"をワイルドカードとして使えます。(例 ".example./hoge")

And you can use regular expressions, except "", "." and "?".
また、正規表現も使えます。ただし正規表現のうち、"
" "." "?" は使えません。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment