// ==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.4 // @match https://kbin.social/* // @run-at document-idle // @downloadURL none // ==/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; if (activeButton.className.includes("vote__up")) article.style.display = getSetting("hide-upvoted") === "true" ? "none" : "grid"; else article.style.display = getSetting("hide-downvoted") === "true" ? "none" : "grid"; }); } 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 }); 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.addEventListener("click", () => { setSetting(name, option === "yes" ? true : false); Array.from(btn.parentElement.children).forEach((e) => e.classList.remove("active")); btn.classList.add("active"); btn.href = "#0"; 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); }