默认弹出窗口匹配URL
我测试一个在线调查应用程序。我的应用程序中有数百个文本框,为了测试目的,我必须输入一些数字。所以我创建了一个Chrome扩展来填充表单。我做到了,几乎和我预期的一样 - 除了有一个小问题。默认弹出窗口匹配URL
manifest.json的:
{
"name": "FillForm",
"version": "1.0",
"manifest_version": 2,
"description": "FillForm",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": ["activeTab"]
}
每当我点击browserAction按钮 - 它打开,那里是一个文本框的popup.html。如果我在那里输入1
,它将在我的应用程序中输入所有文本框的1
- 这是我想要的。
现在我想打开popup.html
仅适用于我的应用程序,即匹配URL http://example.com
,因为我不想在任何其他页面中输入任何信息。
我该如何做到这一点?
我会在与指定的URL匹配的页面中注入popup.html的内容。
- 这简化你的行动来填补你的表格(你没有必须点击扩展程序图标)
- 它不会有额外的图标
餍足您的浏览器这样做,第一修改清单:
{
"name": "FillForm",
"version": "1.0",
"manifest_version": 2,
"description": "FillForm",
"content_scripts": [
{
"matches": ["http://*.xxx.com/*"], // put your URL pattern here
"js": ["popup_inject.js"]
}
],
"web_accessible_resources": ["popup.html"]
"permissions": ["activeTab"]
}
和popup_inject.js
var iframe = document.createElement ("iframe");
iframe.src = chrome.extension.getURL ("popup.html");
iframe.style.position="absolute";
iframe.style.top="10px";
iframe.style.right="10px";
iframe.style.border="solid 1px #aaa";
document.querySelector("body").appendChild(iframe);
这就是Page Actions的确切目的:提供一个仅在特定网站上可见的按钮。
首先,你browser_action
键更改为page_action
:
"page_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
你需要决定自己的时候表现出来。通过declarativeContent
API,您可以提供一套规则说明您何时需要这样做。
添加declarativeContent
权限:
"permissions": ["activeTab", "declarativeContent"]
然后,添加一个background script将管理规则。由于您不需要后台脚本始终处于活动状态,因此非常适合Event Page。现在
"background": {
"scripts": ["eventPage.js"],
"persistent": false
},
,活动页面代码:
// eventPage.js
// This only needs to run on install/update, rules are remembered
chrome.runtime.onInstalled.addListener(function(details) {
var rule1 = {
conditions: [
new chrome.declarativeContent.PageStateMatcher({
// See declarativeContent docs for more options
pageUrl: { hostEquals: 'www.example.com' }
})
],
actions: [ new chrome.declarativeContent.ShowPageAction() ]
};
// Remove existing rules, if any
chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
// Then, add our rule1
chrome.declarativeContent.onPageChanged.addRules([rule1]);
});
});
加1为您的时间和答案。我会检查并通知你。 – KitKarson 2015-03-03 18:40:50
加1的时间和答案。我会检查并通知你。 – KitKarson 2015-03-03 18:40:38