JavaScript to Block Sites Based on Word Content?
May 11, 2009 9:20 AM Subscribe
How can I use JavaScript to block a website if it contains specific words?
I have the wonderful GlimmerBlocker installed on my Mac. I'm hoping to use it to block a website if the website contains a specific word.
I'm using this JavaScript to successfully block a site:
(function () {
var b = (document.getElementsByTagName("body")[0]);
b.setAttribute('style', 'display:none!important');
alert("This site has been blocked!");
})();
How can I modify it to block only sites that contain specific words? It would also be nice if I could define sites that are guaranteed not to be blocked. Thanks! :)
I have the wonderful GlimmerBlocker installed on my Mac. I'm hoping to use it to block a website if the website contains a specific word.
I'm using this JavaScript to successfully block a site:
(function () {
var b = (document.getElementsByTagName("body")[0]);
b.setAttribute('style', 'display:none!important');
alert("This site has been blocked!");
})();
How can I modify it to block only sites that contain specific words? It would also be nice if I could define sites that are guaranteed not to be blocked. Thanks! :)
Quick and dirty, and probably won't even work but should give you a good idea as to what you need to do.
posted by xbonesgt at 10:35 AM on May 11, 2009
(function () { var b = (document.getElementsByTagName("body")[0]); var evilKeyword = "word to block"; // if the page contents contains the keyword, AND // the page is not an acceptable one, THEN // block it if ((b.innerHTML.indexOf(evilKeyword) > -1) && (!isInWhitelist())) { b.setAttribute('style', 'display:none!important'); alert("This site has been blocked!"); } })(); function isInWhitelist(pageAddress) { var safeSites = new Array(); safeSites[0] = "metafilter.com"; safeSites[1] = "zombo.com"; // etc. ... for (int i = 0; i <> -1) { return 1; } } return 0; }>
posted by xbonesgt at 10:35 AM on May 11, 2009
dang, it looked good in preview. See, told you it wouldn't work!
Pass an argument to the isInWhitelist() function, and make sure you write the for() loop properly.
posted by xbonesgt at 10:37 AM on May 11, 2009
Pass an argument to the isInWhitelist() function, and make sure you write the for() loop properly.
posted by xbonesgt at 10:37 AM on May 11, 2009
Response by poster: Okay, this is what I have so far and it doesn't work (modified sections in bold):
(function () {
var b = (document.getElementsByTagName("body")[0]);
var evilKeyword = "word to block";
// if the page contents contains the keyword, AND
// the page is not an acceptable one, THEN
// block it
if ((b.innerHTML.indexOf(evilKeyword) > -1) && (!isInWhitelist(document.write(document.domain)))) {
b.setAttribute('style', 'display:none!important');
alert("This site has been blocked!");
}
})();
function isInWhitelist(pageAddress) {
var safeSites = new Array();
safeSites[0] = "metafilter.com";
safeSites[1] = "zombo.com";
// etc. ...
for (int i = 0; i <> -1) {
return 1;
}
}
return 0;
}>
Also, how do I do it with multiple keywords?
posted by 47triple2 at 11:29 AM on May 11, 2009
(function () {
var b = (document.getElementsByTagName("body")[0]);
var evilKeyword = "word to block";
// if the page contents contains the keyword, AND
// the page is not an acceptable one, THEN
// block it
if ((b.innerHTML.indexOf(evilKeyword) > -1) && (!isInWhitelist(document.write(document.domain)))) {
b.setAttribute('style', 'display:none!important');
alert("This site has been blocked!");
}
})();
function isInWhitelist(pageAddress) {
var safeSites = new Array();
safeSites[0] = "metafilter.com";
safeSites[1] = "zombo.com";
// etc. ...
for (int i = 0; i <> -1) {
return 1;
}
}
return 0;
}>
Also, how do I do it with multiple keywords?
posted by 47triple2 at 11:29 AM on May 11, 2009
Best answer: This should work for Greasemonkey:
// ==UserScript==
// @name Kill By Keyword
// @namespace KBK
// @description Kills webpages based upon keywords
// @include http://*
// ==/UserScript==
(function() {
//
// Get the innerHtml for the current page
//
var b = document.body;
var bih = document.body.innerHTML;
//
// Here is the list of keywords to block
//
var evilKeywords = new Array("platypus", "aardvark", "etc.");
//
// See if any of the keywords are in the innerHtml
//
for (key in evilKeywords)
if (bih.indexOf(evilKeywords[key]) > -1) {
b.setAttribute('style', 'display:none!important');
alert("This site has been blocked!");
break;
};
})();
That said, you probably want to get a real parental control (I use SafeEyes). This isn't going to stop a savvy kid for very long.
posted by srt19170 at 10:06 AM on May 12, 2009
// ==UserScript==
// @name Kill By Keyword
// @namespace KBK
// @description Kills webpages based upon keywords
// @include http://*
// ==/UserScript==
(function() {
//
// Get the innerHtml for the current page
//
var b = document.body;
var bih = document.body.innerHTML;
//
// Here is the list of keywords to block
//
var evilKeywords = new Array("platypus", "aardvark", "etc.");
//
// See if any of the keywords are in the innerHtml
//
for (key in evilKeywords)
if (bih.indexOf(evilKeywords[key]) > -1) {
b.setAttribute('style', 'display:none!important');
alert("This site has been blocked!");
break;
};
})();
That said, you probably want to get a real parental control (I use SafeEyes). This isn't going to stop a savvy kid for very long.
posted by srt19170 at 10:06 AM on May 12, 2009
Best answer: Okay, the script that srt19170 gave didn't work quite perfectly in GlimmerBlocker. Here's the script I'm currently using, I don't know if it will work in GreaseMonkey or not:
// ==UserScript==
// @name Kill By Keyword
// @namespace KBK
// @description Kills webpages based upon keywords
// @include http://*
// ==/UserScript==
(function() {
//
// Get the innerHtml and URL for the current page
//
var b = document.body;
var bih = document.body.innerHTML;
var tsu = document.location;
var tsh = document.location.hostname;
//
// Here is the list of keywords to block and sites to whitelist
//
var evilKeywords = new Array("platypus", "coolz", "LOLz", "etc.");
var goodSites = /youtube|google|metafilter|wikipedia/;
//
// Find out if the site is whitelisted
//
var searchGoodSites = tsh.search(goodSites);
if(searchGoodSites <>
//
// It isn't! Quick, find out if it has any of the evil keywords!
//
for (thekey in evilKeywords){
var texttry1 = ((bih.indexOf(evilKeywords[thekey])) + 1);
var texttry2 = (bih.indexOf(evilKeywords[thekey], texttry1));
if (texttry2 > -1) {
//
// It does! Block the site!
//
b.setAttribute('style', 'display:none!important');
alert("This site has been blocked!");
break;}
}
break;}
})();>
Thank you all for the help. :)
posted by 47triple2 at 12:45 PM on May 13, 2009
// ==UserScript==
// @name Kill By Keyword
// @namespace KBK
// @description Kills webpages based upon keywords
// @include http://*
// ==/UserScript==
(function() {
//
// Get the innerHtml and URL for the current page
//
var b = document.body;
var bih = document.body.innerHTML;
var tsu = document.location;
var tsh = document.location.hostname;
//
// Here is the list of keywords to block and sites to whitelist
//
var evilKeywords = new Array("platypus", "coolz", "LOLz", "etc.");
var goodSites = /youtube|google|metafilter|wikipedia/;
//
// Find out if the site is whitelisted
//
var searchGoodSites = tsh.search(goodSites);
if(searchGoodSites <>
//
// It isn't! Quick, find out if it has any of the evil keywords!
//
for (thekey in evilKeywords){
var texttry1 = ((bih.indexOf(evilKeywords[thekey])) + 1);
var texttry2 = (bih.indexOf(evilKeywords[thekey], texttry1));
if (texttry2 > -1) {
//
// It does! Block the site!
//
b.setAttribute('style', 'display:none!important');
alert("This site has been blocked!");
break;}
}
break;}
})();>
Thank you all for the help. :)
posted by 47triple2 at 12:45 PM on May 13, 2009
« Older Is there a simple way to integrate Salesforce and... | How should my college (i.e., workplace) social... Newer »
This thread is closed to new comments.
posted by a robot made out of meat at 10:29 AM on May 11, 2009