帮助使用户脚本在 Chrome 中工作
我为 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);
和 i'我不确定这是否是一个好主意。
我尝试的另一件事是保留 window.addEventListener('load', init, false);
行并在 内使用
以防找不到canvasframe。window.setTimeout(init, 1000);
>init()
因此,请让我知道使该脚本跨浏览器兼容的最佳方法是什么。 哦,我全神贯注地想让这个脚本变得更好/更高效的代码明智(我确信这是可能的)
编辑:没有帮助......? :'(
4 月 28 日编辑:
我重新编写了一些代码,现在看起来像这样:
if(document.location != top.location) return; (function() { var interval = window.setInterval(waitforiframe, 3000); var canvas; function waitforiframe() { console.log("Finding canvas frame"); canvas = document.getElementById("canvas_frame"); if (canvas && canvas.contentDocument) { console.log("Found canvas frame"); pimpmygmail(); } } function pimpmygmail() { gmail = canvas.contentDocument; if(!gmail) return; window.clearInterval(interval); console.log("Lets PIMP.MY.GMAIL!!!"); ......rest of the code...... })();
这在 Firefox 中工作得很好,但在 Chrome 中,它给了我一个 top is undefined
错误。 我注意到的另一件事是,如果我删除第一行 if(document.location != top.location) return;
, waitforiframe()
方法会不断被调用,并且再来一次。 (即我在控制台中看到“查找画布框架”错误)
有人可以告诉我第一行的作用是什么?我的意思是它实现了什么?如果我删除该行,为什么 waitforiframe()
方法会永远运行?
I've written a userscript for Gmail : Pimp.my.Gmail & i'd like it to be compatible with Google Chrome too.
Now i have tried a couple of things, to the best of my Javascript knowledge (which is very weak) & have been successful up-to a certain extent, though im not sure if it's the right way.
Here's what i tried, to make it work in Chrome:
The very first thing i found is that contentWindow.document
doesn't work in chrome, so i tried contentDocument
, which works.
BUT i noticed one thing, checking the console messages in Firefox and Chrome, i saw that the script gets executed multiple times in Firefox whereas in Chrome it just executes once!
So i had to abandon the window.addEventListener('load', init, false);
line and replace it with window.setTimeout(init, 5000);
and i'm not sure if this is a good idea.
The other thing i tried is keeping the window.addEventListener('load', init, false);
line and using window.setTimeout(init, 1000);
inside init()
in case the canvasframe is not found.
So please do lemme know what would be the best way to make this script cross-browser compatible.
Oh and im all ears for making this script better/efficient code wise (which im sure is possible)
edit: no help...? :'(
edit 28-Apr:
i re-wrote the code a little and now it looks something like this.:
if(document.location != top.location) return; (function() { var interval = window.setInterval(waitforiframe, 3000); var canvas; function waitforiframe() { console.log("Finding canvas frame"); canvas = document.getElementById("canvas_frame"); if (canvas && canvas.contentDocument) { console.log("Found canvas frame"); pimpmygmail(); } } function pimpmygmail() { gmail = canvas.contentDocument; if(!gmail) return; window.clearInterval(interval); console.log("Lets PIMP.MY.GMAIL!!!"); ......rest of the code...... })();
This works perfectly fine in Firefox, but in Chrome, it gives me a top is undefined
error.
Another thing i noticed is that if i remove the first line if(document.location != top.location) return;
, the waitforiframe()
method keeps getting called over and over again. (ie i see the "Finding canvas frame"
error in the console)
can someone tell me what does the first line do? i mean what does it achieve & why does the waitforiframe()
method run forever if i remove that line??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
非常感谢所有提供帮助的人! -_-
顺便说一句,这就是我在脚本开始时所需要的:
A BIG THANK YOU TO ALL WHO HELPED! -_- meh
btw, this was all i needed at the beginning of the script: