簡體   English   中英

打開新頁面時使彈出窗口保持打開狀態

[英]Keep Popup Open when Opening a new Page

我有以下代碼來介紹我的Chrome擴展程序。

// 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');
}

但是可悲的是,當我單擊擴展名時,它不會打開popup.html而是僅打開介紹頁面。它需要保持popup.html打開狀態,我敢肯定有一種方法可以做到這一點。 我想同時打開它們。

做這個的最好方式是什么?

您使用的方法是有效的並且應該可以使用,但是您可能應該只使用onInstalled事件來保持一致性:

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

它不需要新的權限,並且可以使您的安裝代碼與其余代碼清楚地分開。

盡管Marc Guiselin的回答很好,但了解如何在不關閉彈出窗口的情況下打開選項卡可能很有用。

您可以在后台打開標簽頁,這樣就不會關閉彈出窗口。

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

通常,您應該避免在擴展程序中使用window.open ,而應使用chrome.tabschrome.windows API。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM