相关疑难解决方法(0)

使我的用户脚本等待加载其他脚本

[ 编辑:我用一个简单的例子来代替原来的,令人困惑的问题来证明这个问题.]

背景

我正在尝试编写一个将在Chrome中运行的用户脚本.这个脚本需要调用一个AlertMe()在usercript之外的JavaScript函数- 这个函数是页面的一部分,包含在服务器端动态生成的变量,所以不能在我的函数中重写这个函数. userscript.

页面上的脚本(访问页面):

<script type="text/javascript">
    function AlertMe()
    {
        alert("Function AlertMe was called!");
        // then do stuff with strings that were dynamically generated
        // on the server so that I can't easily rewrite this into the userscript
    }
</script>
Run Code Online (Sandbox Code Playgroud)

我的用户脚本(在Chrome中安装):

function tryAlert()
{
    if (typeof AlertMe == "undefined") {
        console.log('AlertMe is undefined.');
        window.setTimeout(tryAlert, 100);
    }
    else {
        AlertMe();
    }
}

tryAlert();
Run Code Online (Sandbox Code Playgroud)

问题

当我试图简单地调用该功能时,Chrome的控制台让我知道AlertMe is not defined.认为这是因为我的用户脚本在加载所有其他脚本之前运行,我曾经setTimeout …

javascript google-chrome userscripts

7
推荐指数
2
解决办法
9869
查看次数

标签 统计

google-chrome ×1

javascript ×1

userscripts ×1