我目前正在为我的本地路由器制作一个黑暗主题,它使用的图像很糟糕,我想替换它们,但它们是用 JavaScript 添加的,经过对 Tampermonkey/Greasemonkey 和 Stylish 的大量研究,我不能找不到任何方法来做到这一点。
我唯一的想法是将文件名为 wan_up.png 的图像替换为另一个图像。
最佳答案
在 Stylish 或类似扩展中使用 CSS。
#your-selector-here {
height: 0 !important;
width: 0 !important;
/* these numbers match the new image's dimensions */
padding-left: 32px !important;
padding-top: 32px !important;
background: url(http://example.com/your/image/here) no-repeat !important;
background-size: 32px 32px; /* modern CSS property to scale the new image */
}
使用MutationObserver用于在显示图像元素之前更改图像元素的 API。
MutationObserver 有很多包装器,这里有一个基于 setMutationHandler 的示例:
// ==UserScript==//
// @name Replace image
// @match http://192.168.0.1/*
// @run-at document-start
// @require https://greasyfork.org/scripts/12228/code/setMutationHandler.js
// ==/UserScript==
setMutationHandler({
processExisting: true,
selector: 'img[src*="wan_up"]',
handler: images => images.forEach(img => {
img.src = 'http://foo/bar.png';
})
});
编辑@match中的URL,选择要替换的图像,相应的新图像URL。
关于javascript - 如何使用 Tampermonkey 或 Stylish 将名称为 x.png 的所有图像更改为 x.png?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42265241/