Hacker News Mark as Read

Mark things on Hacker News as read

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Hacker News Mark as Read
// @namespace    http://tampermonkey.net/
// @version      1.4
// @description  Mark things on Hacker News as read
// @license      GPL v3.0
// @author       xdpirate
// @match        https://news.ycombinator.com/
// @match        https://news.ycombinator.com/?p*
// @match        https://news.ycombinator.com/news*
// @match        https://news.ycombinator.com/best*
// @match        https://news.ycombinator.com/newest*
// @match        https://news.ycombinator.com/front*
// @match        https://news.ycombinator.com/ask*
// @match        https://news.ycombinator.com/show*
// @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("td.title > span.titleline > a");
    let titleContainer = things[i].querySelector("td.title > span.titleline");
    let commentLink = things[i].nextSibling.querySelector("td.subtext > span.subline > 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);
        }
    }

}