Prevent execution of a specific inline script tag

后端 未结 2 1055
没有蜡笔的小新
没有蜡笔的小新 2021-01-18 05:58

I\'m trying to write a script for Tampermonkey that prevents the execution of a specific inline script tag. The body of the page looks something like this

&l         


        
相关标签:
2条回答
  • 2021-01-18 06:28

    Alternative version free of doc.write/XHR --

       (() => { 
              'use strict';
              let needle = '/window.location/';
              if ( needle === '' || needle === '{{1}}' ) {
                  needle = '.?';
              } else if ( needle.slice(0,1) === '/' && needle.slice(-1) === '/' ) {
                  needle = needle.slice(1,-1);
              } else {
                  needle = needle.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
              }
              needle = new RegExp(needle);
              const jsnode = () => {
                            try {
                                const jss = document.querySelectorAll('script');
                                for (const js of jss) {
                                    if (js.outerHTML.match(needle)) {
                                            js.remove();
                                    }           
                                }
                            } catch { }
              };
              const observer = new MutationObserver(jsnode);
              observer.observe(document.documentElement, { childList: true, subtree: true });
    })();
    
    0 讨论(0)
  • 2021-01-18 06:40

    Delete script tag on document-start (as suggested by wOxxOm):

    (function() {
        'use strict';
        window.stop();
        const xhr = new XMLHttpRequest();
        xhr.open('GET', window.location.href);
        xhr.onload = () => {
            var html = xhr.responseText
            .replace(/<script\b[\s\S]*?<\/script>/g, s => {
                // check if script tag should be replaced/deleted
                if (s.includes('window.location')) {
                    return '';
                } else {
                    return s;
                }
            });
            document.open();
            document.write(html);
            document.close();
        };
        xhr.send();
    })();
    
    0 讨论(0)
提交回复
热议问题