Created
June 13, 2010 04:04
-
-
Save hitode909/436334 to your computer and use it in GitHub Desktop.
expand-image-link.user.js
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 expand-image-link | |
// @namespace http://www.hatena.ne.jp/hitode909 | |
// @include * | |
// @exclude http://fastladder.com/reader/ | |
// ==/UserScript== | |
(function() { | |
var meta = document.querySelector('meta[name="expand-image"]'); | |
if (meta && meta.content == 'no') return; | |
// http://remysharp.com/2010/07/21/throttling-function-calls/ | |
function throttle(fn, delay) { | |
var timer = null; | |
return function () { | |
var context = this, args = arguments; | |
clearTimeout(timer); | |
timer = setTimeout(function () { | |
fn.apply(context, args); | |
}, delay); | |
}; | |
} | |
var expand = throttle(function(target) { | |
if (!target) target = document.body; | |
if (!target.tagName || target.tagName.toLowerCase() == 'img') return; | |
Array.prototype.slice.apply(target.querySelectorAll('a')).forEach(function(a) { | |
if (a.href.split(/\?|\\#/)[0].match(/(jpg|gif|png|bmp)$/i) | |
&& !a.querySelector('img') | |
&& !document.querySelector('img[src^="' + a.href + '"]')) { | |
var img = document.createElement('img'); | |
img.src = a.href; | |
img.style.maxWidth = '100%'; | |
a.appendChild(img); | |
} | |
}); | |
}, 100); | |
document.body.addEventListener('DOMContentLoaded',function(ev){ | |
expand(document.body); | |
}, false); | |
document.body.addEventListener('DOMNodeInserted',function(ev){ | |
expand(document.body); | |
}, false); | |
document.body.addEventListener('mouseup',function(ev){ | |
expand(document.body); | |
}, false); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment