【发布时间】: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.com、amazon.com 或任何其他变体。我仍然无法通过在中间添加逗号来添加多个网站,有什么想法吗?
【问题讨论】:
-
尝试在
conditions数组中添加几个条件,并在PageStateMatcher中使用urlContains而不是hostEquals。 -
另外,
hostEquals表示与主机名完全匹配,当然没有 protocol://。
标签: javascript plugins google-chrome-extension