Javascript 在chrome扩展的background.JS中使用npm运行build调用JS函数

Javascript 在chrome扩展的background.JS中使用npm运行build调用JS函数,javascript,npm,google-chrome-extension,Javascript,Npm,Google Chrome Extension,我试图在开发chrome扩展时自动化我的工作流程,而现在我却陷入了在浏览器中自动重新加载扩展的困境。我试图在每次构建之后自动运行它 在background.js中 function extensionReload() { chrome.tabs.query({ active: true, currentWindow: true }, tabs => { // NB: see https://github.com/xpl/crx-hotreload/issues/5 i

我试图在开发chrome扩展时自动化我的工作流程,而现在我却陷入了在浏览器中自动重新加载扩展的困境。我试图在每次构建之后自动运行它

在background.js中

function extensionReload() {
  chrome.tabs.query({ active: true, currentWindow: true }, tabs => {
    // NB: see https://github.com/xpl/crx-hotreload/issues/5

    if (tabs[0]) {
      chrome.tabs.reload(tabs[0].id);
    }

    chrome.runtime.reload();
  });
}; 
export { extensionReload }; //export as module. Not sure if necessary
在package.json中

"scripts": {
    "reload-extension":  "node background.js extensionReload",
    "build-script": "gulp background && gulp content && gulp popup && gulp manifest && gulp third-parties",
    "build": "npm run build-script && npm run build-css-chrome && npm run build-css-firefox && npm run reload-extension"
} // I have left some things out for faster read
但是当我运行它时,我得到以下错误:

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! browser-ext@1.0.0 reload-extension: `node background.js extensionReload`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the browser-ext@1.0.0 reload-extension script.

我对整个JS世界都很陌生,所以我猜我有一个实现错误,我似乎无法弄清楚。

扩展脚本只有在浏览器中的特殊扩展页面中运行时才能访问
chrome
API,例如背景页面中有内部chrome扩展名://URL,但
node
不能那样做。为自动监视文件更改的扩展名查找并使用热重新加载包。extensionReload()函数从监视文件更改的热重新加载包中取出。问题在于,很难对重新加载进行正确计时,因为根据可用资源的多少,构建需要不同的时间。有时,在构建完成之前会触发重新加载。我希望能够作为其他“npm运行构建”命令的一部分触发该函数,以便它能够自动正确计时。不会工作。您可以调整重新加载包,使其仅在特殊文件位于可配置位置时进行。OTOH我可能只需要添加一个文件来手动执行,因为在编辑一些脚本(如弹出脚本)后不需要重新加载。感谢wOxxOm的解释和想法!我将配置一个热键使其快速工作,然后在稍后的实践中实现您的其他想法。祝你度过愉快的一天,保持安全!