1

I'm trying to use tampermonkey to delete html elements from a list on a web page, but after ~0.5s, the remaining elements get half-overwritten -

Originally: [item 1 links to item 1, item 2 links to item 2, item 3 links to item 3]

For 0.5 seconds after my script everything is perfect: [item 2 links to item 2, item 3 links to item 3]

Then the elements get overwritten (even though the hover displays correctly): [item 2 links to item 1, item 3 links to item 2]

I'm assuming this is due to some script that is running in the background, but even if I try to remove all the scripts via:

// @run-at document-start: var scripts = document.getElementsByTagName('script'); for (var k = 0; k < scripts.length; k++) { scripts[k].setAttribute('src', 'asdf'); scripts[k].innerText = ""; scripts[k].innerHTML = ""; } 

There is still some sort of script running in the background that is overriding the default link... Is my script override buggy/not enough? Or is there a better approach to solving this?

2
  • Are you trying to ensure that no <script> tags on the target page run? Commented Mar 28, 2020 at 7:40
  • As I mentioned - it's optional. I believe I have the following options: 1. prevent scripts from running after mine does 2. prevent scripts from modifying the elements I modify 3. pray removing all scripts will still load the content I want Commented Mar 28, 2020 at 7:47

1 Answer 1

3

If you run your userscript at document-start, the page may not have loaded yet - there will be no tags to remove.

If you run your userscript normally, the page will have already loaded - script tags will have likely already run.

If you want to make sure the page doesn't run any of its own scripts, attach a MutationObserver to the entire document at the beginning of pageload, and when a script tag is added, remove it. The microtask during which a MutationObserver runs will be before the script contents will run:

<script> // Example userscript code const observer = new MutationObserver((mutations) => { for (const { addedNodes} of mutations) { for (addedNode of addedNodes) { if (addedNode.nodeType === 1 && addedNode.tagName === 'SCRIPT') { console.log('script removed'); addedNode.remove(); } } } }); observer.observe(document.documentElement, { childList: true, subtree: true }); </script> <script> console.log('Example page script running'); </script> <div> Content <script> console.log('Example nested page script running'); </script> </div>

Make sure that your userscript runs with instant script injection and document-start, to ensure that it runs before anything on the page exists. (If the page is able to get a script in before your userscript runs, it can do anything it wants)

Sign up to request clarification or add additional context in comments.

2 Comments

Worked like a charm! I had to separate my scripts into 2 with this method - the document-start to strip off the javascript, and a document-end to modify what I want :)
You can use a single script by putting the rest into a DOMContentLoaded listener, if you like

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.