这里演示了一个油猴插件脚本,用于在网页读取但是加载之前,修改网页内容。当然也演示了在文档加载之后如何修改页面节点。
示例内容是关闭反Adblock的提示
(function() {
'use strict';
var observer = new MutationObserver(function(mutationsList) {
for (var mutation of mutationsList) {
if (mutation.type === 'childList') {
for (var addedNode of mutation.addedNodes) {
if (addedNode.tagName === 'SCRIPT') {
if (addedNode.src.indexOf('adsninja_client.js') > 0
|| addedNode.src.indexOf('advertisement.js') > 0
|| addedNode.src.indexOf('adblock-checker.js') > 0
) {
addedNode.src = "";
} else if (addedNode.textContent.indexOf('typeof(aad)==') >= 0) {
addedNode.textContent = '';
} else {
addedNode.textContent = addedNode.textContent.replace("r9aeadS();", "");
addedNode.textContent = addedNode.textContent.replace("setTimeout(checker, 1000);", "");
addedNode.textContent = addedNode.textContent.replace("document.getElementById('google_esf')", "document.body");
}
}
}
}
}
});
observer.observe(document, { childList: true, subtree: true });
document.addEventListener("DOMContentLoaded", function () {
const node = document.createElement('div');
node.id = 'google_esf';
node.style.display = 'none';
document.body.appendChild(node);
});
})();