简体   繁体   English

Chrome扩展程序-更改弹出窗口

[英]Chrome Extensions - Changing the popup window

I have a chrome extension that allows a user to log into a system. 我有一个chrome扩展程序,允许用户登录系统。 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. 目前,我正在通过javascript实现此功能,但有时会出现时间延迟。 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? 有没有更好的方法来实现这一目标?

You should be looking at chrome.browserAction API. 您应该查看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. 设置当用户单击浏览器操作的图标时将HTML文档作为弹出窗口打开。

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"});
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM