如何在 chrome:// 上防止 chrome 扩展到 运行
How to prevent chrome extension to run on chrome://
绝对JS新手,请见谅,恕我孤陋寡闻
在 service_worker (background.js) 我正在尝试构建的 Chrome 扩展的后台 (manifest_version: 3) 我使用 chrome.action API 控制工具栏图标的行为。
chrome.action.onClicked.addListener(async function (tab) {
chrome.scripting.executeScript({
target: {tabId: tab.id},
files: ['iconClicked.js']
});
});
现在,当用户在 chrome://extensions 上单击该图标时,会产生错误:
Uncaught (in promise) Error: Cannot access a chrome:// URL
如何防止我的脚本在 chrome://URLs 上 运行?
我尝试将其包装在检查当前 URL:
的条件中
if (window.location.href.includes("urls/where/i/want/my/script/to/run") == true) {
chrome.action.onClicked.addListener(async function (tab) {
chrome.scripting.executeScript({
target: {tabId: tab.id},
files: ['iconClicked.js']
});
});
};
但是,唉,显然在 chrome:// "window" 上没有定义:
ReferenceError: window is not defined
我还能如何有效地防止我的扩展程序在 chrome:// 上抛出错误(最好是 w/out 使用 jQuery 或任何其他库)?
非常感谢任何提示!
后台脚本作为服务工作者运行,因此它与网页的 location
无关。
因为 executeScript
returns 默认是一个 Promise 所以你可以 catch
它忽略错误:
chrome.action.onClicked.addListener(async tab => {
await chrome.scripting.executeScript({
target: {tabId: tab.id},
files: ['iconClicked.js'],
}).catch(() => {});
});
或用 await
包裹在 try
中:
chrome.action.onClicked.addListener(async tab => {
try {
const results = await chrome.scripting.executeScript({
target: {tabId: tab.id},
files: ['iconClicked.js'],
});
// console.log(results);
} catch (e) {
// ignoring the error
}
});
为了完整起见,ManifestV3 也可以与回调一起使用:
chrome.action.onClicked.addListener(tab => {
chrome.scripting.executeScript({
target: {tabId: tab.id},
files: ['iconClicked.js'],
}, () => chrome.runtime.lastError); // ignoring the error
});
绝对JS新手,请见谅,恕我孤陋寡闻
在 service_worker (background.js) 我正在尝试构建的 Chrome 扩展的后台 (manifest_version: 3) 我使用 chrome.action API 控制工具栏图标的行为。
chrome.action.onClicked.addListener(async function (tab) {
chrome.scripting.executeScript({
target: {tabId: tab.id},
files: ['iconClicked.js']
});
});
现在,当用户在 chrome://extensions 上单击该图标时,会产生错误:
Uncaught (in promise) Error: Cannot access a chrome:// URL
如何防止我的脚本在 chrome://URLs 上 运行?
我尝试将其包装在检查当前 URL:
的条件中if (window.location.href.includes("urls/where/i/want/my/script/to/run") == true) {
chrome.action.onClicked.addListener(async function (tab) {
chrome.scripting.executeScript({
target: {tabId: tab.id},
files: ['iconClicked.js']
});
});
};
但是,唉,显然在 chrome:// "window" 上没有定义:
ReferenceError: window is not defined
我还能如何有效地防止我的扩展程序在 chrome:// 上抛出错误(最好是 w/out 使用 jQuery 或任何其他库)?
非常感谢任何提示!
后台脚本作为服务工作者运行,因此它与网页的 location
无关。
因为 executeScript
returns 默认是一个 Promise 所以你可以 catch
它忽略错误:
chrome.action.onClicked.addListener(async tab => {
await chrome.scripting.executeScript({
target: {tabId: tab.id},
files: ['iconClicked.js'],
}).catch(() => {});
});
或用 await
包裹在 try
中:
chrome.action.onClicked.addListener(async tab => {
try {
const results = await chrome.scripting.executeScript({
target: {tabId: tab.id},
files: ['iconClicked.js'],
});
// console.log(results);
} catch (e) {
// ignoring the error
}
});
为了完整起见,ManifestV3 也可以与回调一起使用:
chrome.action.onClicked.addListener(tab => {
chrome.scripting.executeScript({
target: {tabId: tab.id},
files: ['iconClicked.js'],
}, () => chrome.runtime.lastError); // ignoring the error
});