Google Chrome:在“开发者模式”下更新扩展程序(本地)来自userscript [英] Google Chrome: updating extension in "Developer mode" (locally) from userscript

查看:163
本文介绍了Google Chrome:在“开发者模式”下更新扩展程序(本地)来自userscript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为Google Chrome创建了一个扩展,它为指定的网页运行一个小的userscript,并通过 background.js 向chrome API(如chrome.tabs和其他)发送一些请求。

现在,我在开发者模式中进行本地测试,如下所示:


  1. 我编辑我的 userscript.js background.js ;

  2. 然后我转到 chrome:// Google Chrome浏览器扩展程序标签,然后从本地 D:\ my-extension 文件夹中手动重新加载(CTR + R)我的扩展程序。最后,我切换到带有指定网页的选项卡,并重新加载它以查看更改。

问题是:

因为我是初学者,所以我的 userscript.js background.js 在我的扩展和userscript开始按预期工作。
所以,这个过程在测试时涉及扩展和页面标签之间的单调切换。

这个想法是:

在页面上添加一个按钮或一个快捷键,在我的用户脚本里有一个'update_extension_and_reload_page'函数,这样当我点击这个按钮时,它会从本地文件夹D:\my-extension 调用扩展更新,然后进行页面重新加载。 (另一种选择是将这种'update_extension_and_reload_page'函数分配给扩展的browser_action图标。)

现在,我只是感兴趣:

is there任何类型的chrome.extesion.update方法,以便我可以通过 background.js 从我的 userscript.js 发出请求,并调用自动重新加载扩展名(在开发者模式中),而无需转到 chrome:// extensions 标签。

解决方案

扩展可以通过调用 chrome.runtime.reload() ,所以这是触发扩展的问题。



下面的代码将以下功能附加到浏览器操作按钮上:


  1. 跟踪
  2. 重新加载扩展程序。

  3. 重新加载活动标签。

manifest.json

  ... 
browser_action:{
default_title:重新加载
//default_icon:{
//19:img / icon19.png,
// 38:img / icon38.png
//},
},
...

background.js

  chrome.browserAction.onClicked .addListener(函数(标签){
localStorage。 tabToReload = tab.id;
chrome.runtime.reload();
});

函数reloadTab(){
var tabID = localStorage.tabToReload;
if(tabID){
chrome.tabs.reload(parseInt(tabID));
delete(localStorage.tabToReload);
}
}
reloadTab();
...






另请参阅, 此答案 自动完成重新加载过程。


I created an extension for Google Chrome which runs a small userscript for designated web-page and makes some requests via background.js to the chrome APIs (such chrome.tabs and others).

now, I'm testing it locally in "Developer mode" like this:

  1. I edit my userscript.js and background.js;
  2. then I go to chrome://extensions tab in Google Chrome and manually reload (CTR+R) my extension from local D:\my-extension folder.
  3. finally, I switch to the tab with designated web-page and reload it to see the changes.

the problem is:
since I'm a beginner, a lot of changes have to be done to my userscript.js and background.js before my extension and userscript start working as expected. so, this process involves a lot of monotonous switching between extensions and page tabs while testing.

the idea is:
it would be nice to add a button or a shortcut key on the page where the userscript is being tested and attach a sort of 'update_extension_and_reload_page' function to it in my userscript, so that evey time when I click this button it will call extension update from the local folder D:\my-extension then followed by page reload. (another alternative would be to assign such 'update_extension_and_reload_page' function to extension's browser_action icon.)

now, I'm just interested:
is there any sort of 'chrome.extesion.update' method, so that I could make a request to it from my userscript.js via background.js and call automatic reload of the extension (in "Developer mode") without need to go to the chrome://extensions tab.

解决方案

The extension can reload itself, by calling chrome.runtime.reload(), so it's a matter of triggering the extension to do it.

The code below attaches the following functionality to the browser-action button:

  1. Keeps track of the active tab.
  2. Reloads the extension.
  3. Reloads the active tab.

manifest.json

...
"browser_action": {
    "default_title": "Reload"
//    "default_icon": {
//        "19": "img/icon19.png",
//        "38": "img/icon38.png"
//    },
},
...

background.js

chrome.browserAction.onClicked.addListener(function (tab) {
    localStorage.tabToReload = tab.id;
    chrome.runtime.reload();
});

function reloadTab() {
    var tabID = localStorage.tabToReload;
    if (tabID) {
        chrome.tabs.reload(parseInt(tabID));
        delete(localStorage.tabToReload);
    }
}
reloadTab();
...


See, also, this answer on how to automate the re-loading process.

这篇关于Google Chrome:在“开发者模式”下更新扩展程序(本地)来自userscript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆