I want to stop a javascript .js to load on a specific website with greasemonkey/violentmonkey scrpit
https://cdn.jsdelivr.net/gh/s9e/hljs-loader@1.0.16/loader.min.js
This is the script that loads on pagesource:
<script async="" crossorigin="anonymous" data-hljs-style="github-gist" integrity="sha384-TB2DTH77ndX7xwCHAtxD7BZqyn4r429ZSoGL7vcrb5x0bFSvLGAMoiriMUcIqeTu" onload="hljsLoader.highlightBlocks(this.parentNode)" src="https://cdn.jsdelivr.net/gh/s9e/hljs-loader@1.0.16/loader.min.js"></script>
This is the url I want to stop loading on a webpage because it highlights code with syntax highlihgting, I want to stop syntax highlight.
If I block it on browser using ublock origin it works but I want to block it with userscript.
Edit: I used the script as suggested but it is not working, what could I be doing wrong?
// ==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/hljs-loader@1.0.16/loader.min.js') {
addedNode.remove();
observer.disconnect();
}
});
});
});
observer.observe(document.documentElement, { childList: true, subtree: true });
Am I missing something?
Here is the url That I'm trying to run script on https://discuss.flarum.org/d/25739-disable-syntax-highlighting
The second method below usually works but something seems to be interfering with it on your page for some reason. An ugly workaround is to put an empty hljs property on the window in advance, so that the page script, when run, thinks it already exists and does nothing:
window.hljsLoader = {};
You can add a MutationObserver to the document at the beginning of pageload and watch for the addition of a script tag which has that URL as a src, and remove it. Make sure you're using @run-at document-start
, and then do:
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/hljs-loader@1.0.16/loader.min.js') {
addedNode.remove();
observer.disconnect();
}
});
});
});
observer.observe(document.documentElement, { childList: true, subtree: true });
Although the script tag will get temporarily added to the DOM, it will be removed in the observer microtask before it gets a chance to run.
Live snippet of the script working:
<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/hljs-loader@1.0.16/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/hljs-loader@1.0.16/loader.min.js"></script>
If there are multiple scripts, you'll have to loop through them rather than stop on the first:
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/hljs-loader@1.0.16/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/hljs-loader@1.0.16/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/hljs-loader@1.0.16/loader.min.js"></script>