Chrome 某些站点上的扩展内容脚本不是 运行
Chrome extension content scripts not running on certain sites
我一直在尝试编写一个非常简单的 Chrome 扩展程序(清单 v3),以便在您加入会议后自动关闭那些烦人的缩放标签页。
到目前为止,我已经能够让大多数页面随我的扩展自动关闭,但它只是拒绝 运行 在某些域上,包括我实际需要它 运行 的域: https://company-name-here.zoom.us/
。我最终想将内容脚本匹配器设置为仅缩放,但现在我已将其扩展到所有站点以减少错误源。
无论我如何尝试加载页面,它都不起作用,无论是通过在 google 日历事件上单击重定向 url,在页面打开后手动重新加载,甚至手动输入 url 并按回车键。 zoom 主页遇到同样的问题,但其他站点(如堆栈溢出)显示“内容脚本已加载”控制台日志,并在 5 秒内关闭,正如我预期的那样。
请在下面找到扩展的完整来源:
manifest.json
{
"manifest_version": 3,
"name": "Zoom Auto Closer",
"version": "1.0",
"background": {
"service_worker": "src/background.js"
},
"content_scripts": [{
"run_at": "document_start",
"matches": ["<all_urls>"],
"js": ["src/content.js"]
}]
}
src/content.js
const closeDelay = 5_000;
const closeCurrentTab = () => chrome.runtime.sendMessage('close-tab');
const main = () => {
console.log('Content script loaded');
setTimeout(closeCurrentTab, closeDelay);
};
main();
src/background.js
const closeTab = tabId => chrome.tabs.remove(tabId);
const onMessage = (message, sender) => {
console.log('Received a message:', message);
switch (message) {
case 'close-tab': return closeTab(sender.tab.id);
}
}
const main = () => {
console.log('Service worker registered');
chrome.runtime.onMessage.addListener(onMessage);
}
main();
问题可能与 <all_urls>
.
的使用有关
Google 在 matching patterns docs 上说:
The special pattern <all_urls> matches any URL that starts with a
permitted scheme.
允许的方案是http:
、https:
和file:
。
我不太熟悉 Zoom,但是 this 文章建议 Zoom 使用协议 zoommtg:
来启动桌面程序,所以这不在 <all_urls>
涵盖的范围内.
编辑:
现在我看到您声明网址以 https://
开头,这样可能会使我的建议无效。仍然值得尝试 "*://*.zoom.us/*"
而不是 <all_urls>
。
您可以尝试使用 "*://*.zoom.us/*"
。如果这不起作用,您可以尝试放弃内容脚本并在后台服务工作者中处理所有内容。
在后台 service worker 中,您可以为 chrome.tabs.onUpdated
and check the url value to see if it matches the url for a Zoom tab and close it from there. You would also need to use the Alarms API 添加一个监听器来延迟。
此方法效率不高,因为它会在每次选项卡更新时调用(真的不值得担心),但如果您无法使用内容脚本方法,它是一种可能的解决方法。
我一直在尝试编写一个非常简单的 Chrome 扩展程序(清单 v3),以便在您加入会议后自动关闭那些烦人的缩放标签页。
到目前为止,我已经能够让大多数页面随我的扩展自动关闭,但它只是拒绝 运行 在某些域上,包括我实际需要它 运行 的域: https://company-name-here.zoom.us/
。我最终想将内容脚本匹配器设置为仅缩放,但现在我已将其扩展到所有站点以减少错误源。
无论我如何尝试加载页面,它都不起作用,无论是通过在 google 日历事件上单击重定向 url,在页面打开后手动重新加载,甚至手动输入 url 并按回车键。 zoom 主页遇到同样的问题,但其他站点(如堆栈溢出)显示“内容脚本已加载”控制台日志,并在 5 秒内关闭,正如我预期的那样。
请在下面找到扩展的完整来源:
manifest.json
{
"manifest_version": 3,
"name": "Zoom Auto Closer",
"version": "1.0",
"background": {
"service_worker": "src/background.js"
},
"content_scripts": [{
"run_at": "document_start",
"matches": ["<all_urls>"],
"js": ["src/content.js"]
}]
}
src/content.js
const closeDelay = 5_000;
const closeCurrentTab = () => chrome.runtime.sendMessage('close-tab');
const main = () => {
console.log('Content script loaded');
setTimeout(closeCurrentTab, closeDelay);
};
main();
src/background.js
const closeTab = tabId => chrome.tabs.remove(tabId);
const onMessage = (message, sender) => {
console.log('Received a message:', message);
switch (message) {
case 'close-tab': return closeTab(sender.tab.id);
}
}
const main = () => {
console.log('Service worker registered');
chrome.runtime.onMessage.addListener(onMessage);
}
main();
问题可能与 <all_urls>
.
Google 在 matching patterns docs 上说:
The special pattern <all_urls> matches any URL that starts with a permitted scheme.
允许的方案是http:
、https:
和file:
。
我不太熟悉 Zoom,但是 this 文章建议 Zoom 使用协议 zoommtg:
来启动桌面程序,所以这不在 <all_urls>
涵盖的范围内.
编辑:
现在我看到您声明网址以 https://
开头,这样可能会使我的建议无效。仍然值得尝试 "*://*.zoom.us/*"
而不是 <all_urls>
。
您可以尝试使用 "*://*.zoom.us/*"
。如果这不起作用,您可以尝试放弃内容脚本并在后台服务工作者中处理所有内容。
在后台 service worker 中,您可以为 chrome.tabs.onUpdated
and check the url value to see if it matches the url for a Zoom tab and close it from there. You would also need to use the Alarms API 添加一个监听器来延迟。
此方法效率不高,因为它会在每次选项卡更新时调用(真的不值得担心),但如果您无法使用内容脚本方法,它是一种可能的解决方法。