如何自动重新加载Chrome扩展? [英] How to reload a chrome extension automatically?
问题描述
当你保存一个文件时,你必须刷新<$> c $ c> chrome:\\extensions 页面让浏览器重新加载扩展名。然后,您必须重新加载测试页,以查看文件的更改是否生效。
我是Chrome扩展开发的新手。有什么办法可以减少重复作品?我也很好奇铬扩展开发工作流程的最佳实践。
英语不好,请随时纠正。
扩展可以通过调用 chrome.runtime.reload()
,所以这是触发扩展的问题。
对我而言,一种方法是观察制表符 onUpdated
事件,并寻找特定的URL(您已经提出),例如 http:// localhost / reloadX?id = ...
。
这是示例代码放置在 background.js 中):
var myReloadURL ='http:// localhost / reloadX? id ='
+ chrome.i18n.getMessage('@@ extension_id');
chrome.tabs.onUpdated.addListener(函数(tabId,info,tab){
if(info.url === myReloadURL){
chrome.tabs.remove tabId);
chrome.runtime.reload();
}
});
其他权限(将在 manifest.json 中声明): p>
...
权限:[
...
标签,
http:// localhost / reloadX?id = *
myReloadURL
是任意的,可以是任何URL,不一定是真实的URL或资源将无法访问。
现在,为了重新加载您的扩展程序,您需要在Chrome中打开以下地址:
http:// localhost / reloadX?id =< ; your_extension_id>
您可以选择如何在保存时触发该功能。它可以是编辑器中的 on-save
钩子,一个自定义的grunt-watch任务(因为你似乎熟悉grunt)等等。
(顺便说一句,您不需要重新加载 chrome:// extensions
页面。重新加载扩展名就足够了。)
I am developing a chrome extension, and I found that there are repeating useless manual reloading works.
When you save a file, you have to refresh the chrome:\\extensions
page to let browser to reload the extension. And then you have to reload the test page to see if the changes to files take effect.
I am newbie to Chrome extension development. And are there any ways to reduce the repeating works? I am also curious that what is the best practice of chrome extension development workflow.
Poor English, feel free to correct.
The extension can reload itself, by calling chrome.runtime.reload()
, so it's a matter of triggering the extension to do it.
One method that worked for me, is to watch for tabs' onUpdated
event and look for a specific URL (you have come up with), e.g. http://localhost/reloadX?id=...
.
This is the sample code (to be placed in background.js):
var myReloadURL = 'http://localhost/reloadX?id='
+ chrome.i18n.getMessage('@@extension_id');
chrome.tabs.onUpdated.addListener(function(tabId, info, tab) {
if (info.url === myReloadURL) {
chrome.tabs.remove(tabId);
chrome.runtime.reload();
}
});
Additional permissions (to be declared in manifest.json):
...
"permissions": [
...
"tabs",
"http://localhost/reloadX?id=*"
myReloadURL
is arbitrary and can be any URL, just doesn't have to be a real URL or the resource will be rendered unreachable.
Now, in order to reload your extension, you need to open the following address in Chrome:
http://localhost/reloadX?id=<your_extension_id>
It is up to you to choose how to trigger that on save. It could be an on-save
hook in your editor, a custom grunt-watch task (since you seem to be familiar with grunt) etc .
(BTW, you don't need to reload the chrome://extensions
page. It suffices to reload the extension.)
这篇关于如何自动重新加载Chrome扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!