Chrome扩展程序仅适用于“developer.chrome.com”

时间:2018-06-14 20:21:32

标签: javascript plugins google-chrome-extension

这是我的第一个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,非https://www.amazon.comamazon.com或任何其他变体。我仍然无法通过添加逗号来添加多个网站,其中有任何想法吗?

2 个答案:

答案 0 :(得分:3)

尝试添加更多条件并使用urlContainshostEquals代替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()] }]); });

例如:

{{1}}

答案 1 :(得分:-1)

尝试添加“*”以展开您的扩展程序将使用的亚马逊网页。

例如:

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

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