Chrome extension to modify page's script includes and JS

前端 未结 5 1763
名媛妹妹
名媛妹妹 2020-12-13 13:51

I work on a javascript library that customers include on their site to embed a UI widget. I want a way to test dev versions of the library live on the customer\'s site with

5条回答
  •  粉色の甜心
    2020-12-13 14:19

    You might be interested in the hooks available in the Opera browser. Opera used to have* very powerful hooks, available both to User JavaScript files (single-file things, very easy to write and deploy) and Extensions. Some of these are:

    BeforeExternalScript:

    This event is fired when a script element with a src attribute is encountered. You may examine the element, including its src attribute, change it, add more specific event listeners to it, or cancel its loading altogether.

    One nice trick is to cancel its loading, load the external script in an AJAX call, perform text replacement on it, and then re-inject it into the webpage as a script tag, or using eval.

    window.opera.defineMagicVariable:

    This method can be used by User JavaScripts to override global variables defined by regular scripts. Any reference to the global name being overridden will call the provided getter and setter functions.

    window.opera.defineMagicFunction:

    This method can be used by User JavaScripts to override global functions defined by regular scripts. Any invocation of the global name being overridden will call the provided implementation.


    *: Opera recently switched over to the Webkit engine, and it seems they have removed some of these hooks. You can still find Opera 12 for download on their website, though.

提交回复
热议问题