Skip to content

Instantly share code, notes, and snippets.

@Glench
Last active October 17, 2017 22:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Glench/e45417cd6c660a96cac7cea769981f3c to your computer and use it in GitHub Desktop.
Save Glench/e45417cd6c660a96cac7cea769981f3c to your computer and use it in GitHub Desktop.
Utility functions for injecting into webpages to do scraping and fun things.
// 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