Reddit Hide All Posts & Reload Hotkey

Hide all posts & reload page on Ctrl+Shift+R; cancel with Escape

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         Reddit Hide All Posts & Reload Hotkey
// @namespace    redditposthideandreload_kk
// @version      1.5
// @description  Hide all posts & reload page on Ctrl+Shift+R; cancel with Escape
// @match        https://*.reddit.com/*
// @grant        none
// @run-at       document-start
// ==/UserScript==

let oldhidebtns;
let newhidebtns;

function getbtns() {
	oldhidebtns = document.getElementsByTagName("a");
	newhidebtns = document.getElementsByClassName("icon-hide");
}

let wait = ms => new Promise(resolve => setTimeout(resolve, ms));
let waitTime = 500;
let canReload = true;

async function doHide() {
	getbtns();

	try {
		for (let i = 0; i < oldhidebtns.length; i++) {
			if (!canReload) return;

			if (oldhidebtns[i].innerText.toLowerCase() === "hide") {
				oldhidebtns[i].click();
				await wait(waitTime);
			}
		}

		let didNewBtnRun = false;

		for (let i = 1; i < newhidebtns.length; i++) {
			if (!canReload) return;
			i--;

			newhidebtns[i].click();
			didNewBtnRun = true;
			await wait(waitTime);
		}

		if (didNewBtnRun) return doHide();

		await wait(waitTime + 1000);

		if (!canReload) return;

		location.reload();
	}
	catch (e) {
		console.warn(e);
		doHide();
	}
}

document.addEventListener("keydown", (e) => {
	if (e.ctrlKey && e.shiftKey && e.keyCode == 82) {
		e.preventDefault();
		canReload = true;
		doHide();
	}
	if (e.key.toLowerCase() === "escape" || e.code.toLowerCase() === "escape") {
		e.preventDefault();
		canReload = false;
	}
}, false);