Skip to content

Instantly share code, notes, and snippets.

@emonkak
Created September 21, 2011 06:06
Show Gist options
  • Save emonkak/1231368 to your computer and use it in GitHub Desktop.
Save emonkak/1231368 to your computer and use it in GitHub Desktop.
ldr-reload-image-without-referer.user.js
// ==UserScript==
// @name ldr-reload-image-without-referer
// @include http://reader.livedoor.com/reader/
// @include http://fastladder.com/reader/
// ==/UserScript==
(function() {
function initPool() {
var origin = location.protocol + '//' + location.host;
var html = document.implementation.createHTMLDocument('');
var iframe = document.createElement('iframe');
var script = document.createElement('script');
script.type = 'text/javascript';
script.textContent = '(' + (function(origin) {
var channel = new MessageChannel();
var port = channel.port1;
window.parent.postMessage('', [channel.port2], origin);
port.onmessage = function(e) {
var img = document.createElement('img');
var uri = e.data.uri;
var key = uri.indexOf('?') > -1 ? '&' : '?';
img.src = uri + key;
img.onload = function(e) {
port.postMessage({ loaded: true, uri: uri, key: key });
e.target.parentNode.removeChild(e.target);
};
img.onerror = function(e) {
port.postMessage({ loaded: false, uri: uri });
e.target.parentNode.removeChild(e.target);
};
document.body.appendChild(img);
};
}).toString() + ')(' + JSON.stringify(origin) + ')';
html.body.appendChild(script);
iframe.src = 'data:text/html,' + html.documentElement.outerHTML;
iframe.style.visibility = 'hidden';
iframe.width = 0;
iframe.height = 0;
document.body.appendChild(iframe);
}
function message(str) {
document.getElementById('message').textContent = str;
}
var loading = {};
window.addEventListener('message', function(e) {
if (e.origin !== 'null' || !e.ports[0])
return;
var port = e.ports[0];
document.body.addEventListener('error', function(e) {
if (e.target.tagName.toUpperCase() === 'IMG') {
if (e.target.src in loading) {
loading[e.target.src].push(e.target);
} else {
loading[e.target.src] = [e.target];
port.postMessage({ uri: e.target.src });
}
}
}, true);
port.onmessage = function(e) {
if (e.data.uri in loading) {
if (e.data.loaded) {
loading[e.data.uri].forEach(function(img) {
if (img)
img.src = e.data.uri + e.data.key;
});
} else {
message('Error: ' + e.target.src);
}
delete loading[e.data.uri];
}
};
window.removeEventListener('message', arguments.callee, false);
}, false);
document.addEventListener('click', function(e) {
if (e.target.tagName.toUpperCase() === 'A')
e.target.rel = 'noreferrer';
else if (e.target.parentNode.tagName.toUpperCase() === 'A')
e.target.parentNode.rel = 'noreferrer';
}, false);
initPool();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment