Greasemonkey, overriding website functions

后端 未结 1 441
独厮守ぢ
独厮守ぢ 2020-12-15 01:35

i\'ve been reading a lot and have been trying to get this done for about 5 hours now... so here it is

I want to write a script that will override a function dummy()

相关标签:
1条回答
  • 2020-12-15 01:59

    This happens because the script is running in GM scope.
    If you don't use any GM function (like GM_setValue or GM_xmlhttpRequest), I recommend you to do the following:

    var script = document.createElement('script'); 
    script.type = "text/javascript"; 
    script.innerHTML = (<><![CDATA[
    
    // YOUR CODE GOES HERE
    
    ]]></>).toString();
    document.getElementsByTagName('head')[0].appendChild(script);
    

    Write the code as a normal script, not a GM script.
    I mean, remove all unsafeWindow references and related stuff.
    This will make the script to run in the correct scope.

    BUT if you use GM functions, then you will need to add unsafeWindow before every variable in normal scope (like $) or do something like the following and pray to make it work:

    $ = unsafeWindow.$;
    //...
    

    PS.: Multiline string with E4X is not supported anymore. Some other options are:
    1) add your code into a function and then use Function.prototype.toString
    2) create your code as a separate file and then add it as a resource
    3) add a backslash at the end of each line

    0 讨论(0)
提交回复
热议问题