From f1a5e8a61dec3f58b6346637cd28fd1093e0a7bb Mon Sep 17 00:00:00 2001 From: js0ny Date: Thu, 22 Jan 2026 02:36:27 +0000 Subject: [PATCH] feat(zhihu_sticker_replacer) --- zhihu_sticker_replacer.js | 64 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 zhihu_sticker_replacer.js diff --git a/zhihu_sticker_replacer.js b/zhihu_sticker_replacer.js new file mode 100644 index 0000000..8908c7d --- /dev/null +++ b/zhihu_sticker_replacer.js @@ -0,0 +1,64 @@ +// ==UserScript== +// @name Zhihu Image URL Replacer +// @namespace https://tampermonkey.net/ +// @version 1.0 +// @description Replace a specific Zhihu image URL with another one +// @match https://www.zhihu.com/* +// @match https://zhuanlan.zhihu.com/* +// @grant none +// ==/UserScript== + +// 此脚本用于将知乎的「惊喜」表情替换为旧版 +// 作者与原专栏:https://zhuanlan.zhihu.com/p/1997308999595483524 + +(function () { + 'use strict'; + + const OLD_URL = 'https://pic1.zhimg.com/v2-5c9b7521eb16507c9d2f747f3a32a813.png'; + const NEW_URL = 'https://pic2.zhimg.com/v2-3846906ea3ded1fabbf1a98c891527fb.png'; + + function replaceImages(root = document) { + // Replace + const imgs = root.querySelectorAll(`img[src="${OLD_URL}"]`); + imgs.forEach(img => { + img.src = NEW_URL; + }); + + // Replace srcset (Zhihu often uses this) + const srcsetImgs = root.querySelectorAll('img[srcset]'); + srcsetImgs.forEach(img => { + if (img.srcset.includes(OLD_URL)) { + img.srcset = img.srcset.replaceAll(OLD_URL, NEW_URL); + } + }); + + // Replace background-image URLs + const elements = root.querySelectorAll('*'); + elements.forEach(el => { + const bg = getComputedStyle(el).backgroundImage; + if (bg && bg.includes(OLD_URL)) { + el.style.backgroundImage = bg.replaceAll(OLD_URL, NEW_URL); + } + }); + } + + // Initial run + replaceImages(); + + // Observe dynamically loaded content + const observer = new MutationObserver(mutations => { + for (const m of mutations) { + m.addedNodes.forEach(node => { + if (node.nodeType === Node.ELEMENT_NODE) { + replaceImages(node); + } + }); + } + }); + + observer.observe(document.body, { + childList: true, + subtree: true + }); +})(); +