Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrichardsz/61045d2c347db6a342a4f3ecb5eb7db1 to your computer and use it in GitHub Desktop.
Save jrichardsz/61045d2c347db6a342a4f3ecb5eb7db1 to your computer and use it in GitHub Desktop.
tampermokey google image
// ==UserScript==
// @name ReplaceURLs(GoogleImages)
// @namespace http://ashiato1.blog62.fc2.com/
// @description Replace URLs with orig image
// @author tomato111
// @version 0.0.8
// @downloadURL https://gist.github.com/tomato111/681d634ded1a9bd3107e2baf60460a35/raw/ReplaceURLs(GoogleImages).user.js
// @updateURL https://gist.github.com/tomato111/681d634ded1a9bd3107e2baf60460a35/raw/ReplaceURLs(GoogleImages).user.js
// @include https://www.google.*/search?*tbm=isch*
// @include https://www.google.*/search?*tbs=sbi*
// @run-at document-start
// @grant none
// @license ISC
// ==/UserScript==
(function () {
function replace_url(nodesSnapshot) {
const image_url_RE = /\?imgurl=(.+?)&/i;
for (let i = 0; i < nodesSnapshot.snapshotLength; i++) {
const a = nodesSnapshot.snapshotItem(i);
const href = a.getAttribute('href');
if (image_url_RE.test(href)) {
let image_url = decodeURIComponent(RegExp.$1);
image_url = image_url.replace('-origin.fc2.', '.fc2.');
a.setAttribute('href', image_url);
a.onmousedown = function () { /*nop*/ };
}
}
}
function replace_on_mouseup(nodesSnapshot) {
for (let i = 0; i < nodesSnapshot.snapshotLength; i++) {
const a = nodesSnapshot.snapshotItem(i);
a.addEventListener("mouseup", function (event) {
//nodesSnapshot-like
replace_url({ snapshotLength: 1, snapshotItem: () => event.currentTarget });
}, { once: true, capture: true });
}
}
document.addEventListener('DOMContentLoaded', function () {
let xpath, obs_target, replace;
if (document.URL.includes('tbm=isch')) { // *tbm=isch*
xpath = '//a[contains(@class, "islib")]';
obs_target = document.querySelector('div.islrc');
replace = replace_on_mouseup;
}
else { // *tbs=sbi*
xpath = '//a[contains(@href, "imgurl=")]';
obs_target = document.querySelector('div#rso');
replace = replace_url;
}
const nodesSnapshot = document.evaluate(xpath, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
if (nodesSnapshot.snapshotLength) {
replace(nodesSnapshot);
}
const observer = new MutationObserver(function (MutationRecords) {
MutationRecords.forEach(function (Record) {
Record.addedNodes.forEach(function (node) {
if (node.nodeType === node.ELEMENT_NODE && node.tagName === "DIV") {
const nodesSnapshot = document.evaluate('.' + xpath, node, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); //.== self::node()
replace(nodesSnapshot);
}
});
});
});
observer.observe(obs_target, {
childList: true
});
}, false);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment