Last active
October 28, 2023 12:06
-
-
Save NuarkNoir/6a24d825fb914cdaf6d4e17979d74dfe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name HideCommentsOnPinterest | |
// @namespace http://tampermonkey.net/ | |
// @version 0.4 | |
// @description title is descriptive | |
// @author nuark | |
// @match https://pinterest.com/pin/* | |
// @match https://*.pinterest.com/pin/* | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=pinterest.com | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
const HIDDEN_MARK = "nuark-has-hidden-this-bullshit"; | |
let displayState = "none"; | |
const createButton = (text, className, callback) => { | |
const btn = document.createElement("button"); | |
btn.textContent = text; | |
btn.className = className; | |
btn.addEventListener("click", callback); | |
btn.style.height = "1.5rem"; | |
btn.style.fontSize = "0.75rem"; | |
btn.style.marginLeft = "1rem"; | |
btn.style.borderRadius = "1rem"; | |
btn.style.border = "none"; | |
return btn; | |
} | |
const findInfoAndCommentsBlock = () => { | |
try { | |
const infoBlock = document.querySelector(`[data-test-id=CloseupDetails]`) || document.querySelector(`:has(>[data-test-id="story-pin-closup-creator-card"])`); | |
if (!infoBlock) return null; | |
const commentsBlock = [...infoBlock.childNodes].slice(-1)[0]; | |
if (!commentsBlock) return null; | |
return {infoBlock, commentsBlock}; | |
} catch (e) { | |
return null; | |
} | |
} | |
const mountButtons = async () => { | |
const result = findInfoAndCommentsBlock(); | |
if (result === null) return; | |
const { infoBlock, commentsBlock } = result; | |
console.log(); | |
if (displayState === "none" && !commentsBlock.querySelector(".hider")) { | |
commentsBlock.children[0].classList.add(HIDDEN_MARK); | |
const hideBtn = commentsBlock.children[0].querySelector("button.seeker"); | |
if (hideBtn) hideBtn.remove(); | |
const unhideBtn = createButton("Показать", "hider", () => { | |
displayState = ""; | |
mountButtons(); | |
process(); | |
}); | |
commentsBlock.children[0].append(unhideBtn); | |
} else if (displayState === "" && !commentsBlock.querySelector(".seeker")) { | |
commentsBlock.children[0].classList.remove(HIDDEN_MARK); | |
const showBtn = commentsBlock.children[0].querySelector("button.hider"); | |
if (showBtn) showBtn.remove(); | |
const unhideBtn = createButton("Скрыть", "seeker", () => { | |
displayState = "none"; | |
mountButtons(); | |
process(); | |
}); | |
commentsBlock.children[0].append(unhideBtn); | |
} | |
} | |
const process = async () => { | |
const result = findInfoAndCommentsBlock(); | |
if (result === null) return; | |
const { infoBlock, commentsBlock } = result; | |
if (commentsBlock.childElementCount === 3 && commentsBlock.children[1].style.display !== displayState) { | |
commentsBlock.children[1].style.display = displayState; | |
} | |
} | |
const logInFix = () => { | |
try { | |
const collapseButton = document.querySelector(`[data-test-id="collapse-button"] svg path`); | |
if (collapseButton.getAttribute("d") === `M21.75 19.5c-.58 0-1.15-.22-1.59-.65L12 10.79l-8.16 8.06c-.88.87-2.3.87-3.18 0a2.21 2.21 0 0 1 0-3.15L12 4.5l11.34 11.2c.88.87.88 2.28 0 3.15-.44.43-1.01.65-1.59.65`) { | |
collapseButton.parentElement.parentElement.click(); | |
} | |
} catch (e) {} | |
} | |
let observer = new MutationObserver(mutationRecords => { | |
mountButtons(); | |
process(); | |
logInFix(); | |
}); | |
observer.observe(document.body, { | |
childList: true, | |
subtree: true, | |
characterDataOldValue: true, | |
}); | |
process(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment