Greasy Fork

kbin hide posts after voting

Removes posts that you've voted on from your feed

目前为 2024-03-05 提交的版本。查看 最新版本

// ==UserScript==
// @name          kbin hide posts after voting
// @description   Removes posts that you've voted on from your feed
// @author        ShaunaTheDead86
// @namespace     http://tampermonkey.net/
// @license       MIT
// @version       0.8
// @match         https://kbin.social/*
// @match         https://fedia.io/*
// @run-at        document-idle
// ==/UserScript==

updateSettings();
updatePosts();
attachObserver();

function updateSettings() {
	try {
		const allSettings = [
			{ text: "Hide Upvoted", name: "hide-upvoted" },
			{ text: "Hide Downvoted", name: "hide-downvoted" },
		];

		allSettings.forEach((setting) => (getSettingCookie(setting.name) === null ? setSetting(setting.name, true) : null));

		const settingsList = Array.from(document.getElementById("settings").children).find((e) => e.className === "settings-list");

		const settingLabel = document.createElement("strong");
		settingLabel.innerText = "Hide posts after voting";
		settingsList.appendChild(settingLabel);

		allSettings.forEach((setting) => {
			const row = document.createElement("div");
			row.className = "row";

			const span = document.createElement("span");
			span.innerText = setting.text;

			const buttonContainer = document.createElement("div");
			const settingActive = getSetting(setting.name) === "true";
			buttonContainer.appendChild(createSettingButton("yes", setting.name, settingActive));
			buttonContainer.append(" | ");
			buttonContainer.appendChild(createSettingButton("no", setting.name, !settingActive));

			row.appendChild(span);
			row.appendChild(buttonContainer);

			settingsList.appendChild(row);
		});
	} catch (err) {
		console.log("ERROR: function updateSettings: ", err);
	}
}

function updatePosts() {
	try {
		const articles = Array.from(document.getElementsByTagName("article")).filter((e) => e.id.includes("entry-"));
		articles.forEach((article) => {
			const articleChildren = Array.from(article.children);
			const voteContainerChildren = Array.from(articleChildren.find((e) => e.className === "vote").children);
			const activeButton = voteContainerChildren.find((e) => e.className.includes("active"));
			if (!activeButton) return;

      const isUpvote = activeButton.className.includes("vote__up");
      const activeSetting = getSetting(isUpvote ? "hide-upvoted" : "hide-downvoted") === "true";
			const displayString = activeSetting ? "none" : "grid";
			article.style.display = displayString;
			const sibling = article.nextElementSibling;
			if (sibling.className === "js-container") sibling.style.display = displayString;
		});
	} catch (err) {
		console.log("ERROR: function updatePosts: ", err);
	}
}

function attachObserver() {
	try {
		const target = document.querySelector("#content").children[0];
		const contentObserver = new MutationObserver(updatePosts);
		contentObserver.observe(target, { childList: true, subtree: true });
		if (!window.location.toString().includes("https://kbin.social")) contentObserver.disconnect();
	} catch (err) {
		console.log("ERROR: function attachObserver: ", err);
	}
}

function createSettingButton(option, name, active) {
	try {
		const btn = document.createElement("a");
		btn.innerText = option === "yes" ? "Yes" : "No";
		btn.className = ["kes-setting-" + option, "link-muted", active ? "active" : ""].join(" ");
		btn.dataset.setting = name;
		btn.style.cursor = "pointer";
		btn.addEventListener("click", () => {
			setSetting(name, option === "yes" ? true : false);
			Array.from(btn.parentElement.children).forEach((e) => e.classList.remove("active"));
			btn.classList.add("active");
			updatePosts();
		});
		return btn;
	} catch (err) {
		console.log("ERROR: function createSettingButton: ", err);
	}
}

function getSettingCookie(setting) {
	return localStorage.getItem("setting-" + setting);
}

function getSetting(setting) {
	const value = localStorage.getItem("setting-" + setting);
	return value ? value : "true";
}

function setSetting(name, value) {
	localStorage.setItem("setting-" + name, value);
}