Created
April 28, 2009 10:03
-
-
Save Griever/103061 to your computer and use it in GitHub Desktop.
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 Imitation lightbox | |
// @namespace http://d.hatena.ne.jp/Griever/ | |
// @include http://* | |
// @run-at document-start | |
// ==/UserScript== | |
({ | |
STOP_TARGET_FRAME: true, // 他のフレームで開くリンクは無視 | |
REMOVED_SCROLL: true, // 画像をクリックして消したあと元の位置にスクロールする | |
IMAGE_RESIZING: true, // 大きい画像を画面内に縮小する | |
REGEXP: [ | |
{ | |
name: 'Google Images' | |
,url: /^.*?imgurl=(https?:\/\/.+?)\&.*?$/i | |
,newURL: "$1" | |
} | |
,{ | |
name: 'akiba-pc.watch.impress.co.jp' | |
,url: /^(http:\/\/akiba\-pc\.watch\.impress\.co\.jp\/hotline\/.*)\.html$/i | |
,newURL: "$1.jpg" | |
} | |
,{ | |
name: 'impress' | |
,url: /^(http:\/\/[^/]+\.impress\.co\.jp\/img\/.*?\/)html\/(\w+\.jpg)\.html.*?$/i | |
,newURL: "$1$2" | |
} | |
,{ | |
name: 'ITMedia' | |
,url: /^(http:\/\/image\.itmedia\.co\.jp\/)\w\/\w+\/(.*\.jpg).*?$/i | |
,newURL: "$1$2" | |
} | |
,{ | |
name: 'journal.mycom' | |
,url: /^http:\/\/journal\.mycom\.co\.jp\/photo\/(.*\.jpg).*?$/i | |
,newURL: "http://j.mycom.jp/$1" | |
} | |
,{ | |
name: 'http://mantan-web.jp' | |
,url: /^(http:\/\/mantan-web\.jp\/)(gallery\/.*?\/)([1-9]+).html.*?$/i | |
,newURL: "$1assets/images/$20$3.jpg" | |
} | |
,{ | |
name: 'asablo.jp' | |
,url: /^(http:\/\/.+asablo.jp\/blog\/)imgview(\/.+\.(?:jpe?g|gif|png|bmp))\.html.*?$/i | |
,newURL: "$1img$2" | |
} | |
,{ | |
name: 'Yahoo Blogs' | |
,url: /^.*?(http:\/\/img[0-9]?\.blogs\.yahoo\.co\.jp\/.*?\/img_[0-9_]+).*?$/i | |
,newURL: "$1" | |
} | |
,{ | |
name: '2ch etc' | |
,url: /^(http:\/\/)(?:ime\.nu|pinktower\.com)\/(.*\.(?:jpe?g|gif|png|bmp)).*?$/i | |
,newURL: "$1$2" | |
} | |
,{ | |
url: /^.*?(https?:\/\/(?:[^\/][^\/]|[^#]+)\.(?:jpe?g|gif|png|bmp)).*?$/i | |
,newURL: "$1" | |
} | |
], | |
get imageTemplate() { | |
var img = document.createElement('img'); | |
img.alt = 'img'; | |
img.className = 'imitation-lightbox-image'; | |
img.style.cssText = 'position:absolute; z-index:20000; border:1px solid blue;'; | |
if (this.IMAGE_RESIZING) { | |
img.style.maxWidth = '100%'; | |
img.style.maxHeight = '100%'; | |
img.style.WebkitBoxSizing = | |
img.style.OBoxSizing = | |
img.style.MozBoxSizing = | |
img.style.boxSizing = 'border-box'; | |
} | |
delete this.imageTemplate; | |
return this.imageTemplate = img; | |
}, | |
isChrome: !!window.chrome, | |
init: function() { | |
this.REGEXP.forEach(function(e){ | |
if (typeof e.url == 'string') | |
e.url = new RegExp(e.url, 'i'); | |
}); | |
document.addEventListener('click', this, true); | |
document.addEventListener((this.isChrome? 'keydown' : 'keypress'), this, true); | |
}, | |
handleEvent: function(event) { | |
switch(event.type) { | |
case 'click' : this.onClick(event); break; | |
case 'keydown' : this.onKeypress(event); break; | |
case 'keypress': this.onKeypress(event); break; | |
case 'error' : this.onError(event); break; | |
} | |
}, | |
onClick: function(event) { | |
if (event.button != 0) return; | |
if (event.ctrlKey || event.shiftKey || event.altKey) return; | |
var elem = event.target; | |
if (elem.className.indexOf('imitation-lightbox-image') >= 0) { | |
if (this.REMOVED_SCROLL) | |
elem.scrollIntoView(true); | |
elem.parentNode.removeChild(elem); | |
event.preventDefault(); | |
event.stopPropagation(); | |
return; | |
} | |
do { | |
if (typeof elem.hasAttribute === 'function' && elem.hasAttribute('href')) | |
break; | |
} while(elem = elem.parentNode); | |
if (!elem) return; | |
if (!this.STOP_TARGET_FRAME) { | |
var target = elem.getAttribute('target'); | |
if (target && target !== '_blank' && target !== 'self') | |
return; | |
} | |
var href = elem.href; | |
var info; | |
this.REGEXP.some(function(e){ | |
if (e.url.test(href)){ | |
info = e; | |
return true; | |
} | |
}); | |
if (!info) return; | |
event.preventDefault(); | |
event.stopPropagation(); | |
var doc = elem.ownerDocument; | |
var win = doc.defaultView; | |
var img = this.imageTemplate.cloneNode(false); | |
img.src = href.replace(info.url, info.newURL); | |
img.style.top = win.pageYOffset + 'px'; | |
img.style.left = win.pageXOffset + 'px'; | |
img.addEventListener('error', this, false); | |
doc.body.appendChild(img); | |
}, | |
onKeypress: function(event) { | |
if (event.keyCode == 27 && !event.shiftKey && !event.ctrlKey && !event.altKey) { | |
var imgs = Array.prototype.slice.call(document.getElementsByClassName('imitation-lightbox-image')); | |
imgs.forEach(function(e) { | |
e.parentNode.removeChild(e); | |
}); | |
} | |
}, | |
onError: function(event) { | |
var elem = event.target; | |
elem.parentNode.removeChild(elem); | |
window.status = 'Error:' + elem.href; | |
} | |
}).init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Firefox, Opera, Chrome で動くように書いたつもり。
Chrome か Opera で拡張子.js で実行するとページ読み込み前から動作する