Chrome 扩展 - 内容脚本从 AJAX 链接/导航多次堆积
我非常接近让我的 Chrome 扩展程序完美运行,但现在遇到了最后一个问题。
该问题似乎与 Facebook 等网站上重复注入的内容脚本有关,点击链接时整个页面不会重新加载等......
我认为该问题与该网站非常依赖 AJAX 有关。
如果我刷新整个页面,或者第一次加载它,那么就没有问题。我的扩展运行并且内容脚本被正确注入。
但是,如果我第一次加载页面,然后单击网站上的链接,Facebook 的“内容部分”将加载您单击的任何链接,而顶部的浮动蓝色导航栏不会刷新或更改根本不。
IE 整个页面不会重新加载。由于网站的“内容部分”和导航栏都在同一框架中,因此您单击的每个链接都会再次注入内容脚本。
所以最终我在控制台日志中看到它正在运行 2X、3X、4X、5X 等。当我点击更多链接时,数字每次都会增加 1。
所以我的问题是这样的:
有没有一种简单的方法可以在再次注入之前检查内容脚本是否已经存在/活动?或者还有什么可以作为解决这种情况的方法?
I am super close to getting my Chrome Extension running perfectly, but am running into one last issue now.
The problem seems to be related to the content script being injected repeatedly on sites like Facebook, where the entire page is not reloaded upon clicking a link, etc...
I think the issue is related to the site being very AJAX-dependent.
If I refresh the entire page, or load it for the first time, then there is no problem. My extension runs and the content script is injected properly.
However, if I load the page the initial time, then click on a link on the site, the "content section" of Facebook will load whatever link you clicked on, while the floating blue navigation bar at the top doesn't refresh or change at all.
I.E. the entire page does not reload. Since both the "content section" of the site and the navigation bar are both in the same frame, the content script just keeps getting injected another time for every link you click.
So eventually I see in my console log that it's running 2X, 3X, 4X, 5X etc.. As I click around on more links, the number increases by 1 each time.
So my question is this:
Is there a simple way to check if the content script is ALREADY present/active before injecting it again? Or what else can be used as a work-around for a situation like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在为扩展程序编写内容脚本时预测到了类似的问题,因此我向每个页面的
元素添加了一个独特的属性,其中包含扩展程序名称和版本。
内容脚本所做的第一件事是检查该属性是否已编写脚本,如果已编写则停止。否则,它将添加此属性(值为
true
)并继续其用途。如果您想查看我的解决方案;
该技术稍微复杂的方面是使用 消息传递 获取版本并解析清单(在 AJAX 请求中检索)到 JSON 对象。
要准确了解我是如何实现这一目标的,请查看
init
和onRequestHelper
函数 此处;当然,您可以对版本进行硬编码或完全忽略它。只要属性名称是唯一的。
I predicted a similar problem when writing the content script for my extension so I added a unique attribute to the
<body>
element of each page with the extension name and version.First thing the content script does is check if this attribute already scripts and stops if it does. Otherwise, it will add this attribute (with a value of
true
) and continue with its purpose.If you want to see my solution;
The slightly more complicated aspect of this technique is fetching the version using message passing and parsing the manifest (retrieved in an AJAX request) in to a JSON object.
To see exactly how I achieved this look at the
init
andonRequestHelper
functions here;Of course, you could just hard code the version or omit it entirely. So long as the attribute name is unique.
在父框架中,您可以添加
window.doneInjecting = true;
或类似的内容吗? (或者可能向页面添加一个隐藏元素 - 我不确定子框架是否可以访问其父框架的 JavaScript 变量)但基本上设置一个简单的标志,以便每次注入内容脚本时,检查该标志是否存在并且仅如果没有继续?
In the parent frame, could you add
window.doneInjecting = true;
or something similar? (or perhaps add a hidden element to the page - I'm not sure if child frames can access their parent's JavaScript variables)But basically setting a simple flag, so that each time your content script is injected, check if the flag exists and only continue if it doesn't?
每次选项卡状态更改时 onUpdated 都会触发。只需在
changeInfo.status == "complete"
background.js加载您的内容脚本
onUpdated fires each time a tab's state changes. just load your content scripts at
changeInfo.status == "complete"
background.js