如何在首次安装chrome扩展时添加侦听器

How to add listener for when chrome extension is first installed?

本文关键字:扩展 添加 侦听器 chrome 安装      更新时间:2023-09-26

我有一个chrome扩展,它接管了新的选项卡页面。当用户第一次安装插件并打开一个新的选项卡时,我想向他们展示一些东西。我正在尝试使用

chrome.runtime.onInstalled.addListener(function(details){
    console.log('test');
    alert('test');
});

来自https://developer.chrome.com/extensions/runtime#event-安装是为了让它正常工作。然而,在我安装插件并打开一个新的选项卡后,什么都没有发生。有什么建议吗?

对于mv3,请将以下内容放在后台:

const installListener = (details) => {
  if (details.reason === chrome.runtime.OnInstalledReason.INSTALL) {
    // do something
  }
  if (details.reason === chrome.runtime.OnInstalledReason.UPDATE) {
    // TODO: show changelog
  }
};
chrome.runtime.onInstalled.addListener(installListener);