【问题标题】:Help making userscript work in chrome帮助使用户脚本在 chrome 中工作
【发布时间】:2010-04-24 14:41:50
【问题描述】:

我已经为 Gmail 编写了一个用户脚本:Pimp.my.Gmail 并且我希望它也与 Google Chrome 兼容。
现在我已经尝试了几件事,尽我的 Javascript 知识(非常薄弱)并且在一定程度上取得了成功,尽管我不确定这是否是正确的方法。

这是我尝试过的,以使其在 Chrome 中运行:
我发现的第一件事是 contentWindow.document 在 chrome 中不起作用,所以我尝试了 contentDocument,它有效。
但是我注意到一件事,检查 Firefox 和 Chrome 中的控制台消息,我看到该脚本在 Firefox 中执行了多次,而在 Chrome 中它只执行一次!
所以我不得不放弃window.addEventListener('load', init, false); 行并用window.setTimeout(init, 5000); 替换它,我不确定这是否是个好主意。

我尝试的另一件事是保留window.addEventListener('load', init, false); 行并在init() 中使用window.setTimeout(init, 1000);,以防找不到canvasframe。

所以请让我知道什么是使该脚本跨浏览器兼容的最佳方法。 哦,我全神贯注于使这个脚本更好/更高效的代码(我确信这是可能的)

编辑:没有帮助...? :'(

4 月 28 日编辑:
我重新编写了一点代码,现在看起来像这样。:

if(document.location != top.location) 返回; (功能() { var 间隔 = window.setInterval(waitforiframe, 3000); var 画布; 函数 waitforiframe() { console.log("寻找画布框架"); canvas = document.getElementById("canvas_frame"); if (canvas && canvas.contentDocument) { console.log("找到画布框架"); pimpmygmail(); } } 函数 pimpmygmail() { gmail = canvas.contentDocument; 如果(!gmail)返回; window.clearInterval(间隔); console.log("让 PIMP.MY.GMAIL !!!"); ......剩下的代码...... })();

这在 Firefox 中运行良好,但在 Chrome 中,它给了我一个 top is undefined 错误。 我注意到的另一件事是,如果我删除第一行 if(document.location != top.location) return;waitforiframe() 方法会不断被一遍又一遍地调用。 (即我在控制台中看到"Finding canvas frame" 错误)

谁能告诉我第一行是做什么的?我的意思是,如果我删除那条线,为什么 waitforiframe() 方法会永远运行??

【问题讨论】:

  • 哎呀,我需要做什么才能得到这个答案....
  • 看在老天爷的份上,请...

标签: javascript cross-browser google-chrome greasemonkey userscripts


【解决方案1】:

非常感谢所有帮助过的人! -_- 嗯

顺便说一句,这就是我在脚本开头所需要的:

try { if(top.location.href != window.location.href) { return; } }
catch(e) { return; }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-24
    • 2017-06-02
    • 1970-01-01
    相关资源
    最近更新 更多