我想为我的chrome扩展重新加载每次我保存在扩展文件夹中的文件,而不必显式点击“重新加载”在chrome://extensions/。这可能吗?

编辑:我知道我可以更新Chrome重新加载扩展的间隔,这是一个中途的解决方案,但我宁愿让我的编辑器(emacs或textmate)触发保存重新加载或要求Chrome监控目录的变化。


当前回答

更新:我已经添加了一个选项页面,这样你就不必手动查找和编辑扩展的ID了。CRX和源代码在:https://github.com/Rob--W/Chrome-Extension-Reloader 更新2:添加快捷方式(见我的仓库在Github)。 包含基本功能的原始代码如下所示。


创建一个扩展,并使用Browser Action方法与chrome.extension.management API一起重新加载已解包的扩展。

下面的代码为Chrome添加了一个按钮,点击后将重新加载扩展。

manifest.json

{
    "name": "Chrome Extension Reloader",
    "version": "1.0",
    "manifest_version": 2,
    "background": {"scripts": ["bg.js"] },
    "browser_action": {
        "default_icon": "icon48.png",
        "default_title": "Reload extension"
    },
    "permissions": ["management"]
}

bg.js

var id = "<extension_id here>";
function reloadExtension(id) {
    chrome.management.setEnabled(id, false, function() {
        chrome.management.setEnabled(id, true);
    });
}
chrome.browserAction.onClicked.addListener(function(tab) {
    reloadExtension(id);
});

icon48.png:选择一个漂亮的48x48图标,例如:

其他回答

是的,你可以间接地做!这是我的解决方案。

在manifest.json

{
    "name": "",
    "version": "1.0.0",
    "description": "",
    "content_scripts":[{
        "run_at":"document_end",
        "matches":["http://*/*"],
        "js":["/scripts/inject.js"]
    }]
}

在inject.js

(function() {
    var script = document.createElement('script'); 
    script.type = 'text/javascript'; 
    script.async = true;
    script.src = 'Your_Scripts';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(script, s);
})();

注入的脚本可以从任何位置注入其他脚本。

这种技术的另一个好处是你可以忽略孤立世界的限制。参见内容脚本执行环境

我已经分叉LiveJS,以允许实时重新加载打包的应用程序。只需将文件包含在应用程序中,每次保存文件时,应用程序将自动重载。

我主要在Firefox中开发,其中web-ext运行会在文件更改后自动重新加载扩展。一旦准备就绪,我会在Chrome中进行最后一轮测试,以确保没有Firefox中没有出现的任何问题。

如果你想主要在Chrome上开发,并且不想安装任何第三方扩展,那么另一个选择是在扩展的文件夹中创建一个test.html文件,并添加一堆SSCCE。然后,该文件使用一个正常的<script>标记来注入扩展脚本。

您可以使用它进行95%的测试,然后当您想在现场测试它时手动重新加载扩展。

这并不能完全重现扩展所运行的环境,但对于许多简单的事情来说已经足够好了。

你可以使用Chrome的“Extensions Reloader”:

Reloads all unpacked extensions using the extension's toolbar button or by browsing to "http://reload.extensions" If you've ever developed a Chrome extension, you might have wanted to automate the process of reloading your unpacked extension without the need of going through the extensions page. "Extensions Reloader" allows you to reload all unpacked extensions using 2 ways: 1 - The extension's toolbar button. 2 - Browsing to "http://reload.extensions". The toolbar icon will reload unpacked extensions using a single click. The "reload by browsing" is intended for automating the reload process using "post build" scripts - just add a browse to "http://reload.extensions" using Chrome to your script, and you'll have a refreshed Chrome window.

更新:截至2015年1月14日,扩展是开源的,可在GitHub上使用。

我已经做了一个简单的可嵌入脚本热重载:

https://github.com/xpl/crx-hotreload

它监视扩展名目录中的文件更改。当检测到更改时,它重新加载扩展并刷新活动选项卡(以重新触发更新的内容脚本)。

通过检查文件的时间戳来工作 支持嵌套目录 在生产配置中自动禁用自身