Last active
November 23, 2020 16:22
-
-
Save Farow/6186e09c6addb8f75607 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 Google images direct | |
// @namespace https://github.com/Farow/userscripts | |
// @description Adds direct links | |
// @include /https://www\.google\.[a-z]{2,3}/.+?tbm=isch/ | |
// @version 1.0.1 | |
// @grant none | |
// ==/UserScript== | |
/* | |
changelog: | |
1.0.1 - 2016-04-08 | |
- updated to work with the new image rendering | |
- works on new searches | |
1.0.0 - 2016-02-19 - initial release | |
*/ | |
let observer = new MutationObserver(attr_observer); | |
try { | |
init(); | |
} | |
catch (error) { | |
console.log(error); | |
} | |
function init() { | |
let wrapper = document.getElementById('rg_s'); | |
let images = wrapper.getElementsByClassName('rg_l'); | |
for (let image of images) { | |
if (image.href) { | |
add_direct_links(image); | |
} | |
else { | |
observer.observe(image, { attributes: true, attributeFilter: ['href'] }); | |
} | |
} | |
+new MutationObserver(image_observer).observe(wrapper, { childList: true }); | |
+new MutationObserver(search_observer).observe(document.getElementById('search'), { childList: true }); | |
} | |
function add_direct_links(image) { | |
let href_hash = parse_href(image.search.slice(1)); | |
/* location to put the direct links */ | |
let location = image.getElementsByClassName('rg_ilmbg')[0]; | |
/* create direct ink */ | |
let direct_link = document.createElement('a'); | |
direct_link.href = href_hash.imgurl; | |
direct_link.rel = 'noreferrer'; | |
direct_link.textContent = location.firstElementChild.textContent; | |
/* add styles */ | |
direct_link.style.setProperty('display', 'block'); | |
direct_link.style.setProperty('color', 'white'); | |
direct_link.style.setProperty('text-decoration', 'none'); | |
/* block image expander */ | |
direct_link.addEventListener('click', block_expander); | |
/* place it */ | |
location.removeChild(location.firstElementChild); | |
location.appendChild(direct_link); | |
} | |
function attr_observer (mutations) { | |
for (let mutation of mutations) { | |
add_direct_links(mutation.target); | |
} | |
} | |
function image_observer(mutations) { | |
for (let mutation of mutations) { | |
for (let node of mutation.addedNodes) { | |
if (node.tagName != 'DIV') { | |
continue; | |
} | |
if (!node.classList.contains('rg_di')) { | |
continue; | |
} | |
let image = node.getElementsByClassName('rg_l')[0]; | |
/* some links already have href set */ | |
if (image.href) { | |
add_direct_links(image); | |
} | |
else { | |
observer.observe(image, { attributes: true, attributeFilter: ['href'] }); | |
} | |
} | |
} | |
} | |
function search_observer (mutations) { | |
for (let mutation of mutations) { | |
for (let node of mutation.addedNodes) { | |
if (node.tagName == 'DIV') { | |
init(); | |
} | |
} | |
} | |
} | |
function parse_href(href) { | |
let hash = { }; | |
for (let pair of href.split('&')) { | |
let [key, value] = pair.split('='); | |
hash[key] = decodeURIComponent(value); | |
} | |
return hash; | |
} | |
function block_expander(event) { | |
event.stopPropagation(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Google images direct 1.0.1 does not work. Firefox 77.0b9 x64, Microsoft Windows 10.0.18363.836, Tampermonkey 4.10.6112.