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

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


当前回答

我正在使用快捷方式重新加载。我不想在保存文件时总是重新加载

所以我的方法是轻量级的,你可以保留重载函数

manifest.json

{
    ...
    "background": {
        "scripts": [
            "src/bg/background.js"
        ],
        "persistent": true
    },
    "commands": {
        "Ctrl+M": {
            "suggested_key": {
                "default": "Ctrl+M",
                "mac": "Command+M"
            },
            "description": "Ctrl+M."
        }
    },
    ...
}

src / bg /背景js。

chrome.commands.onCommand.addListener((shortcut) => {
    console.log('lets reload');
    console.log(shortcut);
    if(shortcut.includes("+M")) {
        chrome.runtime.reload();
    }
})

现在在chrome浏览器中按Ctrl + M重新加载

其他回答

更新:我已经添加了一个选项页面,这样你就不必手动查找和编辑扩展的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图标,例如:

BROWSER-SYNC

使用惊人的浏览器同步

更新浏览器(任何)当源代码改变(HTML, CSS,图像等) 支持Windows、MacOS和Linux操作系统 您甚至可以使用移动设备观看您的代码更新(实时)

MacOS上的安装(查看其他OS上的安装帮助)

安装NVM,这样您就可以尝试任何Node版本

brew install nvm             # install a Node version manager
nvm ls-remote                # list available Node versions
nvm install v10.13.0         # install one of them
npm install -g browser-sync  # install Browser-Sync

如何使用浏览器同步静态网站

让我们来看两个例子:

browser-sync start --server --files . --host YOUR_IP_HERE --port 9000

browser-sync start --server --files $(ack --type-add=web:ext:htm,html,xhtml,js,css --web -f | tr \\n \ ) --host $(ipconfig getifaddr en0) --port 9000

——server选项允许您在终端的任何地方运行本地服务器,而——files选项允许您指定将跟踪哪些文件的更改。我更喜欢对跟踪的文件更具体,因此在第二个示例中,我使用ack来列出特定的文件扩展名(重要的是这些文件没有带空格的文件名),并使用useipconfig来查找我在MacOS上的当前IP。

如何使用浏览器同步动态网站

如果您正在使用PHP、Rails等,您已经有一个正在运行的服务器,但是当您更改代码时它不会自动刷新。所以你需要使用——proxy开关让browser-sync知道该服务器的主机在哪里。

browser-sync start --files $(ack --type-add=rails:ext:rb,erb,js,css,sass,scss,coffee --rails -f | tr \\n \ ) --proxy 192.168.33.12:3000 --host $(ipconfig getifaddr en0) --port 9000 --no-notify --no-open

在上面的例子中,我已经在浏览器的192.168.33.12:3000上运行了一个Rails应用程序。它实际上运行在使用Vagrant盒子的虚拟机上,但我可以使用该主机上的端口3000访问虚拟机。我喜欢——no-notify来停止浏览器同步,每次我修改代码时,都会在浏览器上发送通知警报,——no-open来停止浏览器同步行为,当服务器启动时立即加载浏览器选项卡。

重要提示:如果您正在使用Rails,请避免在开发中使用Turbolinks,否则在使用——proxy选项时将无法单击链接。

希望对别人有用。我尝试了许多技巧来刷新浏览器(甚至是我之前使用AlfredApp提交的关于这个StackOverflow问题的旧帖子),但这是真正的方法;没有更多的黑客,它只是流动。

CREDIT:用一个命令启动本地实时重载web服务器

你可以使用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上使用。

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

在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);
})();

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

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

感谢@GmonC和@Arik和一些空闲时间,我设法让它工作。我必须更改两个文件才能使其工作。

(1)为该应用程序安装LiveReload和Chrome扩展。 这将在文件更改时调用一些脚本。

打开<LiveReloadInstallDir>\ wrapped \backend\res\livereload.js

(3)将509行改为

This.window.location.href = "http://reload.extensions";

(4)现在安装另一个扩展Extensions Reloader,它有有用的链接处理程序,重新加载所有的开发扩展导航到“http://reload.extensions”

现在以这种方式更改扩展的background.min.js

if ((d.installType = = "发展")(&& d.enabled = = true) && (d.name != Reloader扩展”))

替换为

if((d.installType=="development")&&(d.enabled==true)&&(d.name!="Extensions Reloader")&&(d.name!="LiveReload"))

打开LiveReload应用程序,隐藏扩展重新加载按钮,并通过单击工具栏中的按钮激活LiveReload扩展,您现在将重新加载页面和每个文件更改扩展,同时使用所有其他好东西从LiveReload (css重新加载,图像重新加载等)

唯一不好的是,你将不得不重复在每次扩展更新上更改脚本的过程。为避免更新,请将扩展添加为未打包的扩展。

当我有更多的时间来处理这个时,我可能会创建一个扩展,消除对这两个扩展的需求。

在那之前,我正在做我的扩展Projext Axeman