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 badges
Add a ment  | 

1 Answer 1

Reset to default 8

You 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