在我的Firefox网络扩展程序中,我在浏览器工具栏中添加了一个新按钮,打开一个小弹出窗口。在此弹出窗口中,会显示一个按钮,该按钮应创建一个新的浏览器窗口,在新选项卡中打开几个URL,最后在这些页面上执行不同的内容脚本。
我的代码看起来像这样
的manifest.json
{
"manifest_version": 2,
"name": "Auto URL Opener",
"version": "0.0.1",
"description": "Auto URL Opener",
"icons": { "48": "icon48.png" },
"permissions": [ "tabs", "<all_urls>" ],
"browser_action": {
"default_icon": { "48": "icon48.png" },
"default_title": "Auto URL Opener",
"default_popup": "popup.html"
}
}
popup.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<button id="openUrlsButton">Open URLs</button>
<script src="popup.js"></script>
</body>
</html>
popup.js
document.getElementById('openUrlsButton').addEventListener("click", function(e) {
console.log('### create new window and open tabs');
browser.windows.create({
incognito: false,
url: 'about:blank'
}).then((window) => {
console.log('### window created', window.id);
browser.tabs.create({
url: 'http://www.xkcd.com/',
windowId: window.id
}).then((tab) => {
console.log('### tab1 created', tab.id);
browser.tabs.executeScript(tab.id, {
code: 'document.body.style.border = "5px solid green"'
}).then(() => {
console.log('### executed content script in tab1');
});
});
browser.tabs.create({
url: 'http://www.google.com/',
windowId: window.id
}).then((tab) => {
console.log('### tab2 created', tab.id);
browser.tabs.executeScript(tab.id, {
code: 'document.body.style.border = "5px solid blue"'
});
}).then(() => {
console.log('### executed content script in tab2');
});
});
}
我希望的行为是
### create new window and open tabs
### window created <window id>
### tab1 created <tab id>
### executed content script in tab1
### tab2 created <tab id>
### executed content script in tab2
实际行为是
### create new window and open tabs
### window created <window id>
当打开新的浏览器窗口并自动关闭Web扩展弹出窗口时,感觉就像执行代码一样。
当我按以下方式重新考虑我的代码时
的manifest.json
{
"manifest_version": 2,
"name": "Auto URL Opener",
"version": "0.0.1",
"description": "Auto URL Opener",
"icons": { "48": "icon48.png" },
"permissions": [ "tabs", "<all_urls>" ],
"background": {
"scripts": ["background.js"]
},
"browser_action": {
"default_icon": { "48": "icon48.png" },
"default_title": "Auto URL Opener",
"default_popup": "popup.html"
}
}
popup.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<button id="openUrlsButton">Open URLs</button>
<script src="popup.js"></script>
</body>
</html>
popup.js
document.getElementById('openUrlsButton').addEventListener("click", function(e) {
browser.runtime.sendMessage({
task: "open urls"
});
}
background.js
function openUrls() {
console.log('### create new window and open tabs');
browser.windows.create({
incognito: false,
url: 'about:blank'
}).then((window) => {
console.log('### window created', window.id);
browser.tabs.create({
url: 'http://www.xkcd.com/',
windowId: window.id
}).then((tab) => {
console.log('### tab1 created', tab.id);
browser.tabs.executeScript(tab.id, {
code: 'document.body.style.border = "5px solid green"'
}).then(() => {
console.log('### executed content script in tab1');
});
});
browser.tabs.create({
url: 'http://www.google.com/',
windowId: window.id
}).then((tab) => {
console.log('### tab2 created', tab.id);
browser.tabs.executeScript(tab.id, {
code: 'document.body.style.border = "5px solid blue"'
});
}).then(() => {
console.log('### executed content script in tab2');
});
});
}
browser.runtime.onMessage.addListener(openUrls);
我得到了理想的行为,但我不确定这是否是一个很好的解决方案。
答案 0 :(得分:0)
脚本2方法是解决问题的正确方法