Chrome扩展内容脚本在URL更改时不起作用,而仅在刷新后起作用
我正在开发一个Chrome扩展程序,以在GitHub站点上进行一些更改,而URL Postfix匹配源代码类型(例如,XXX.CPP),
我希望每次选项卡时都会运行content.js
完成加载DOM元素。但是,仅在我按“刷新”按钮时运行。如果我单击超链接并转到另一个URL,content.js
将无法运行。
我做错了什么?谢谢。
以下是代码。
subtest.json
{
"name": "My Chrome Ext",
"description": "change appearance of source code on Github",
"version": "0.1",
"manifest_version": 3,
"permissions": ["storage", "activeTab"],
"action": {},
"content_scripts": [
{
"matches": ["*://github.com/*cpp"],
"all_frames": true,
"js": ["content.js"],
"run_at": "document_end"
}
]
}
content.js
window.alert("start running content.js");
I'm developing a chrome extension to make some changes on Github site while the URL postfix matches the source code type (for example, xxx.cpp)
I expect the content.js
will run every time the tab finishes loading the DOM elements. However, it only runs when I press the refresh button. If I click the hyperlinks and go to another URL, content.js
won't run.
What did I do wrong? Thanks.
Following are the code.
manifest.json
{
"name": "My Chrome Ext",
"description": "change appearance of source code on Github",
"version": "0.1",
"manifest_version": 3,
"permissions": ["storage", "activeTab"],
"action": {},
"content_scripts": [
{
"matches": ["*://github.com/*cpp"],
"all_frames": true,
"js": ["content.js"],
"run_at": "document_end"
}
]
}
content.js
window.alert("start running content.js");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Chrome扩展内容脚本仅在匹配您清单中指定的URL的全新网页时才加载。在这个单页Web应用程序的时代,包括GitHub在内的许多网站都使用JavaScript框架和Ajax调用,仅在用户在网站周围导航时仅更新现有网页内容的部分。即使正在更新地址栏,大多数时候都没有执行实际的页面负载,因此您的Chrome扩展名不会触发。
这改善了网站的性能,但这确实意味着即使浏览器正在显示匹配的URL,浏览器扩展程序也不能总是取决于其内容脚本。
我的解决方案?启用整个站点和门的扩展名
。页面重新加载。您可能必须使用观察者配置来确保观察者在所有适当的时间触发回调。
请注意,除非整个页面刷新整个页面,否则观察者的目标节点是保留在文档中的节点。如果被覆盖,您将失去观察者,而您的脚本将不会被触发。在我的情况下,文档可以正常工作。
The Chrome extension content script will only load when a completely new webpage matching the URL specified in your manifest is loaded. In this era of single page web applications, many websites, including GitHub, use Javascript frameworks and Ajax calls to only update parts of the existing webpage content as the user navigates around the site. Even though the address bar is being updated, most of the time no actual page loads are being executed, so your chrome extension won't trigger.
This improves the performance of the website, but it does mean that browser extensions can't always depend on their content scripts being loaded even if the browser is displaying a matching URL.
My solution? Enable the extension for the entire site and the gate the content script functionality behind a MutationObserver that checks window.location.href every time the callback is called:
I also invoke the callback immediately the content script is called to ensure the script triggers correctly on a page reload. You might have to play with the observer configuration to ensure the observer triggers the callback at all the right times.
Note, the target node for the observer be one that remains in the document unless the entire page is refreshed. If it's overwritten, you will lose the observer and your script won't be triggered. In my case the document.body works just fine.
在战斗了几天之后,我找到了一种解决历史状态更新时可靠起作用的解决方案,但它需要许多许可才能运行。
这需要清单V3的以下权限
,并将
以下代码添加到Service-worker.js
After fighting this for days, I found a solution that works reliably when the history state is updated, but it requires many permissions to run.
This requires the following permissions for manifest v3
and
Add the following code to service-worker.js