HNCleaner

Remove annoying HN posts

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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) + "."
	}


})();