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

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


当前回答

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

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

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

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

其他回答

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

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

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

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

正如文档中提到的:下面的命令行将重新加载应用程序

/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --load-and-launch-app=[path to the app ]

所以我只是创建了一个shell脚本,并从gulp调用该文件。超级简单:

var exec = require('child_process').exec;



gulp.task('reload-chrome-build',function(cb){

console.log("reload");

var cmd="./reloadchrome.sh"

exec(cmd,function (err, stdout, stderr) {
    console.log("done: "+stdout);

    cb(err);
    }
);});

在脚本上运行必要的监视命令,并在需要时调用重载任务。干净,简单。

感谢@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

这里有一个函数,您可以使用它来监视文件的更改,并在检测到更改时重新加载。它通过AJAX轮询它们,并通过window.location.reload()重新加载。我想你不应该在分发包中使用这个。

function reloadOnChange(url, checkIntervalMS) {
    if (!window.__watchedFiles) {
        window.__watchedFiles = {};
    }

    (function() {
        var self = arguments.callee;
        var xhr = new XMLHttpRequest();

        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4) {
                if (__watchedFiles[url] &&
                    __watchedFiles[url] != xhr.responseText) {
                    window.location.reload();
                } else {
                    __watchedFiles[url] = xhr.responseText
                    window.setTimeout(self, checkIntervalMS || 1000);
                }
            }
        };

        xhr.open("GET", url, true);
        xhr.send();
    })();
}

reloadOnChange(chrome.extension.getURL('/myscript.js'));

使用npm init创建一个包。Json在根目录下

NPM install——save-dev gulp-open &&

然后创建gulpfile.js

它看起来像:

/* File: gulpfile.js */

// grab our gulp packages
var gulp  = require('gulp'),
    open = require('gulp-open');

// create a default task and just log a message
gulp.task('default', ['watch']);

// configure which files to watch and what tasks to use on file changes
gulp.task('watch', function() {
  gulp.watch('extensionData/userCode/**/*.js', ['uri']);
});

gulp.task('uri', function(){
  gulp.src(__filename)
  .pipe(open({uri: "http://reload.extensions"}));
});

这适用于我开发CrossRider,你可能会观察改变路径,你看文件在,也假设你有npm和节点安装。