Select 首次点击浏览器操作时会打开哪个弹出窗口
Select which popup opens on a first click on a browser action
我开发了一个 chrome 扩展,可以将数据上传到我的 API。
接下来是 signin
页和 data-upload
页。至于这个功能,一切正常。
问题是当我第一次点击扩展时,它没有打开。第二次点击它打开。当用户单击扩展程序时,我需要立即打开我的扩展程序。我不知道该怎么做。
这是我的代码:
manifest.json
{
"content_scripts": [
{
"matches": [ "http://*/*", "https://*/*"],
"js": [ "js/dataupload.js", "js/signin.js" ]
}
],
"name": "upload",
"version": "1.1",
"manifest_version": 2,
"description": "Quick access to upload data",
"content_security_policy": "script-src 'self' https://ajax.googleapis.com; object-src 'self'",
"permissions": [
"http://*/*",
"https://*/*",
"https://myapi.com/*",
"https://ajax.googleapis.com/*",
"management",
"tabs"
],
"icons": {
"128": "images/icon.png"
},
"background": {
"scripts": ["js/background.js"]
},
"browser_action": {
"default_title": "Upload APP",
"default_icon": "images/icon.png"
},
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
}
background.js
chrome.browserAction.onClicked.addListener(function(tab) {
if (window.localStorage.getItem('user_api_key')) {
chrome.browserAction.setPopup({popup: "upload.html"});
}
else{
chrome.browserAction.setPopup({popup: "signin.html"});
}
});
代码按预期工作。首次点击被 chrome.browserAction.onClicked
捕获,它设置了下一次点击时发生的情况。
有 will be soon 一个 chrome.browserAction.openPopup
函数形式的直接解决方案,但是 它还没有稳定版。
所以现在您必须重新组织逻辑,因为如果弹出窗口没有在点击时立即发生,您将无法打开它。您应该在清单中将 upload.html
设置为默认弹出窗口,然后在 upload.html
中检查条件并在需要时重定向。请注意,localStorage
在后台和弹出窗口之间共享。
所以,清单:
"browser_action": {
"default_title": "Upload APP",
"default_icon": "images/icon.png",
"default_popup": "upload.html"
},
upload.html
的代码:
if (!window.localStorage.getItem('user_api_key')) {
window.location = chrome.runtime.getURL("signin.html");
}
我开发了一个 chrome 扩展,可以将数据上传到我的 API。
接下来是 signin
页和 data-upload
页。至于这个功能,一切正常。
问题是当我第一次点击扩展时,它没有打开。第二次点击它打开。当用户单击扩展程序时,我需要立即打开我的扩展程序。我不知道该怎么做。
这是我的代码:
manifest.json
{
"content_scripts": [
{
"matches": [ "http://*/*", "https://*/*"],
"js": [ "js/dataupload.js", "js/signin.js" ]
}
],
"name": "upload",
"version": "1.1",
"manifest_version": 2,
"description": "Quick access to upload data",
"content_security_policy": "script-src 'self' https://ajax.googleapis.com; object-src 'self'",
"permissions": [
"http://*/*",
"https://*/*",
"https://myapi.com/*",
"https://ajax.googleapis.com/*",
"management",
"tabs"
],
"icons": {
"128": "images/icon.png"
},
"background": {
"scripts": ["js/background.js"]
},
"browser_action": {
"default_title": "Upload APP",
"default_icon": "images/icon.png"
},
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
}
background.js
chrome.browserAction.onClicked.addListener(function(tab) {
if (window.localStorage.getItem('user_api_key')) {
chrome.browserAction.setPopup({popup: "upload.html"});
}
else{
chrome.browserAction.setPopup({popup: "signin.html"});
}
});
代码按预期工作。首次点击被 chrome.browserAction.onClicked
捕获,它设置了下一次点击时发生的情况。
有 will be soon 一个 chrome.browserAction.openPopup
函数形式的直接解决方案,但是 它还没有稳定版。
所以现在您必须重新组织逻辑,因为如果弹出窗口没有在点击时立即发生,您将无法打开它。您应该在清单中将 upload.html
设置为默认弹出窗口,然后在 upload.html
中检查条件并在需要时重定向。请注意,localStorage
在后台和弹出窗口之间共享。
所以,清单:
"browser_action": {
"default_title": "Upload APP",
"default_icon": "images/icon.png",
"default_popup": "upload.html"
},
upload.html
的代码:
if (!window.localStorage.getItem('user_api_key')) {
window.location = chrome.runtime.getURL("signin.html");
}