在浏览器端编辑javascript

Edit javascript on browser side

本文关键字:javascript 编辑 浏览器      更新时间:2023-09-26

出于集成目的,我使用其他提供商的javascript文件。我把它添加到我的网站在下一个方式

    var other = document.createElement('script');
    other.type = 'text/javascript'; 
    other.async = true;
    other.src = SOME_URL_ON_OTHER_DOMAIN;
    (document.getElementsByTagName('body')[0]).appendChild(other);

现在我想更改它的内容。我可以在浏览器端自己做这件事而不改变javascript提供者端吗?如果不是跨浏览器的解决方案,那就好了。

您必须监视并等待加载远程文件。加载后,您可以重新定义函数。例如,这样的东西应该让你开始:

redefineRemoteFunctions = setInterval( function() {
    if ( typeof window["remoteFunction"] == "function" ) {
        // looks like the remote library has been loaded, we can now redefine the functions
        window["remoteFunction"] = function( ) {
            // do whatever you want here
        };
        clearInterval(redefineRemoteFunctions);
    }
}, 100 );