javascript 让我的用户脚本等待其他脚本加载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5092726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make my userscript wait for other scripts to load
提问by Michael Martin-Smucker
[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.
我正在尝试编写一个将在 Chrome 中运行的用户脚本。此脚本需要调用用户脚本AlertMe()之外的JavaScript 函数——此函数是页面的一部分,包含在服务器端动态生成的变量,因此无法在我的脚本中重新编写此函数用户脚本。
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):
我的用户脚本(在 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 setTimeoutto wait for the AlertMefunction to become defined.
当我尝试简单地调用该函数时,Chrome 的控制台让我知道AlertMe is not defined. 认为这是因为我的用户脚本在所有其他脚本加载之前运行,我过去常常setTimeout等待AlertMe函数被定义。
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 AlertMeinto Chrome's console, it will correctly respond with "function", so why is it that my userscript always thinks that AlertMeis undefined?
不幸的是,如果您安装脚本然后访问该页面,您会看到这只是AlertMe is undefined.永远输出并且永远不会调用该函数。如果您输入typeof AlertMeChrome 的控制台,它会正确响应"function",那么为什么我的用户脚本总是认为这AlertMe是未定义的?
回答by epascarello
You can always write a little function that checks to see if the function is loaded
你总是可以写一个小函数来检查函数是否被加载
function waitForFnc(){
if(typeof absearch == "undefined"){
window.setTimeout(waitForFnc,50);
}
else{
runMyFunction();
}
}
function runMyFunction(){
var urlParams = window.location.search.substring(1).split('&'),
username = "",
hscEmailInput = document.getElementById('userfield6'),
i = 0;
if (urlParams !== "") {
for (i = 0; i < urlParams.length; i++) {
if (urlParams[i].substr(0,4) === "USER") {
username = urlParams[i].replace('USER=', '');
hscEmailInput.value = username + '@example.com';
absearch('&PAGESIZE=1');
}
}
}
}
waitForFnc();
回答by Wayne
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:
您遇到了 Greasemonkey 的安全限制,这会阻止您执行页面中的功能。请参阅我对上一个问题的回答以获取解释和一些安全的解决方法:
UserScripts & Greasemonkey: calling a website's JavaScript functions
回答by no.good.at.coding
If the problem is indeed that your script runs too early, perhaps checking and waiting until the DOM is ready is the solution: Relying on DOM readiness to invoke a function (instead of window.onload)
如果问题确实是您的脚本运行得太早,也许检查并等待 DOM 准备就绪是解决方案:依靠 DOM 准备来调用函数(而不是 window.onload)
Edit
编辑
This answer on SOmight also prove to be useful.
这个关于 SO 的答案也可能被证明是有用的。

