Last active
May 25, 2023 10:14
-
-
Save syxanash/77fd7970574d5183416d9c03f4e0c9cd to your computer and use it in GitHub Desktop.
Bookmarklet that extracts links from a Hacker News thread
This file contains 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
// OLÉ - Orange Link Extractor | |
// bookmarklet that extracts links from a Hacker News thread | |
// https://www.yourjs.com/bookmarklet/ | |
if (window.location.hostname !== "news.ycombinator.com") { | |
const url = `https://hn.algolia.com/?q=${window.location.href}`; | |
window.open(url, '_blank').focus(); | |
throw new Error("Not on Hacker News"); | |
} | |
if (document.getElementById("hnLinkExtractor") !== null) { | |
alert("OLÉ ALREADY RUNNING!"); | |
throw new Error("OLÉ already running"); | |
} | |
const commentLinks = Array.from(document.querySelectorAll(".commtext a")); | |
const titleLinks = Array.from(document.querySelectorAll(".titleline a")); | |
const externalLinks = ( | |
commentLinks.length === 0 ? titleLinks : commentLinks | |
).filter((link) => link.host !== "news.ycombinator.com"); | |
const downloadFile = () => { | |
if (externalLinks.length === 0) { | |
alert("NO LINKS FOUND!"); | |
return; | |
} | |
const content = externalLinks | |
.map((link, index) => { | |
return link.href; | |
}) | |
.join("\n"); | |
const link = document.createElement("a"); | |
const file = new Blob([content], { | |
type: "text/plain" | |
}); | |
link.href = URL.createObjectURL(file); | |
link.download = `orange_links_${new Date().valueOf()}.txt`; | |
link.click(); | |
URL.revokeObjectURL(link.href); | |
}; | |
const toggleLinksList = () => { | |
const div = document.getElementById("linksList"); | |
if (div.style.display === "none") { | |
div.style.display = "block"; | |
} else { | |
div.style.display = "none"; | |
} | |
} | |
const pageLinks = externalLinks.map((link, index) => { | |
link.id = `anchor_${index}`; | |
link.target = "_blank"; | |
return link.outerHTML; | |
}); | |
const linksDivs = pageLinks.map( | |
(link, index) => | |
`<div><span><span style='cursor: zoom-in' onClick='document.getElementById("anchor_${index}").scrollIntoView()'>[${index}]</span> ${link}</span></div>` | |
); | |
const innerDiv = `<h3>Total links found: ${ | |
pageLinks.length | |
}</h3>${linksDivs.join("<br>")}`; | |
const mainDiv = `<div id='hnLinkExtractor' onMouseOver='this.style.opacity="1"' onMouseOut='this.style.opacity="0.2"' style='position: fixed; background-color: white; width: 30%; top: 0px; right: 0px; border: 2px solid; padding: 10px; overflow: scroll; max-height: 500px; opacity: 1;min-width: 200px;'> | |
<span style='color: #ff6600; font-weight: bold'>OLÉ</span> | |
<div style='float: right'> | |
<button onClick='toggleLinksList()' title='minimize'>_</button> | |
<button onClick='downloadFile()' title='download links'>↓</button> | |
<button onClick='document.getElementById("hnLinkExtractor").remove()'>X</button> | |
</div> | |
<div id='linksList'>${innerDiv}</div> | |
</div>`; | |
document.body.innerHTML += mainDiv; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
demo
install
The code might be shit on purpose to sabotage Copilot, just star this gist right now trust me bro!