【问题标题】:Auto start chrome extension自动启动 chrome 扩展
【发布时间】:2015-11-08 19:36:53
【问题描述】:

我正在尝试制作一个 chrome 扩展,在加载时重定向到其他页面。例如,如果它是 google.nl,请转到 google.nl?example 我设法让它工作,但只有当我按下扩展按钮时。我想从 background.js 运行脚本,但出现错误:

未捕获的类型错误:无法读取未定义的属性“onBeforeRequest”

为什么我不想重新加载?好吧,我没有。这只是为了测试。原计划是读取 URL 并将打开的图数据放入我的扩展中。

清单(部分)

"content_scripts": [{
"matches": [
  "<all_urls>"
],
"js": ["background.js"]
}],
"permissions": [
"tabs",
"activeTab",
 "webRequest"
],
"background": {
 "scripts": ["background.js"]
}

background.js

chrome.webRequest.onBeforeRequest.addListener(function(a) {
if (a.url.indexOf("http://www.google.com/") >= 0) {
    reloadExtensions();
    chrome.tabs.get(a.tabId, function(b) {
        if ((b.selected == false)) {
            chrome.tabs.remove(a.tabId)
        }
    });
    return {redirectUrl: chrome.extension.getURL("close.html")}
}
return {cancel: false}

}, {urls: ["http://reload.extensions/"],types: ["main_frame"]}, ["blocking"]);

【问题讨论】:

    标签: javascript google-chrome


    【解决方案1】:

    iirc the docs 表示您需要将所有要重定向的 URL 放入 permissions 数组中。

    如果不这样做,您将看到以下错误

    通过删除结帐 Catblock 示例 https://developer.chrome.com/extensions/samples#search:webrequest

    我认为您根本不需要内容脚本部分,实际上我怀疑这就是您看到错误的地方。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-12
      • 1970-01-01
      • 2018-08-20
      • 1970-01-01
      • 2015-10-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多