Keep Popup Open when Opening a new Page

ε祈祈猫儿з 提交于 2019-12-20 07:14:25

问题


I have the following code to introduce my Chrome Extension.

// detect if this is the first time running
var first_run = false;
if (!localStorage['ran_before']) {
    first_run = true;
    localStorage['ran_before'] = '1';
}

// if not, start the intro() script
if (first_run) intro();

// intro script
function intro() {
    window.open("intro/index.html", '_blank');
}

But sadly, when I click the extension, it doesn't open the popup.html but just opens the intro page.. it needs to keep the popup.html open and I'm sure there is a way to do this. I want to open them both at the same time.

What is the best way to do this?


回答1:


While Marc Guiselin's answer is excellent, it may be useful to know how to open a tab without closing a popup.

You could open the tab in the background, that way it won't close your popup.

chrome.tabs.create({
  url: chrome.runtime.getURL("intro/index.html"),
  active: false
});

In general, you should avoid using window.open in extensions and use chrome.tabs and chrome.windows API instead.




回答2:


the method you are using is valid and should work, but you should probably just use the onInstalled event for consistency:

chrome.runtime.onInstalled.addListener(function(info){
    if(info.reason == "install"){
        console.log("Installed!");
    }else if(info.reason == "update"){
        console.log("Updated!");
    }
});

It doesn't require new permissions, and will keep your install code clearly separated from the rest of your code.



来源:https://stackoverflow.com/questions/35272905/keep-popup-open-when-opening-a-new-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!