HackerNews Post Hider (based on post Domains and Titles)

Simply hides posts with specific user-specified domains or title keywords on HackerNews front page

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

You will need to install an extension such as Tampermonkey to install this script.

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name     HackerNews Post Hider (based on post Domains and Titles)
// @match https://news.ycombinator.com/
// @match https://news.ycombinator.com/?p=*
// @match https://news.ycombinator.com/front*
// @version  2.2
// @grant    none
// @namespace ahappyviking
// @license MIT
// @description Simply hides posts with specific user-specified domains or title keywords on HackerNews front page
// @icon https://www.kindpng.com/picc/m/61-613142_see-no-evil-monkey-icon-monkey-eyes-closed.png
// ==/UserScript==

let hiddenDomains = ["dailymail.co.uk", "cbsnews.com"]
let hiddenTitleKeywords = ["chatgpt", "gpt", "llm", "ai-", "claude", "ai ", " agi "] //These should stay lowercase...

function hncleaner_main() {

    let numberOfBlocked = 0

    //First blocking based on domains...
    let posts = document.getElementsByClassName("athing")
    for (let post of posts) {
        let hasBeenHidden = false;

        //Check the title first...
        let titleHolder = post.querySelector(".titleline")
        const title = titleHolder.firstChild.textContent.toLocaleLowerCase()
        for (const word of hiddenTitleKeywords) {
            if (!title.includes(word)) continue;
            hidepost_hidePost(post)
            numberOfBlocked++
            hasBeenHidden = true
            break
        }

        if (hasBeenHidden) continue
        let link = post.querySelector(".sitestr")
        if (link && hiddenDomains.includes(link.innerHTML)) {
            hidepost_hidePost(post)
            numberOfBlocked++
        }
    }
    if (numberOfBlocked) {
        hncleaner_addBlockCount(numberOfBlocked);
    }
}

function hncleaner_createBlockNotice() {
    //Actually decided to have this return null just to make things cleaner
    const notice = document.createElement("div");
    return notice
}

function hidepost_hidePost(owner) {
    owner.nextElementSibling?.nextElementSibling?.remove() //Removing "spacer" element
    owner.nextElementSibling?.remove() //Removing comments
    owner.replaceWith(hncleaner_createBlockNotice()) //Removing title
}

function hncleaner_addBlockCount(blockCount) {
    const text = document.createElement("p")
    text.style.margin = "0 0 8 0"
    text.style.userSelect = "none"
    text.innerText = `Hidden posts: ${blockCount}`
    text.style.color = "grey"
    text.style.fontSize = "10px"
    document.getElementById("bigbox")?.before(text)
}

hncleaner_main()