Paste: Urban Dictionary Indexer
Author: | KnifeySpooney |
Mode: | javascript |
Date: | Sun, 30 Jan 2011 06:26:38 |
Plain Text |
var pages = "ABCDEFGHIJKLMNOPQRSTUVWXYZ*".split("");
var idx = 0;
var pid = 0;
var key = "&index=yes";
var console;
var counter;
var button;
var working = true;
var done = false;
if (new RegExp(key).test(document.location) === false) {
if (document.body.className == "browse_controller")
insertBar();
}
function start() {
var body = document.body;
body.innerHTML = "";
body.removeAttribute("class");
body.style.textAlign = "center";
body.style.padding = "0";
body.style.backgroundColor = "white";
counter = document.createElement("p");
counter.style.fontSize = "1.3em";
body.appendChild(counter);
button = document.createElement("input");
button.setAttribute("type", "button");
button.value = "Pause";
button.addEventListener("click", function(){toggleWorking();}, false);
body.appendChild(button);
var div = document.createElement("div");
div.style.width = "800px";
div.style.height = "600px";
div.style.margin = "5px auto";
console = document.createElement("textarea");
console.style.width = "100%";
console.style.height = "100%";
div.appendChild(console);
body.appendChild(div);
requestNextPage();
}
function close() {
document.body.removeChild(document.getElementById("_GM_bar"));
}
function load(page, num) {
var seed = "&seed="+parseInt((new Date().getTime()) / 1000);
GM_xmlhttpRequest({
method: 'GET',
url: 'http://www.urbandictionary.com/browse.php?character='+page+'&page='+num+key+seed,
headers: {
'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
'Accept': 'text/html',
},
onload: function(responseDetails) {
requestNextPage(index(responseDetails.responseText));
}
});
}
function requestNextPage() {
if (working) {
if (done) {
pid = 0;
idx++;
if (idx >= pages.length) {
alert("Indexing complete.");
return;
}
}
if (idx < pages.length) {
counter.innerHTML = "Searching page "+pages[idx]+pid;
load(pages[idx], pid);
}
}
}
function index(html) {
html = html.substring(html.indexOf("<body"), html.indexOf("</html>"));
var root = document.createElement("div");
root.innerHTML = html;
var storage = new Array();
var a = root.getElementsByTagName("a");
for (var i=0; i<a.length; i++) {
if (/^urbantip$/i.test(a[i].className))
storage.push(a[i].innerHTML);
}
done = (html.indexOf('disabled next_page') >= 0) ? true : false;
console.value += "==== Page "+pages[idx]+pid+" ====\n";
console.value += storage.join('\n').stripX()+'\n';
pid++;
return done;
}
String.prototype.stripX = function() {
return this.replace(/\&/g, '&').replace(/\</g, '<').replace(/\>/g, '>');
}
function toggleWorking() {
if (working) {
working = false;
button.value = "Resume";
} else {
working = true;
button.value = "Pause";
requestNextPage();
}
}
function insertBar() {
var bar = document.createElement("div");
bar.id = "_GM_bar";
bar.style.position = "fixed";
bar.style.backgroundColor = "rgba(255, 54, 54, 0.5)";
bar.style.borderBottom = "1px solid rgba(0, 0, 0, 0.5)";
bar.style.width = "100%";
bar.style.top = "0px";
bar.style.left = "0px";
var box = document.createElement("div");
box.style.width = "600px";
box.style.margin = "3px auto";
bar.style.textAlign = "center";
var elm = document.createElement("span");
elm.style.color = "white";
elm.style.textShadow = "#000 0 0 3px";
elm.style.fontWeight = "bold";
elm.style.fontSize = "1.2em";
elm.style.clear = "both";
elm.innerHTML = "Would you like to begin indexing Urban Dictionary?";
box.appendChild(elm);
elm = document.createElement("input");
elm.setAttribute("type", "button");
elm.setAttribute("value", "Start");
elm.style.marginLeft = "10px";
elm.addEventListener("click", function(){start();}, false);
box.appendChild(elm);
elm = document.createElement("input");
elm.setAttribute("type", "button");
elm.setAttribute("value", "Not now");
elm.style.marginLeft = "3px";
elm.addEventListener("click", function(){close();}, false);
box.appendChild(elm);
bar.appendChild(box);
document.body.appendChild(bar);
}
New Annotation