Created
February 17, 2020 14:17
-
-
Save adamlopresto/113544fa2596006726242387c9dd1df3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(function(){ | |
var sel = window.getSelection().toString(); | |
if (sel != "") { | |
window.open("https://www.google.com/search?q="+sel, '_blank'); | |
return; | |
} | |
// Search through elements in document order | |
var links = [].concat( | |
Array.prototype.slice.call(document.getElementsByTagName("LINK")), | |
Array.prototype.slice.call(document.getElementsByTagName("A")) | |
); | |
// Determine the destinations of the rels we care about | |
for(var i in links){ | |
if(!links[i].rel || !links[i].href) continue; | |
if(links[i].rel.toLowerCase() == "next"){ | |
if(links[i].tagName == "A"){ | |
links[i].click(); | |
} else { | |
window.location = links[i].href; | |
} | |
return; | |
} | |
} | |
// Flip the links to now search bottom-up | |
links = links.reverse(); | |
var regexes = []; | |
regexes.push(/\bnext\b/i); | |
regexes.push(/→/i); | |
regexes.push(/»/i); | |
// Attempt to match links based on text or class name (first match wins) | |
for(var r in regexes){ | |
for(var i in links){ | |
var text = links[i].innerText.trim(); | |
var className = links[i].className.trim(); | |
if(links[i].href && | |
(text.match(regexes[r]) || className.match(regexes[r]))){ | |
links[i].click(); | |
return; | |
} | |
} | |
}})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment