admin管理员组文章数量:1313822
I have a chrome extension that allows a user to log into a system. When the user is not logged in I want the default page to appear when the extension is opened, but if they've already logged in, I want a different popup window to show when they open the extension.
I'm achieving this through javascript at the moment but sometimes there's a time delay. The original page will flash up for an instant before the script executes and switches to the different page. Is there a better way to implement this?
I have a chrome extension that allows a user to log into a system. When the user is not logged in I want the default page to appear when the extension is opened, but if they've already logged in, I want a different popup window to show when they open the extension.
I'm achieving this through javascript at the moment but sometimes there's a time delay. The original page will flash up for an instant before the script executes and switches to the different page. Is there a better way to implement this?
Share Improve this question asked Apr 2, 2015 at 11:52 Sean MorrisSean Morris 1351 gold badge2 silver badges10 bronze badges1 Answer
Reset to default 8You should be looking at chrome.browserAction
API.
Specifically:
chrome.browserAction.setPopup(object details)
Sets the html document to be opened as a popup when the user clicks on the browser action's icon.
So, to set your popup, you would call it like this:
chrome.browserAction.setPopup({popup: "logged_in.html"});
You can do this when you understand that the user is logged in; however, you may want to restore this state when extension is restarted. To do so, you should save the fact that you are logged in (or out) in persistent storage and call this function on initialization. An event script is a good place to do so.
// wherever you can do this in your code
function login_success() {
/* ... */
chrome.storage.local.set({logged_in: true});
chrome.browserAction.setPopup({popup: "logged_in.html"});
}
function login_failure() {
/* ... */
chrome.storage.local.set({logged_in: false});
chrome.browserAction.setPopup({popup: "default.html"});
}
// This goes into eventPage.js and executes once on extension load
chrome.storage.local.get("logged_in", function(data) {
if(data.logged_in)
chrome.browserAction.setPopup({popup: "logged_in.html"});
});
本文标签: javascriptChrome ExtensionsChanging the popup windowStack Overflow
版权声明:本文标题:javascript - Chrome Extensions - Changing the popup window - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741955139a2406945.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论