我想停止 javascript .js 以使用 Greasemonkey/violentmonkey scrpit 加载到特定网站上
https://cdn.jsdelivr.net/gh/s9e/[email protected]/loader.min.js
这是在页面源上加载的脚本:
<script async="" crossorigin="anonymous" data-hljs-style="github-gist" integrity="sha384-TB2DTH77ndX7xwCHAtxD7BZqyn4r429ZSoGL7vcrb5x0bFSvLGAMoiriMUcIqeTu" onload="hljsLoader.highlightBlocks(this.parentNode)" src="https://cdn.jsdelivr.net/gh/s9e/[email protected]/loader.min.js"></script>
这是我想停止在网页上加载的网址,因为它用语法高亮突出显示代码,我想停止语法突出显示。
如果我使用 ublock origin 在浏览器上阻止它,它可以工作,但我想用用户脚本阻止它。
编辑: 我按照建议使用了脚本,但它不起作用,我可能做错了什么?
// ==UserScript==
// @name New script
// @namespace Violentmonkey Scripts
// @match *flarum.org*
// @include *flarum.org*
// @grant none
// @version 1.0
// @run-at document-start
// @author -
// @description 12/26/2020, 9:57:34 AM
// ==/UserScript==
const observer = new MutationObserver((mutations) => {
mutations.forEach(({ addedNodes }) => {
addedNodes.forEach((addedNode) => {
if (addedNode.nodeType === 1 && addedNode.matches('script') && addedNode.src === 'https://cdn.jsdelivr.net/gh/s9e/[email protected]/loader.min.js') {
addedNode.remove();
observer.disconnect();
}
});
});
});
observer.observe(document.documentElement, { childList: true, subtree: true });
我错过了什么吗?
这是我尝试运行脚本的网址 https://discuss.flarum.org/d/25739-disable-syntax-highlighting
下面的第二种方法通常有效,但由于某种原因,您的页面上似乎有一些东西干扰了它。一个丑陋的解决方法是提前在窗口上放置一个空的 hljs 属性,以便页面脚本在运行时认为它已经存在并且不执行任何操作:
window.hljsLoader = {};
您可以在页面加载开始时向文档添加 MutationObserver,并观察是否添加了将该 URL 作为 src 的脚本标记,然后将其删除。确保您使用的是
@run-at document-start
,然后执行以下操作:
const observer = new MutationObserver((mutations) => {
mutations.forEach(({ addedNodes }) => {
addedNodes.forEach((addedNode) => {
if (addedNode.nodeType === 1 && addedNode.matches('script') && addedNode.src === 'https://cdn.jsdelivr.net/gh/s9e/[email protected]/loader.min.js') {
addedNode.remove();
observer.disconnect();
}
});
});
});
observer.observe(document.documentElement, { childList: true, subtree: true });
虽然脚本标签会暂时添加到 DOM 中,但在观察者微任务有机会运行之前,它将被删除。
脚本运行的实时片段:
<script>
const observer = new MutationObserver((mutations) => {
mutations.forEach(({ addedNodes }) => {
[...addedNodes].forEach((addedNode) => {
if (addedNode.nodeType === 1 && addedNode.matches('script') && addedNode.src === 'https://cdn.jsdelivr.net/gh/s9e/[email protected]/loader.min.js') {
console.log('Script tag addition intercepted and removed');
addedNode.remove();
observer.disconnect();
console.log('hljsLoader has not been loaded:', typeof hljsLoader);
}
});
});
});
observer.observe(document.documentElement, { childList: true, subtree: true });
</script>
<script async="" crossorigin="anonymous" data-hljs-style="github-gist" integrity="sha384-TB2DTH77ndX7xwCHAtxD7BZqyn4r429ZSoGL7vcrb5x0bFSvLGAMoiriMUcIqeTu" onload="console.log(hljsLoader)" src="https://cdn.jsdelivr.net/gh/s9e/[email protected]/loader.min.js"></script>
如果有多个脚本,您必须循环遍历它们而不是在第一个脚本上停止:
console.log(typeof hljsLoader);
setTimeout(() => {
console.log(typeof hljsLoader);
}, 1000);
<script>
const observer = new MutationObserver((mutations) => {
mutations.forEach(({ addedNodes }) => {
[...addedNodes].forEach((addedNode) => {
if (addedNode.nodeType === 1 && addedNode.matches('script') && addedNode.src === 'https://cdn.jsdelivr.net/gh/s9e/[email protected]/loader.min.js') {
console.log('Script tag addition intercepted and removed');
addedNode.remove();
console.log(typeof hljsLoader);
}
});
});
});
observer.observe(document.documentElement, { childList: true, subtree: true });
window.addEventListener('DOMContentLoaded', () => observer.disconnect());
</script>
<script async="" crossorigin="anonymous" data-hljs-style="github-gist" integrity="sha384-TB2DTH77ndX7xwCHAtxD7BZqyn4r429ZSoGL7vcrb5x0bFSvLGAMoiriMUcIqeTu" onload="console.log(hljsLoader)" src="https://cdn.jsdelivr.net/gh/s9e/[email protected]/loader.min.js"></script>
<script async="" crossorigin="anonymous" data-hljs-style="github-gist" integrity="sha384-TB2DTH77ndX7xwCHAtxD7BZqyn4r429ZSoGL7vcrb5x0bFSvLGAMoiriMUcIqeTu" onload="console.log(hljsLoader)" src="https://cdn.jsdelivr.net/gh/s9e/[email protected]/loader.min.js"></script>
将其添加到脚本标题中可能会起作用
// @webRequest {"selector":"https://cdn.jsdelivr.net/gh/s9e/[email protected]/loader.min.js/*","action":"cancel"}