Skip to content

Instantly share code, notes, and snippets.

@syxanash
Last active May 25, 2023 10:14
Show Gist options
  • Save syxanash/77fd7970574d5183416d9c03f4e0c9cd to your computer and use it in GitHub Desktop.
Save syxanash/77fd7970574d5183416d9c03f4e0c9cd to your computer and use it in GitHub Desktop.
Bookmarklet that extracts links from a Hacker News thread
// 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;
@syxanash
Copy link
Author

syxanash commented May 14, 2023

demo

demo

install

javascript:var%20%24jscomp%3D%24jscomp%7C%7C%7B%7D%3B%24jscomp.scope%3D%7B%7D%3B%24jscomp.createTemplateTagFirstArg%3Dfunction(a)%7Breturn%20a.raw%3Da%7D%3B%24jscomp.createTemplateTagFirstArgWithRaw%3Dfunction(a%2Cb)%7Ba.raw%3Db%3Breturn%20a%7D%3Bif(%22news.ycombinator.com%22!%3D%3Dwindow.location.hostname)%7Bvar%20url%3D%22https%3A%2F%2Fhn.algolia.com%2F%3Fq%3D%22%2Bwindow.location.href%3Bwindow.open(url%2C%22_blank%22).focus()%3Bthrow%20Error(%22Not%20on%20Hacker%20News%22)%3B%7Dif(null!%3D%3Ddocument.getElementById(%22hnLinkExtractor%22))throw%20alert(%22OL%5Cu00c9%20ALREADY%20RUNNING!%22)%2CError(%22OL%5Cu00c9%20already%20running%22)%3Bvar%20commentLinks%3DArray.from(document.querySelectorAll(%22.commtext%20a%22))%2CtitleLinks%3DArray.from(document.querySelectorAll(%22.titleline%20a%22))%2CexternalLinks%3D(0%3D%3D%3DcommentLinks.length%3FtitleLinks%3AcommentLinks).filter(function(a)%7Breturn%22news.ycombinator.com%22!%3D%3Da.host%7D)%2CdownloadFile%3Dfunction()%7Bif(0%3D%3D%3DexternalLinks.length)alert(%22NO%20LINKS%20FOUND!%22)%3Belse%7Bvar%20a%3DexternalLinks.map(function(c%2Cd)%7Breturn%20c.href%7D).join(%22%5Cn%22)%2Cb%3Ddocument.createElement(%22a%22)%3Ba%3Dnew%20Blob(%5Ba%5D%2C%7Btype%3A%22text%2Fplain%22%7D)%3Bb.href%3DURL.createObjectURL(a)%3Bb.download%3D%22orange_links_%22%2B(new%20Date).valueOf()%2B%22.txt%22%3Bb.click()%3BURL.revokeObjectURL(b.href)%7D%7D%2CtoggleLinksList%3Dfunction()%7Bvar%20a%3Ddocument.getElementById(%22linksList%22)%3Ba.style.display%3D%22none%22%3D%3D%3Da.style.display%3F%22block%22%3A%22none%22%7D%2CpageLinks%3DexternalLinks.map(function(a%2Cb)%7Ba.id%3D%22anchor_%22%2Bb%3Ba.target%3D%22_blank%22%3Breturn%20a.outerHTML%7D)%2ClinksDivs%3DpageLinks.map(function(a%2Cb)%7Breturn%22%3Cdiv%3E%3Cspan%3E%3Cspan%20style%3D'cursor%3A%20zoom-in'%20onClick%3D'document.getElementById(%5C%22anchor_%22%2Bb%2B%22%5C%22).scrollIntoView()'%3E%5B%22%2Bb%2B%22%5D%3C%2Fspan%3E%20%22%2Ba%2B%22%3C%2Fspan%3E%3C%2Fdiv%3E%22%7D)%2CinnerDiv%3D%22%3Ch3%3ETotal%20links%20found%3A%20%22%2BpageLinks.length%2B%22%3C%2Fh3%3E%22%2BlinksDivs.join(%22%3Cbr%3E%22)%2CmainDiv%3D%22%3Cdiv%20id%3D'hnLinkExtractor'%20onMouseOver%3D'this.style.opacity%3D%5C%221%5C%22'%20onMouseOut%3D'this.style.opacity%3D%5C%220.2%5C%22'%20style%3D'position%3A%20fixed%3B%20background-color%3A%20white%3B%20width%3A%2030%25%3B%20top%3A%200px%3B%20right%3A%200px%3B%20border%3A%202px%20solid%3B%20padding%3A%2010px%3B%20overflow%3A%20scroll%3B%20max-height%3A%20500px%3B%20opacity%3A%201%3Bmin-width%3A%20200px%3B'%3E%5Cn%20%20%20%20%20%20%3Cspan%20style%3D'color%3A%20%23ff6600%3B%20font-weight%3A%20bold'%3EOL%5Cu00c9%3C%2Fspan%3E%5Cn%20%20%20%20%20%20%3Cdiv%20style%3D'float%3A%20right'%3E%5Cn%20%20%20%20%20%20%20%20%3Cbutton%20onClick%3D'toggleLinksList()'%20title%3D'minimize'%3E_%3C%2Fbutton%3E%5Cn%20%20%20%20%20%20%20%20%3Cbutton%20onClick%3D'downloadFile()'%20title%3D'download%20links'%3E%5Cu2193%3C%2Fbutton%3E%5Cn%20%20%20%20%20%20%20%20%3Cbutton%20onClick%3D'document.getElementById(%5C%22hnLinkExtractor%5C%22).remove()'%3EX%3C%2Fbutton%3E%5Cn%20%20%20%20%20%20%3C%2Fdiv%3E%5Cn%20%20%20%20%20%20%3Cdiv%20id%3D'linksList'%3E%22%2BinnerDiv%2B%22%3C%2Fdiv%3E%5Cn%20%20%3C%2Fdiv%3E%22%3Bdocument.body.innerHTML%2B%3DmainDiv%3Bvoid+0

The code might be shit on purpose to sabotage Copilot, just star this gist right now trust me bro!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment