Checking for userscript update

Script exchange and development requests
Post Reply
janb
Posts: 15
Joined: 29.06.17 18:20:11

Checking for userscript update

Post by janb » 28.10.17 17:13:19

My userscript was downloaded quite some times after I announced it in a forum. But some issues came up, so I'm working on a follow-up. The problem is, I have no way to communicate to all current users. Most of them probably never use the manual "Check for userscript updates" function. So, I learned my lesson: in the next update and in other future projects I'd like to add a function that informs all users when a newer version is available.

Question: is there a way to let a userscript check itself if an updated version is available? Or could I invoke the "Check for userscript updates" from my userscript?
Last edited by janb on 28.10.17 22:26:35, edited 1 time in total.

janb
Posts: 15
Joined: 29.06.17 18:20:11

Re: Checking for userscript update

Post by janb » 28.10.17 21:57:21

I made a working example. This script (version 0.1) checks for the latest version number at pastebin.com (which is 0.2).
To be solved:
- The full script gets downloaded every time, only to read one variable...
- Checking too often could cause problems, maybe once or twice a week and storing the result?

Code: Select all

// ==UserScript==
// @name         Update Test
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        http://*/*
// @grant        GM_getResourceText
// @resource     latestScript https://pastebin.com/raw/FgpEtqqq
// @updateURL    https://pastebin.com/raw/FgpEtqqq
// ==/UserScript==
 
(function() {
    'use strict';
    var checkUpdate = function() {
        var currentVersion = GM_info.script.version;
        var latestScript = GM_getResourceText("latestScript");
        var myRegex = new RegExp('(\/\/ \@version +)([.0-9a-z]+)','igm');
        var availableVersion = myRegex.exec(latestScript);
        var updateURL = GM_info.script.updateURL;
        console.log("Current version: " + currentVersion);
        if (availableVersion[2] !== currentVersion) {
            console.log("Update available: " + availableVersion[2]);
        }
    };
    checkUpdate();
})();

User avatar
derjanb
Site Admin
Posts: 2326
Joined: 20.01.06 02:16:15
Contact:

Re: Checking for userscript update

Post by derjanb » 30.10.17 10:21:50

It's not recommended to implement your own update check since Tampermonkey already has one. Furthermore scripts are checked for updates once a day, while @resources are checked only every 7 days (and some seconds after modifying a script).
ImageIf you like what I'm doing, please consider a donation here. Image

janb
Posts: 15
Joined: 29.06.17 18:20:11

Re: Checking for userscript update

Post by janb » 30.10.17 11:16:27

Thanks for your comment, which doesn't come as a big surprise. :wink:

I will happily skip this script if you can suggest an alternative to reach my goal: let the userscript tell ALL users when it has become outdated – even when the TM update check is disabled. Something similar to the TM announcements for a TM update.

janb
Posts: 15
Joined: 29.06.17 18:20:11

Re: Checking for userscript update

Post by janb » 17.11.17 01:18:22

derjanb wrote:
30.10.17 10:21:50
...while @resources are checked only every 7 days (and some seconds after modifying a script).
To tackle this, I switched to GM_xmlhttpRequest. By the @connect line the pastebin.com domain is allowed to be retrieved by GM_xmlhttpRequest. Instead of retrieving the full script it only checks a small paste containing the version number, nothing more. That's why the regex is no longer necessary. The script works even when update checks are disabled in TM settings.

Updating is not really possible with this demo version: the @updateURL is not there. Also, to prevent unnecessary activity, my final script will check only once a day by storing the date with GM_setValue.

Code: Select all

// ==UserScript==
// @name         Update Test
// @namespace    http://tampermonkey.net/
// @version      0.2
// @match        http://*/*
// @grant        GM_xmlhttpRequest
// @connect      pastebin.com
// @updateURL    Your updateURL here...
// ==/UserScript==

(function() {
    'use strict';
    var checkUpdate = function() {
        var currentVersion = GM_info.script.version;
        GM_xmlhttpRequest ( {
            method:     'GET',
            url:        'https://pastebin.com/raw/ZvEMci4M',
            onload:     function (responseDetails) {
                // DO ALL RESPONSE PROCESSING HERE...
                var availableVersion = responseDetails.responseText;
                if (availableVersion !== currentVersion) {
                    alert("Update available: " + availableVersion + ". Please select 'Check for userscript updates' in the Tampermonkey menu.");
                }
            }
        } );
    };
    checkUpdate();
})();

janb
Posts: 15
Joined: 29.06.17 18:20:11

Re: Checking for userscript update

Post by janb » 16.12.17 15:12:56

derjanb wrote:
30.10.17 10:21:50
It's not recommended to implement your own update check since Tampermonkey already has one.
Update: in the end I skipped my own update check, because of a lucky coincidence: since the original website changed page structure (which broke my user script) users had to search for updates anyhow. :)

Now with @updateURL new script updates won't be missed anymore. I am very happy with the update checks by TM! :mrgreen:
I hope that in a future TM version the update message will include:
  • The update version number
  • A "read more" link to the @supportURL (if provided), with info about changes.

Post Reply

Return to “Userscripts”

Who is online

Users browsing this forum: No registered users and 1 guest