Chrome 扩展仅在特定网站可用
Chrome Extention Only Available in Specific Website
我希望我的扩展只出现在特定的网站上。
详情:
- 假设我将 url 设置为
example.com/*
- 所以每次我打开
example.com/*
我的扩展程序都会出现
- 否则,它应该是不可见的并且不起作用
我怎样才能做到这一点?
我的扩展功能是在特定网站中 运行 并突出显示一些 div 已保存的 ID。
我当前的清单:
{
"manifest_version": 2,
"name": "Dv Extention",
"description": "Highlight Selected",
"version": "1.0",
"background":{
"scripts":["background.js"],
"persistent": false
},
"page_action": {
"default_icon": "icon.png",
"default_popup": "popup.html",
"default_title": "Click Here!"
},
"permissions": [
"activeTab",
"https://ajax.googleapis.com/",
"webNavigation"
],
"content_scripts": [
{
"matches": ["*://example.com/*"],
"js": ["jquery-2.1.3.min.js","main.js"]
}
]
}
background.js
chrome.runtime.onInstalled.addListener(function() {
chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
chrome.declarativeContent.onPageChanged.addRules([{
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: {
hostEquals: 'example.com'
}
})
],
actions: [new chrome.declarativeContent.ShowPageAction()]
}]);
});
});
is correct. Page Actions 专门设计用于仅在特定情况下提供按钮:
Use the chrome.pageAction
API to put icons inside the address bar. Page actions represent actions that can be taken on the current page, but that aren't applicable to all pages.
您的后台代码是正确的,但是它不起作用,因为您忘记了 运行 它的权限。
您所基于的示例使用的是 Declarative Content API。它带有权限,因此您需要将其添加到清单中:
"permissions": [
"activeTab",
"https://ajax.googleapis.com/",
"webNavigation",
"declarativeContent"
],
那么您的页面操作应该会如预期那样显示。
我希望我的扩展只出现在特定的网站上。
详情:
- 假设我将 url 设置为
example.com/*
- 所以每次我打开
example.com/*
我的扩展程序都会出现 - 否则,它应该是不可见的并且不起作用
我怎样才能做到这一点?
我的扩展功能是在特定网站中 运行 并突出显示一些 div 已保存的 ID。
我当前的清单:
{
"manifest_version": 2,
"name": "Dv Extention",
"description": "Highlight Selected",
"version": "1.0",
"background":{
"scripts":["background.js"],
"persistent": false
},
"page_action": {
"default_icon": "icon.png",
"default_popup": "popup.html",
"default_title": "Click Here!"
},
"permissions": [
"activeTab",
"https://ajax.googleapis.com/",
"webNavigation"
],
"content_scripts": [
{
"matches": ["*://example.com/*"],
"js": ["jquery-2.1.3.min.js","main.js"]
}
]
}
background.js
chrome.runtime.onInstalled.addListener(function() {
chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
chrome.declarativeContent.onPageChanged.addRules([{
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: {
hostEquals: 'example.com'
}
})
],
actions: [new chrome.declarativeContent.ShowPageAction()]
}]);
});
});
Use the
chrome.pageAction
API to put icons inside the address bar. Page actions represent actions that can be taken on the current page, but that aren't applicable to all pages.
您的后台代码是正确的,但是它不起作用,因为您忘记了 运行 它的权限。
您所基于的示例使用的是 Declarative Content API。它带有权限,因此您需要将其添加到清单中:
"permissions": [
"activeTab",
"https://ajax.googleapis.com/",
"webNavigation",
"declarativeContent"
],
那么您的页面操作应该会如预期那样显示。