2011-04-10 41 views
3

只是要清楚:我不想做自動更新。我只是想讓用戶知道有一個更新的腳本,並給他們一個鏈接,他們可以去獲取最新版本。無論如何檢查Chrome用戶腳本中的更新?

有幾種方法可以在Firefox中這樣做,但它們似乎都使用Chrome中不支持的Greasemonkey特定功能。

今天我花了4個小時研究這個問題,現在我正在看SO社區尋求幫助。

+3

https://gist.github.com/874058 – 2011-04-10 02:11:03

+0

@Yi蔣我剛纔看到您的評論......這正是我一直在尋找。 – LoveGandhi 2011-04-11 18:08:36

+0

@Yi Jiang如果您將此作爲答案發布,我很樂意將此標記爲答案。 – LoveGandhi 2011-04-11 18:20:00

回答

3

謝謝姜毅誰在評論發表在這個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); 
    }); 
    }); 
}); 
+1

什麼是$(「」)。attr(「src」,URL).appendTo(「head」);'幹嘛? – qwertymk 2011-04-18 04:31:58

+0

@qwertymk這是一個很好的捕獲。 SO不允許我製作$("

2

我剛剛發現了在Chrome上運行的greasefire更新腳本。不知道它會有幫助。 http://code.google.com/p/greasefire/source/browse/chrome/updater.js?r=55

+0

如果我錯了,請糾正我,但這似乎是擴展更新程序的一部分。我正在尋找在用戶腳本中進行更新。 – LoveGandhi 2011-04-10 02:33:22

+0

這是一回事。在chrome中使用時,chrome擴展名和greasemonkey腳本之間沒有區別。 GM腳本被轉換爲擴展名,然後被安裝。然後它們出現在您的擴展列表中。 – 2011-04-10 21:40:43

相關問題