Created
December 11, 2008 14:51
-
-
Save koyachi/34734 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Motsu Tabetai | |
// @namespace http://ikenie.com/ | |
// @include * | |
// @require http://gist.github.com/raw/3242/1a7950e033a207efcfc233ae8d9939b676bdbf46 | |
// @require http://gist.github.com/raw/34615/04333b7e307eb029462680e4f4cf961f72f4324c | |
// ==/UserScript== | |
(function() { | |
var DATABASE_URL = 'http://wedata.net/databases/Motsu/items.json'; | |
var map = {}, words = []; | |
function replacer(str) { | |
return ['<img src="', map[str], '" alt="', str, '"/>'].join(''); | |
} | |
function motsu(doc) { | |
$X('//text()', doc).forEach(function(textNode) { | |
var data = textNode.data; | |
if(data.match(words.join('|'))) { | |
var replaced = data.replace(new RegExp(words.join('|'), | |
'g'), replacer); | |
var div = document.createElement('div'); | |
var range = document.createRange(); | |
div.innerHTML = replaced; | |
range.selectNodeContents(div); | |
var df = range.extractContents(); | |
range.setStartBefore(textNode); | |
range.insertNode(df); | |
range.selectNode(textNode); | |
range.deleteContents(); | |
} | |
}); | |
} | |
var database = new Wedata.Database(DATABASE_URL); | |
if (document.referrer.match(/http:\/\/wedata\.net\/.*\/[new|edit]/) && | |
document.location.href.match(/http:\/\/wedata\.net\/items\/\d+/)) { | |
database.clearCache(); | |
database = new Wedata.Database(DATABASE_URL); | |
} | |
database.get(function(items) { | |
items.forEach(function(item) { | |
map[item.name] = item.data.image_url; | |
}); | |
for(var i in map) { | |
words.push(i); | |
} | |
motsu(document); | |
}); | |
GM_registerMenuCommand('Motsu Tabetai - clear cache', function() { | |
database.clearCache(); | |
}); | |
setTimeout(function() { | |
if (window.AutoPagerize && window.AutoPagerize.addDocumentFilter) | |
{ | |
window.AutoPagerize.addDocumentFilter(motsu); | |
} | |
}, 0); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment