HNCleaner

Remove annoying HN posts

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         HNCleaner
// @namespace    LWPDWyfub
// @version      0.2
// @description  Remove annoying HN posts
// @author       LWPDWyfub
// @match https://news.ycombinator.com/*
// @exclude https://news.ycombinator.com/item*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=ycombinator.com
// @license MIT
// ==/UserScript==

(function () {
	'use strict';
	const annoyingTerms = [
		'ChatGPT',
		'Musk',
		'Elon',
		'spacex',
		'tesla'
	];
	const annoyingSites = ['twitter.com'];
	const annoyingTermsRegex = new RegExp('(' + annoyingTerms.join('|') + ')', 'i')
	const annoyingSitesRegex = new RegExp('(' + annoyingSites.join('|') + ')', 'i')
	const termTargets = Array.from(document.querySelectorAll('.athing')).filter(({ innerText }) => annoyingTermsRegex.test(innerText) || annoyingSitesRegex.test(innerText))
	const logoutButton = document.querySelector("#logout")
	const hidePost = function (postIds, next) {
		var postId = postIds[next].id
		console.log("Hiding post[" + next + "] with ID " + postId)
		var req = XMLHttpRequest({
			method: 'GET',
			url: "https://news.ycombinator.com/hide?id=" + postId,
			withCredentials: true,
			onerror: function (error) {
				console.log("Error hiding " + postId)
			},
			onload: function (response) {
				var resp = response.responseText;
				console.log("Hiding post " + postId + " returned code " + response.status)
				if (resp.includes("Logged") || resp.status != 200) {
					console.log(resp)
				}
				else {
					console.log("Hid post " + postId)
				}

			}
		});
		next = next + 1
		if (next < postIds.length) {
			window.setTimeout(hidePost, 5000, termTargets, next)
		}
		else {
			console.log("Finished hiding final post")
		}
	}

	console.log("Found " + termTargets.length + " annoying targets")
	for (let target of termTargets) {
		console.log("Hiding post " + target.id)
		target.style.display = "none"
	}
	// Clean up spacers and comments
	Array.from(document.querySelectorAll("tr[style*='none'] + tr")).forEach(function (e) { e.style.display = "none" })
	Array.from(document.querySelectorAll("tr[style*='none'] + .spacer")).forEach(function (e) { e.style.display = "none" })

	// Renumber unhidden posts
	var allRanks = Array.from(document.querySelectorAll("span.rank"))
	var shownSpans = Array.from(document.querySelectorAll("span.rank")).filter(
		e => window.getComputedStyle(e.parentElement.parentElement).display != "none")
	const firstNum = parseInt(allRanks[0].innerText.replace("\.", ""))
	for (let i = 0; i < shownSpans.length; i++) {
		shownSpans[i].innerText = (firstNum + i) + "."
	}


})();