多次执行注入脚本
我已阅读文档并理解这是预期的:
脚本被注入到顶级页面和任何具有 HTML 源的子页面中,例如 iframe。不要假设每个浏览器选项卡只有一个脚本实例。
不过,我想知道:
- 除了 iframe 之外,还有哪些其他元素具有“HTML 源”(图像?对象?)? “HTML 源代码”这个术语对我来说含糊不清,令人不安。
- 有没有办法检测哪个元素正在执行脚本?
按照建议,我已通过确定 window === window.top
过滤掉了 iframe,但其他元素仍在执行脚本,并且执行次数比我多很多想要。
谢谢。
I've read the documentation and understand that this is to be expected:
Scripts are injected into the top-level page and any children with HTML sources, such as iframes. Do not assume that there is only one instance of your script per browser tab.
I'm wondering, though:
- Other than iframes, what other elements have "HTML sources" (images? objects?)? The term "HTML sources" is uncomfortably vague to my ears.
- Is there any way to detect which element is executing the script?
I've filtered out iframes by determining that window === window.top
, as recommended, but other elements are still executing the script and it's executing a lot more than I'd like.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我回答自己的问题确实是我自己的错,因为我没有在问题中提供足够的信息。在我看来,我当时不知道我提供的信息太少了。
无论如何,在寻找解决方案的过程中,我在 Apple 的开发论坛上提出了一个问题< /a> 并包含以下关键信息:
document
的beforeload
事件上(或者应该发生)。我了解到的是
beforeload
事件仅针对文档中的子资源触发。不适用于文档(或窗口)本身。我删除了事件处理程序并确保该脚本被应用为启动脚本(确实如此)。我已经将窗口作为顶部窗口进行了测试,所以我被覆盖了。现在我的注入脚本只触发一次。It's really my own fault that I'm answering my own question because I didn't really provide enough information in my question. In my defense, I didn't know at the time that I was providing too little information.
Anyway, while traveling down the road to a solution for this, I asked a question on Apple's dev forum and included the following bit of key info:
beforeload
event of thedocument
(or was supposed to).What I learned was that the
beforeload
event only fires for subresources within the document. Not for the document (or window) itself. I removed the event handler and made sure that the script was applied as a start script (it was). I'd already applied the test for the window as top window so I was covered. Now my injection script only fires once.其他 HTML 源可能是框架或对象标签(带有 HTML 内容)。我不认为它可以是其他任何东西。然而,据我所知,它们也应该用
window === window.top
过滤掉。尝试使用console.log
document.location
变量来查看哪个 URL 运行您注入的脚本,也许您可以找到加载它们的内容。Other HTML sources may be frames or object tags (with HTML contents). I don't think it can be anything else. However, to the extent of my knowledge, they should also be filtered off with
window === window.top
. Tryconsole.log
ing thedocument.location
variable to see which URL runs your injected script, and maybe you can find what loads them.