Skip to content

Instantly share code, notes, and snippets.

@nk9
Created December 29, 2022 19:30
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 nk9/424d312bc9da4fe0b10482a912ed4079 to your computer and use it in GitHub Desktop.
Save nk9/424d312bc9da4fe0b10482a912ed4079 to your computer and use it in GitHub Desktop.
User script for scraping GitHub results as URLs
// ==UserScript==
// @name Copy GitHub results
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://github.com/search*
// @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant none
// ==/UserScript==
var URLs = "";
(function() {
'use strict';
// Select the node that will be observed for mutations
//const targetNode = document.getElementById('code_search_results');
const mainElements = document.getElementsByClassName('page-responsive');
const targetNode = mainElements[0];
// Callback function to execute when mutations are observed
const callback = (mutationList, observer) => {
console.log("results list mutated");
copyFoundFileURLs();
};
// Create an observer instance linked to the callback function
const observer = new MutationObserver(callback);
// Options for the observer (which mutations to observe)
const config = { attributes: true, attributeList: ["data-hydro-click"], childList: true, subtree: true };
// Start observing the target node for configured mutations
observer.observe(targetNode, config);
// Later, you can stop observing
//observer.disconnect();
// Do it once on load
copyFoundFileURLs();
})();
function copyFoundFileURLs() {
var results = document.getElementsByClassName("full-path");
var paths = "";
for (var result of results) {
var href = result.children[1].attributes.href.textContent
paths += `https://raw.githubusercontent.com${href}\n`;
}
if (URLs !== paths) {
console.log(`Sent to clipboard: ${paths}`);
navigator.clipboard.writeText(paths);
URLs = paths;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment