【问题标题】:Keep Popup Open when Opening a new Page打开新页面时保持弹出窗口打开
【发布时间】:2016-02-09 07:31:07
【问题描述】:

我有以下代码来介绍我的 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 处于打开状态,我确信有办法做到这一点. 我想同时打开它们。

最好的方法是什么?

【问题讨论】:

  • 如果你想打开popup.html,你可能不应该打电话给window.open("intro/index.html")
  • @DanielBeck 实际上,我想同时打开它们。
  • 应该打开popup.html的代码在哪里?
  • @DanielBeck 不需要代码,用户点击右上角的图标打开扩展。
  • 我们在 popup.html 中查看的代码也是?

标签: javascript jquery html google-chrome-extension


【解决方案1】:

您使用的方法是有效的并且应该可以工作,但您可能应该 只需使用 onInstalled 事件来保持一致性:

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

它不需要新的权限,并且会让您的安装代码与您的其他代码明确分开。

【讨论】:

    【解决方案2】:

    虽然 Marc Guiselin 的回答非常出色,但了解如何在不关闭弹出窗口的情况下打开选项卡可能会很有用。

    你可以在后台打开标签,这样它就不会关闭你的弹出窗口。

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

    一般来说,您应该避免在扩展中使用window.open,而是使用chrome.tabschrome.windows API。

    【讨论】:

    • 有效,但它在后台打开选项卡,有没有办法在前台打开?
    猜你喜欢
    • 1970-01-01
    • 2016-07-14
    • 2012-06-03
    • 1970-01-01
    • 2017-03-08
    • 1970-01-01
    • 2016-07-26
    • 2011-10-31
    • 2023-03-06
    相关资源
    最近更新 更多