HackerNews Post Hider (based on post Domains and Titles)

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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()