如何用JS脚本监控页面变化只要一出现font-family属性就在该属性值前添加SL-Simplified字体
可以通过使用MutationObserver来监控页面变化,当元素的属性发生变化时,可以通过修改其属性值来实现添加SL-Simplified字体。
示例代码如下:
// 监控body元素的子节点变化
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
// 检查是否有font-family属性被添加或修改
if (mutation.type === 'attributes' && mutation.attributeName === 'style') {
const target = mutation.target;
const style = target.getAttribute('style');
if (style.includes('font-family')) {
// 在font-family属性值前添加SL-Simplified字体
target.setAttribute('style', style.replace(/font-family\s*:\s*([^;]+);/, 'font-family: "SL-Simplified", $1;'));
}
}
});
});
// 开始监控
observer.observe(document.body, { attributes: true, childList: true, subtree: true });
``
原文地址: https://www.cveoy.top/t/topic/e8y9 著作权归作者所有。请勿转载和采集!