已发布的扩展程序不适用于 onInstalled 内的 chrome API 侦听器 [英] Published extension doesn't work with chrome API listeners inside onInstalled

查看:15
本文介绍了已发布的扩展程序不适用于 onInstalled 内的 chrome API 侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚测试了我的 Chrome 插件 https://chrome.google.com/webstore/detail/soft-screen/oecaicengbgemdbdklmajocogdjjgnda正式安装后,即通过 CWS(Chrome 网上应用店),但突然惊讶它不起作用.我一直通过在开发模式下在本地安装它来使用它,即将安装并位于 {The Chrome directory}User DataDefaultExtensionsoecaicengbgemdbdklmajocogdjjgnda 的插件复制到另一个目录,然后重新安装通过将其拖到 chrome://extension URL(在开发者模式开启时).然后我不得不从它的 manifest.json 中删除 key,然后禁用通过 CWS 安装的第一个.我可以在本地开发者模式和 CWS 扩展之间切换扩展,以防止同时安装和启用两个相同的扩展.

I've just tested my Chrome addon https://chrome.google.com/webstore/detail/soft-screen/oecaicengbgemdbdklmajocogdjjgnda after installing it officially, i.e. via CWS (Chrome Web Store), but was suddenly surprised that it's not working. I've always used it by installing it locally in Dev mode, i.e. by copying the addon installed and located at {The Chrome directory}User DataDefaultExtensionsoecaicengbgemdbdklmajocogdjjgnda to another directory and then reinstall it by dragging it to the chrome://extension URL (while Developer mode is on). I then had to remove the key from its manifest.json and then disable the first one that was installed via CWS. I could toggle the extension between local Developer mode and CWS extension to prevent two conflicting identical extensions installed and enabled at once.

区别非常明显,它在使用 CWS(添加到 Chrome")安装时不起作用,但它只是在本地开发人员模式下工作.

The difference is very conspicuous in which it does not work in using the CWS ("Add to Chrome") installation, but it simply works in local Developer mode.

你能像我一样帮助我吗?希望你比我更有见识,以便我能够修复错误并解决问题.

Can you please help me out by doing the same as I did and may you be more knowledgeable than me so that I will be able to fix the bug and solve the problem.

推荐答案

这是一个常见问题:您正在使用非持久性后台脚本(manifest.xml 中的persistent":false.json) 和一个 chrome.runtime.onInstalled 侦听器,您可以在其中注册其他一些侦听器.

This is a common problem: you're using a non-persistent background script ("persistent":false in manifest.json) and a chrome.runtime.onInstalled listener where you register some other listeners.

这是错误的,因为 onInstalled 事件仅在已发布扩展的安装/更新时触发,并且它异步发生,这是一个问题,因为必须注册 chrome API 侦听器同步当使用非持久化后台脚本时,否则唤醒后台脚本的事件不会被分派到这个监听器,即事件将丢失.

This is wrong because onInstalled event is triggered only on installs/updates for a published extension and it happens asynchronously, which is a problem because chrome API listeners must be registered synchronously when using a non-persistent background script, otherwise the event that woke the background script won't be dispatched to this listener i.e. the event will be lost.

它在开发者模式下对你有用只是因为重新加载扩展被 Chrome 视为 onInstalled 中报告的更新事件.或者,您可能为后台脚本打开了 devtools,这阻止了它自动卸载.

It worked for you in developer mode only because reloading an extension is treated by Chrome as an update event reported in onInstalled. Or maybe you had devtools open for your background script which prevented it from unloading automatically.

TL;DR 解决方案:

TL;DR Solution:

将 chrome.browserAction.onClicked 注册移出 chrome.runtime.onInstalled:

Move chrome.browserAction.onClicked registration out of chrome.runtime.onInstalled:

chrome.runtime.onInstalled.addListener(function(info) {
  // code that should run on install/update
  // ...............
});

chrome.browserAction.onClicked.addListener(function(tab) {
  // code that should run on clicking the extension icon
  // ...............
});

这篇关于已发布的扩展程序不适用于 onInstalled 内的 chrome API 侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆