【问题标题】:Is there a way for a packaged hosted chrome extension to request chrome updates it?有没有办法让打包的托管 chrome 扩展请求 chrome 更新?
【发布时间】:2013-06-05 15:53:16
【问题描述】:

通常,浏览器会每隔几个小时检查一次扩展程序更新,您可以通过点击“立即更新扩展程序”按钮手动覆盖它。我想做到这一点,并让我的扩展能够在可用的那一刻进行更新。通过 Ajax,它检查服务器的价值并知道有可用的更新。我想让他们告诉浏览器更新它。这可能吗?

这是一个在外部托管的打包扩展:http://developer.chrome.com/dev/extensions/hosting.html

更新:这是工作代码!

index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Hello World</title>
    <link href="styles/main.css" rel="stylesheet">
    <script src="test.js"></script>
</head>
<body>
    <h1>Hello, World 2 again 17!</h1>
</body>
</html>

test.js(监听文档点击并告诉后台更新的内容脚本)

window.onload = function()
{
    document.body.onclick = function()
    {
        chrome.runtime.requestUpdateCheck(function(status) {
                if (status == "update_available") {
                    console.log("update pending...");
                } else if (status == "no_update") {
                    console.log("no update found");
                } else if (status == "throttled") {
                    console.log("Oops, I'm asking too frequently - I need to back off.");
                }
        });
    }
}

main.js(后台脚本)

//You need to put reload in this callback
chrome.runtime.onUpdateAvailable.addListener(function(details) {
  console.log("updating to version " + details.version);
  chrome.runtime.reload();
});

【问题讨论】:

标签: google-chrome web-applications google-chrome-extension


【解决方案1】:

您是否尝试过使用chrome.runtime.requestUpdateCheck()?你的代码可能是这样的:

chrome.runtime.requestUpdateCheck(function(status,details){});
chrome.runtime.onUpdateAvailable(function(details){
    console.log('Updating to version: ' + details.version);
    chrome.runtime.reload();
});

【讨论】:

  • 谢谢你,我认为它可能有效。我这样做了,它工作了一次,但在我尝试过的所有其他时间,它都会打印到控制台它正在更新,但它永远不会更新。无论我尝试多少次,直到我通过单击“立即更新扩展”以手动方式进行操作。见我上面的代码。
  • 看到更新!控制台打印出它正在更新并在那里显示正确的版本,扩展页面显示它消失一秒钟然后重新出现,但作为旧版本!无论我尝试多少次,它都会继续这样做。但这意味着 Chrome IS 正在加载更新,而不是安装它。 (另外,我添加了一个 setTimeout 延迟,但它仍然不能解决这个问题)
  • 我相信我已经成功了!你的第二个建议是关键。我将重载放在chrome.runtime.onUpdateAvailable 监听器中,现在它可以工作了!
  • @DonRhummy 感谢您告诉我,我希望它会成功。
猜你喜欢
  • 1970-01-01
  • 2020-12-18
  • 2013-05-31
  • 1970-01-01
  • 2013-06-23
  • 2020-10-26
  • 1970-01-01
  • 2023-02-22
  • 2011-01-15
相关资源
最近更新 更多