让我的脚本等待其他脚本加载 [英] Make my userscript wait for other scripts to load

查看:103
本文介绍了让我的脚本等待其他脚本加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[编辑:)我用一个简化的例子来解释原始的,令人困惑的问题,以显示问题。]

背景

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

代码

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

 < script type =text / javascript> 
函数AlertMe()
{
alert(Function AlertMe was called!);
//然后在服务器上动态生成
//的字符串,以便我不能轻易地将它写入userscript
}
< / script>

我的userscript(在Chrome中安装):

 函数tryAlert()

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

tryAlert();

问题

当我试图简单地调用该函数时,Chrome的控制台让我知道 AlertMe未定义。认为这是因为我的用户脚本在所有其他脚本加载之前运行,我使用 setTimeout 来等待 AlertMe 功能被定义。



不幸的是,如果你安装脚本然后访问页面,你会发现这只是输出 AlertMe是未定义的。 typeof AlertMe ,它会正确回应function,那么为什么我的userscript始终认为 AlertMe 未定义?

解决方案

这不是时间问题。

您遇到Greasemonkey的安全限制,这会阻止您在页面中执行功能。请参阅我对前一个问题的回答,以获得解释和一些安全解决方法: greasemonkey-calling-a-websites-javascript-functions> UserScripts& Greasemonkey:调用网站的JavaScript功能


[Edit: I'm replacing the original, confusing question with a simplified example demonstrating the problem.]

Background

I'm trying to write a userscript which will run in Chrome. This script needs to call a JavaScript function AlertMe() that is outside of the userscript -- this function is part of the page and contains variables that are generated dynamically on the server-side, so it isn't possible to re-write this function in my userscript.

Code

Script on the page (visit the page):

<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>

My userscript (install it in Chrome):

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

tryAlert();

The Problem

When I tried to simply call the function, Chrome's console let me know that AlertMe is not defined. Thinking that this was because my userscript was running before all other scripts had been loaded, I used setTimeout to wait for the AlertMe function to become defined.

Unfortunately, if you install the script then visit the page, you'll see that this just outputs AlertMe is undefined. forever and never calls the function. If you type typeof AlertMe into Chrome's console, it will correctly respond with "function", so why is it that my userscript always thinks that AlertMe is undefined?

解决方案

This is not a matter of timing.

You're bumping into Greasemonkey's security restrictions, which prevent you from executing functions in the page. Please see my answer to this previous question for an explanation and some safe workarounds:

UserScripts & Greasemonkey: calling a website's JavaScript functions

这篇关于让我的脚本等待其他脚本加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆