Fau*_*ust 4 google-chrome-extension chrome-extension-manifest-v3
我正在编写一个 chrome 扩展,我想完全禁用弹出窗口,或者在 v3 清单属性不允许当前选项卡的 url 时显示一条消息host_permissions。
这是一个开发支持工具,我们不希望为生产 URL 启用它。所以如果我设置:
"host_permissions": [
"http://localhost:3000/",
"https://*.dev.some-site.com/",
"https://www.uat.some-site.com/"
]
Run Code Online (Sandbox Code Playgroud)
...然后,如果用户位于www.some-site.com(或其他任何地方),我希望禁用弹出窗口。
我可以很容易地获得相关的网址:
let [currentTab] = await chrome.tabs.query({ active: true, currentWindow: true });
const { url } = currentTab;
const cookieUrl = url.match(/https?:\/\/[^/]+\//)?.[0];
Run Code Online (Sandbox Code Playgroud)
...我可以通过以下方式获取允许的文件模式数组
chrome.runtime.getManifest().host_permissions
Run Code Online (Sandbox Code Playgroud)
但是我怎样才能使用这个或其他东西来防止弹出窗口呢?此外,将该通配符转换为真正的正则表达式会有点痛苦。难道没有一些开箱即用的方法来完成这一切吗?
wOx*_*xOm 11
使用chrome.declarativeContent API 为 中的网站启用操作弹出窗口host_permissions,并默认在其他地方禁用它。
禁用后,该图标将变为灰色。单击它将显示内置上下文菜单,不会触发 chrome.action.onClicked。拼图菜单中的扩展名也呈灰色。
清单.json:
"action": {"default_icon": "icon.png"},
"permissions": ["declarativeContent"],
Run Code Online (Sandbox Code Playgroud)
后台脚本:
chrome.action.disable();
chrome.runtime.onInstalled.addListener(() => {
chrome.declarativeContent.onPageChanged.removeRules(() => {
chrome.declarativeContent.onPageChanged.addRules([{
conditions: chrome.runtime.getManifest().host_permissions.map(h => {
const [, sub, host] = h.match(/:\/\/(\*\.)?([^/]+)/);
return new chrome.declarativeContent.PageStateMatcher({
pageUrl: sub ? {hostSuffix: '.' + host} : {hostEquals: host},
});
}),
actions: [new chrome.declarativeContent.ShowAction()],
}]);
});
});
Run Code Online (Sandbox Code Playgroud)
为了简单起见,我不限制 PageStateMatcher 的方案。
| 归档时间: |
|
| 查看次数: |
1218 次 |
| 最近记录: |