2

I want to stop a javascript .js to load on a specific website with greasemonkey/violentmonkey scrpit

https://cdn.jsdelivr.net/gh/s9e/[email protected]/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/[email protected]/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/[email protected]/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

2 Answers 2

1

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/[email protected]/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/[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>

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/[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>

11
  • Sorry my bad, I have added the loaded script from pagesource, Can you please add above code with it, I don't have much knowledge of this script.
    – imHD3D
    Commented Dec 26, 2020 at 4:24
  • 1
    It should work, the src is just as you said it was, and that's all the mutation observer callback checks for Commented Dec 26, 2020 at 4:31
  • 1
    Works just fine here, see live snippet. Make sure your userscript is running on document-start, as the answer says. Commented Dec 26, 2020 at 4:46
  • 1
    Yes, that can be the whole script, nothing else is needed Commented Dec 26, 2020 at 6:55
  • 1
    @alev I think in some circumstances it's impossible to block the network call, but you should still be able to prevent the requested resource from being put onto the page with .remove() in the observer. Commented Nov 4, 2021 at 16:00
0

Adding this to your script header might work

// @webRequest   {"selector":"https://cdn.jsdelivr.net/gh/s9e/[email protected]/loader.min.js/*","action":"cancel"}

cf. How can I block specific requests in a website with a userscript?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.