Created
December 29, 2022 19:30
-
-
Save nk9/424d312bc9da4fe0b10482a912ed4079 to your computer and use it in GitHub Desktop.
User script for scraping GitHub results as URLs
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
// ==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