javascript - 无论如何检查 Chrome 用户脚本中的更新?

标签 javascript google-chrome userscripts

需要说明的是:我不打算进行自动更新。我只想让用户知道有一个更新的脚本,并给他们一个链接,他们可以从那里获取最新版本。

在 Firefox 中有几种方法可以做到这一点,但它们似乎都使用了 Chrome 不支持的 Greasemonkey 特定功能。

我今天花了整整 4 个小时研究这个问题,现在我正在寻求 SO 社区的帮助。

最佳答案

感谢发帖者易江link在评论中。以防脚本消失,我将在此处发布内容:

// ==UserScript==
// @name           SelfUpdatingScript
// @version        1.0.0
// @namespace      Benjol (http://stackoverflow.com/users/11410/benjol)
// @description    Template script for a self-updating script
// @credits        Kudos to http://stackoverflow.com/users/115866/balpha
// @include        https://gist.github.com/*
// ==/UserScript==

function with_jquery(f) {
  var script = document.createElement("script");
  script.type = "text/javascript";
  script.textContent = "(" + f.toString() + ")(jQuery)";
  document.body.appendChild(script);
};

with_jquery(function ($) {
  $(function () {
    //how does this work?
    // 1. The installed script loads first, and sets the local VERSION variable with the currently installed version number
    // 2. window["selfUpdaterCallback:" + URL] is not defined, so this is skipped
    // 3. When updateCheck() is called, it defines window["selfUpdaterCallback:" + URL], which retains the installed version number in VERSION (closure)
    // 4. updateCheck() then loads the external version of the script into the page header
    // 5. when the external version of the script loads, it defines its own local VERSION with the external (potentially new) version number
    // 6. window["selfUpdaterCallback:" + URL] is now defined, so it is invoked, and the external version number is passed in
    // 7. if the external version number (ver) is greater than the installed version (VERSION), the notification is invoked
    var VERSION = 1.0;                                                         // than 1.13; if you mean 1.02, say so!)
    var URL = "https://gist.github.com/raw/874058/selfupdatingscript.user.js"; // VERSION)
          notifier(ver, VERSION, URL);
      }

      // make that a script tag. SO will not allow me to have a greater sign infront.
      /*$(' script />').attr("src", URL).appendTo("head");*/
    }

    // INSERT YOUR CUSTOM SCRIPT AFTER THIS COMMENT
    //Customize this code in the following ways
    //  - modify the callback to show your own custom notification
    //  - You could decide to check for updates less frequently that on every page load
    updateCheck(function (newver, oldver, url) {
      alert("A new version (" + newver + ", your current version is " + oldver + ") of the SelfUpdatingScript is available for download here: " + url);
    });
  });
});

关于javascript - 无论如何检查 Chrome 用户脚本中的更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5609286/

相关文章:

javascript - 使用参数作为变量名 (Javascript)

javascript - Accordion +/- 内容字符串标记不起作用

JavaScript 时钟适用于 Chrome,但不适用于 Firefox 或 IE

html - 如果部分元素由于滚动条而被隐藏,则 HTML5 拖放 DOM 元素的幻影图像会被裁剪

android - 如何在 chrome 上的新方案中将参数传递给 android Intent?

javascript - 为什么我不能使用我的用户脚本定位特定的 div

javascript - 语法验证器

javascript - 早午餐设置 LOGGY_STACKS=true

javascript - 我可以使用什么来代替 DOMSubtreeModified?

jquery - 如何根据内部 SPAN 内部的文本删除整个 DIV