【问题标题】:Chrome Extension only works on "developer.chrome.com"Chrome 扩展程序仅适用于“developer.chrome.com”
【发布时间】:2018-06-14 20:21:32
【问题描述】:

这是我的第一个 chrome 扩展,所以请原谅我提出这样一个新手问题。我的扩展程序目前仅适用于“developer.chrome.com”(图标有颜色),但我希望它适用于 amazon.com(图标为灰色)。通过谷歌搜索,我没有看到其他人提出同样的问题,所以我猜这一定是我的错误。这是我的 manifest.json:

{
  "name": "Reviews Extension",
  "version": "1.0",
  "description": "Get additional reviews from third party retailers",
  "permissions": [
    "https://api.walmartlabs.com/",
    "https://www.amazon.com/",
    "activeTab",
    "declarativeContent",
    "storage"
  ],
  "content_scripts": [
    {
      "matches": [
        "<all_urls>"
      ],
      "js": ["content.js"]
    }
  ],
  "options_page": "options.html",
  "background": {
    "scripts": [
      "background.js"
    ],
    "persistent": false
  },
  "page_action": {
    "default_popup": "popup.html",
    "default_icon": {
      "16": "images/get_started16.png",
      "32": "images/get_started32.png",
      "48": "images/get_started48.png",
      "128": "images/get_started128.png"
    }
  },
  "icons": {
    "16": "images/get_started16.png",
    "32": "images/get_started32.png",
    "48": "images/get_started48.png",
    "128": "images/get_started128.png"
  },
  "manifest_version": 2
}

更新: 当我在pageUrl: {hostEquals: 'developer.chrome.com'} 上摆弄 background.js 时,我发现当我删除 URL 时,它不再适用于 developer.chrome.com。但是当我将其更改为 https://www.amazon.com/ 代替 developer.chrome.com 时,它也无法在亚马逊的主页上运行。

'use strict';

chrome.runtime.onInstalled.addListener(function () {
  chrome.storage.sync.set({ color: '#3aa757' }, function () {
    console.log('The color is green.');
  });
  chrome.declarativeContent.onPageChanged.removeRules(undefined, function () {
    chrome.declarativeContent.onPageChanged.addRules([{
      conditions: [new chrome.declarativeContent.PageStateMatcher({
        pageUrl: { hostEquals: 'developer.chrome.com' },
      })],
      actions: [new chrome.declarativeContent.ShowPageAction()]
    }]);
  });
});

更新 2: 通过更改 background.js 中的 pageURL 使其工作。它必须以非常具体的方式格式化:www.amazon.com、NOT https://www.amazon.comamazon.com 或任何其他变体。我仍然无法通过在中间添加逗号来添加多个网站,有什么想法吗?

【问题讨论】:

  • 尝试在conditions数组中添加几个条件,并在PageStateMatcher中使用urlContains而不是hostEquals
  • 另外,hostEquals 表示与主机名完全匹配,当然没有 protocol://。

标签: javascript plugins google-chrome-extension


【解决方案1】:

尝试添加更多条件并使用hostContainsurlContains 而不是hostEquals

例如:

chrome.declarativeContent.onPageChanged.removeRules(undefined, function () {
    chrome.declarativeContent.onPageChanged.addRules([{
        conditions: [
            new chrome.declarativeContent.PageStateMatcher({
                pageUrl: { hostContains: '.developer.chrome.com' },
            }),
            new chrome.declarativeContent.PageStateMatcher({
                pageUrl: { hostContains: '.amazon.com' },
            })
        ],
        actions: [new chrome.declarativeContent.ShowPageAction()]
    }]);
});

【讨论】:

    【解决方案2】:

    尝试添加“*”以扩展您的扩展程序将在哪些亚马逊页面上运行。

    例如:

    "https://*.amazon.com/*"
    

    https://developer.chrome.com/apps/match_patterns

    【讨论】:

    • 感谢您的输入,我之前尝试过,我只是再次尝试,但没有运气。不过,我会把它留在 manifest.json 中,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2014-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多