Mark things on Hacker News as read
目前為
// ==UserScript==
// @name Hacker News Mark as Read
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Mark things on Hacker News as read
// @license GPL v3.0
// @author xdpirate
// @match https://news.ycombinator.com/
// @match https://news.ycombinator.com/news*
// @icon https://www.google.com/s2/favicons?domain=ycombinator.com
// @grant GM_addStyle
// @grant GM_getValue
// @grant GM_setValue
// @run-at document-end
// ==/UserScript==
let readArticles = GM_getValue("readArticles", []);
GM_addStyle(`
.hnmrCheck {
margin-left: 2px;
margin-right: 2px;
cursor: pointer;
}
`);
let things = document.querySelectorAll("tr.athing");
for(let i = 0; i < things.length; i++) {
let titleLink = things[i].querySelector("a.titlelink");
let titleContainer = titleLink.parentNode;
let commentLink = things[i].nextSibling.querySelector("td.subtext > a[href^=\"item?id\"");
if(commentLink) {
let checkmark = document.createElement("span");
checkmark.classList.add("hnmrCheck");
checkmark.innerHTML = "✅";
checkmark.title = "Mark as read";
let articleIDMatch = commentLink.href.match(/item\?id\=([0-9]+)/i);
if(articleIDMatch) {
checkmark.articleID = articleIDMatch[1];
if(readArticles.includes(checkmark.articleID)) {
things[i].style.opacity = "0.3";
things[i].nextSibling.style.opacity = "0.3";
} else {
things[i].style.opacity = "1";
things[i].nextSibling.style.opacity = "1";
}
checkmark.onclick = function() {
if(readArticles.includes(this.articleID)) {
readArticles.splice(readArticles.indexOf(this.articleID), 1);
let theThing = this.closest("tr.athing");
theThing.style.opacity = "1";
theThing.nextSibling.style.opacity = "1";
} else {
readArticles.push(this.articleID);
let theThing = this.closest("tr.athing");
theThing.style.opacity = "0.3";
theThing.nextSibling.style.opacity = "0.3";
}
GM_setValue("readArticles", readArticles);
};
titleContainer.append(checkmark);
}
}
}