Home > database >  How to block url or .js file execution on a specific using greasemonkey script? (client side)
How to block url or .js file execution on a specific using greasemonkey script? (client side)

Time:08-12

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" onl oad="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

CodePudding user response:

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" onl oad="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" onl oad="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" onl oad="console.log(hljsLoader)" src="https://cdn.jsdelivr.net/gh/s9e/[email protected]/loader.min.js"></script>

  • Related