Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save saitamanodoruji/5756312 to your computer and use it in GitHub Desktop.
Save saitamanodoruji/5756312 to your computer and use it in GitHub Desktop.
Photo - MediaWiki を修正する Tombloo パッチ
Tombloo.Service.extractors.register([
{
name : 'Photo - MediaWiki Thumbnail',
ICON : 'http://www.mediawiki.org/favicon.ico',
check : function(ctx){
return ctx.onLink &&
hasElementClass(ctx.document.body, 'mediawiki') &&
/wiki\/.+:/.test(ctx.link.href) &&
(/\.(svg|png|gif|jpe?g)$/i).test(ctx.link.href);
},
extract : function(ctx){
return request(ctx.link.href).addCallback(function(res){
// 相対パスを処理するためbaseURIを指定する
// base要素が複数ある場合は最初のhref属性が優先するので
// 既にbaseがあれば無視されるようheadの最後に加える
var html = res.responseText.replace(/(<\/head>)/, '<base href="' + ctx.link.href + '" />$1');
var doc = convertToHTMLDocument(html);
// SVGの場合サムネイルを取得する
// XPath中で/@hrefのように指定すると相対パスのまま取り出されてしまうのでだめ
var itemUrl = (/\.svg$/i).test(ctx.link.href)?
$x('id("file")/a/img', doc).src:
$x('id("file")/a', doc).href;
return {
type : 'photo',
item : ctx.title,
itemUrl : itemUrl,
};
});
}
},
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment