DEV Community

panfan
panfan

Posted on • Originally published at manon.icu on

Chrome插件开发 - Hooking into installs

当你想为用户提供一些额外信息的时候

当你想在用户卸载的时候触发某个操作

当你希望在更新的时候重定向到更改日志

扩展 hooks 将非常有必要

在浏览器扩展中捕获onInstall操作

打开public/background.js

chrome.runtime.onInstall.addListener((details) => {
  // do something
})
Enter fullscreen mode Exit fullscreen mode

触发onInstall的可能操作

  • install
  • update
  • chrome_update
  • shared_module_update

以下代码展示了捕获安装和更新的动作

chrome.runtime.onInstalled.addListener((details) => {
  if (details.reason === 'install') {
    chrome.notifications.create({
      type: 'basic',
      iconUrl: 'icons/icon-48.png',
      title: 'Hi there 👋',
      message: 'Welcome to the best extensions you ever installed',
      buttons: [{title: 'Thanks 😅️'}],
      priority: 0
    })
  }

  if (details.reason === 'update') {
    chrome.notifications.create({
      type: 'basic',
      iconUrl: 'icons/icon-48.png',
      title: 'Thank you',
      message: 'For updating this extensions',
      buttons: [{title: 'Cool'}],
      priority: 0
    })
  }
})
Enter fullscreen mode Exit fullscreen mode

当你想在用户卸载的时候,可以重定向到某个 url

chrome.runtime.onInstalled.addListener(details => {
    chrome.runtime.setUninstallURL('https://manon.icu/');
}
Enter fullscreen mode Exit fullscreen mode

Source Code

【Source Code】

Top comments (0)