Created
March 15, 2010 11:00
-
-
Save shibason/332736 to your computer and use it in GitHub Desktop.
ニコニコ大百科⇔ピクペディアを相互参照するGreasemonkeyスクリプト
This file contains 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 NicoVideo-Pixiv CrossPedia | |
// @namespace http://d.hatena.ne.jp/shibason/ | |
// @description ニコニコ大百科⇔ピクペディアの相互参照 | |
// @include http://www.nicovideo.jp/watch/* | |
// @include http://dic.nicovideo.jp/a/* | |
// @include http://www.pixiv.net/member_illust.php* | |
// @include http://dic.pixiv.net/a/* | |
// @author shibason | |
// @version 1.0 | |
// ==/UserScript== | |
(function() { | |
var BasePedia = function() {}; | |
BasePedia.prototype.getIcon = function(keyword, callback) { | |
var self = this; | |
GM_xmlhttpRequest({ | |
method: this.method, | |
url: this.getCheckURL(keyword), | |
onload: function(res) { | |
callback(self.createIcon(keyword, self.isExist(res))); | |
} | |
}); | |
}; | |
BasePedia.prototype.createIcon = function(keyword, isExist) { | |
var a = document.createElement('a'); | |
a.setAttribute('target', '_blank'); | |
a.setAttribute('href', this.getPageURL(keyword)); | |
var img = document.createElement('img'); | |
if (isExist) { | |
a.setAttribute('title', this.getOnTitle(keyword)); | |
img.setAttribute('src', this.onImageURL); | |
} else { | |
a.setAttribute('title', this.getOffTitle(keyword)); | |
img.setAttribute('src', this.offImageURL); | |
} | |
a.appendChild(img); | |
return a; | |
}; | |
var NicoPedia = function() { | |
this.method = 'GET'; | |
this.onImageURL = 'http://res.nimg.jp/img/common/icon/dic_on.gif'; | |
this.offImageURL = 'http://res.nimg.jp/img/common/icon/dic_off.gif'; | |
}; | |
NicoPedia.prototype = new BasePedia(); | |
NicoPedia.prototype.getPageURL = function(keyword) { | |
return 'http://dic.nicovideo.jp/a/' + encodeURIComponent(keyword); | |
}; | |
NicoPedia.prototype.getCheckURL = function(keyword) { | |
keyword = encodeURIComponent(keyword); | |
return 'http://api.nicodic.jp/e/__nicopedia_api_1_e_fn_' + | |
keyword.replace(/%/g, '_') + '/' + keyword; | |
}; | |
NicoPedia.prototype.isExist = function(res) { | |
return !!res.responseText.match(/\(\[1\]\)/); | |
}; | |
NicoPedia.prototype.getOnTitle = function(keyword) { | |
return 'ニコニコ大百科 ' + keyword + ' の記事を読む'; | |
}; | |
NicoPedia.prototype.getOffTitle = function(keyword) { | |
return 'ニコニコ大百科 ' + keyword + ' の記事を書く'; | |
}; | |
var PixPedia = function() { | |
this.method = 'HEAD'; | |
this.onImageURL = 'http://source.pixiv.net/source/images/pedia_on.gif'; | |
this.offImageURL = 'http://source.pixiv.net/source/images/pedia_off.gif'; | |
}; | |
PixPedia.prototype = new BasePedia(); | |
PixPedia.prototype.getPageURL = function(keyword) { | |
return 'http://dic.pixiv.net/a/' + encodeURIComponent(keyword); | |
}; | |
PixPedia.prototype.getCheckURL = PixPedia.prototype.getPageURL; | |
PixPedia.prototype.isExist = function(res) { | |
return !res.responseText; | |
}; | |
PixPedia.prototype.getOnTitle = function(keyword) { | |
return 'ピクペディア ' + keyword + ' の記事を読む'; | |
}; | |
PixPedia.prototype.getOffTitle = function(keyword) { | |
return 'ピクペディア ' + keyword + ' の記事を書く'; | |
}; | |
var BaseProcessor = function() {}; | |
BaseProcessor.prototype.process = function() { | |
this.pedia = new this.pediaClass(); | |
var elements = document.querySelectorAll(this.query); | |
for (var i = 0; i < elements.length; i++) { | |
this.addIcon(elements[i]); | |
} | |
}; | |
BaseProcessor.prototype.addIcon = function(element) { | |
var self = this; | |
this.pedia.getIcon(element.textContent, function(icon) { | |
self.insertIcon(element, icon); | |
}); | |
}; | |
var NicoVideoProcessor = function() { | |
this.pediaClass = PixPedia; | |
this.query = 'div#video_tags a.nicopedia'; | |
}; | |
NicoVideoProcessor.prototype = new BaseProcessor(); | |
NicoVideoProcessor.prototype.insertIcon = function(element, icon) { | |
icon.firstChild.style.verticalAlign = 'middle'; | |
if (element.nextSibling && element.nextSibling.tagName == 'A') { | |
element = element.nextSibling; | |
} | |
element.parentNode.insertBefore(icon, element.nextSibling); | |
}; | |
var NicoPediaProcessor = function() { | |
this.pediaClass = PixPedia; | |
this.query = 'div#main div#article-tab-nico div.article-tab-nico h1'; | |
} | |
NicoPediaProcessor.prototype = new BaseProcessor(); | |
NicoPediaProcessor.prototype.insertIcon = function(element, icon) { | |
icon.style.marginLeft = '0.5em'; | |
icon.firstChild.style.verticalAlign = 'bottom'; | |
element.appendChild(icon); | |
}; | |
var PixivProcessor = function() { | |
this.pediaClass = NicoPedia; | |
this.query = 'span#tags a:not([target])'; | |
} | |
PixivProcessor.prototype = new BaseProcessor(); | |
PixivProcessor.prototype.insertIcon = function(element, icon) { | |
icon.firstChild.style.verticalAlign = 'middle'; | |
do { | |
element = element.nextSibling; | |
} while (element && element.tagName != 'A'); | |
element.parentNode.insertBefore(icon, element.nextSibling); | |
}; | |
var PixPediaProcessor = function() { | |
this.pediaClass = NicoPedia; | |
this.query = 'div#wrapper h1:first-child a span'; | |
}; | |
PixPediaProcessor.prototype = new BaseProcessor(); | |
PixPediaProcessor.prototype.insertIcon = function(element, icon) { | |
icon.style.marginLeft = '0.3em'; | |
element.parentNode.parentNode.appendChild(icon); | |
}; | |
(new ({ | |
'www.nicovideo.jp': NicoVideoProcessor, | |
'dic.nicovideo.jp': NicoPediaProcessor, | |
'www.pixiv.net': PixivProcessor, | |
'dic.pixiv.net': PixPediaProcessor | |
}[location.hostname])()).process(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment