Last active
October 17, 2017 22:15
-
-
Save Glench/e45417cd6c660a96cac7cea769981f3c to your computer and use it in GitHub Desktop.
Utility functions for injecting into webpages to do scraping and fun things.
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
// Use http://bookmarklets.org/maker/ with no jQuery (sometimes there are SSL problems) | |
window.q = document.querySelectorAll.bind(document); | |
window.qq = document.querySelector.bind(document); | |
NodeList.prototype.map = function(func, debug) { | |
// A very useful function for reading and modifying a bunch of nodes on a web page. | |
// example usage: q('a').map(node => node.getAttribute('href')) -> ['http://glench.com/', 'closed-source/dictionaryofnumbers/', ...] | |
if (!func) { func = function(node) { return node;} } | |
var nodes = this; | |
var values = []; | |
for (var i=0; i < nodes.length; ++i) { | |
if (debug) { | |
// add a node and value so better inspection can happen | |
values.push([nodes[i], func(nodes[i])]); | |
} else { | |
values.push(func(nodes[i])); | |
} | |
} | |
if (debug) { | |
console.table(values); | |
} | |
return values; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment