How to aim a bug in Chromium browser extension that there won't be if installed locally in Dev mode

你离开我真会死。 提交于 2021-02-02 10:01:44

问题


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 Data\Default\Extensions\oecaicengbgemdbdklmajocogdjjgnda 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.

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.


回答1:


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

This is wrong because onInstalled is triggered only on installs/updates for a normal extension, but a non-persistent background script should register its listeners in the global context so that they are renewed every time the non-persistent background script is resumed.

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 in 5 seconds, which is its standard behavior.

TL;DR 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
  // ...............
});


来源:https://stackoverflow.com/questions/57472112/how-to-aim-a-bug-in-chromium-browser-extension-that-there-wont-be-if-installed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!