首次安装Chrome扩展程序后,打开"帮助"页面

use*_*340 23 google-chrome google-chrome-extension

我是Chrome扩展新用户.我有一个问题,关于如何使扩展程序在安装后自动打开"帮助"页面.目前,我可以通过将值保存到localStorage来检查扩展是否第一次运行.但只有在单击工具栏上的图标时才会执行此检查.只是想知道是否有一种方式喜欢FF扩展,它在安装后使用javascript打开帮助页面.谢谢.

编辑:感谢davgothic的回答.我已经解决了这个问题.我有关于弹出窗口的另一个问题.我的扩展程序会检查当前标签的网址,

__PRE__
是否有可能以这种方式显示弹出窗口?

Nuh*_*hdy 59

检查Chrome提供的此更新且最可靠的解决方案:chrome.runtime事件

chrome.runtime.onInstalled.addListener(function (object) {
    chrome.tabs.create({url: "http://yoursite.com/"}, function (tab) {
        console.log("New tab launched with http://yoursite.com/");
    });
});
Run Code Online (Sandbox Code Playgroud)

将此添加到您的background.js意思是您在清单上定义的页面,如下所示,

....
"background": {
      "scripts": ["background.js"],
      "persistent": false
  }
...
Run Code Online (Sandbox Code Playgroud)

  • 不要忘记在回调中检查`if(object.reason ==='install')`否则也会触发更新. (7认同)
  • 甚至`if (chrome.runtime.OnInstalledReason.INSTALL === object.reason)` (2认同)
  • 好的解决方案 要打开本地文件,你应该像这样添加'tab部分':`chrome.tabs.create({url:chrome.extension.getURL("help.html")},function(tab){console.log( "使用本地文件启动新选项卡");});` (2认同)

Dav*_*ock 25

更新:不再推荐此方法.请参阅Nuhil最近的答案.

我相信你需要做的就是将这样的东西放到<head>扩展程序背景页面部分的脚本中,例如background.html

function install_notice() {
    if (localStorage.getItem('install_time'))
        return;

    var now = new Date().getTime();
    localStorage.setItem('install_time', now);
    chrome.tabs.create({url: "installed.html"});
}
install_notice();
Run Code Online (Sandbox Code Playgroud)


Moh*_*our 5

最好放置一个“版本”号,这样您就可以知道更新或安装扩展的时间。

已在这里回答: 检测Chrome扩展程序首次运行/更新


And*_*ler 5

截至目前(2020 年 11 月),在首次安装或更新扩展时执行代码的正确方法是使用 runtime.onInstalled 事件。

此事件记录在此处:https : //developer.chrome.com/extensions/runtime#event-onInstalled

chrome.runtime.onInstalled.addListener(function (details) {
  if (details.reason === "install") {
    // Code to be executed on first install
    // eg. open a tab with a url
    chrome.tabs.create({
      url: "https://google.com"
    });
  } else if (details.reason === "update") {
    // When extension is updated
  } else if (details.reason === "chrome_update") {
    // When browser is updated
  } else if (details.reason === "shared_module_update") {
    // When a shared module is updated
  }
});
Run Code Online (Sandbox Code Playgroud)

可以将此代码添加到后台脚本中:https : //developer.chrome.com/extensions/background_pages