diff --git a/package.json b/package.json index 32095fc07..dc63399f9 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "name": "4chan X Beta", "repo": "https://github.com/MayhemYDG/4chan-x/", "page": "https://4chan-x.just-believe.in/", + "buildsPath": "builds/", "mainBranch": "v3", "matches": [ "*://api.4chan.org/*", diff --git a/src/features.coffee b/src/features.coffee index 4549e58ca..e4ffc5d31 100644 --- a/src/features.coffee +++ b/src/features.coffee @@ -210,9 +210,10 @@ Settings = order: 110 open: -> Conf['Enable 4chan\'s Extension'] - unless $.get 'previousversion' + if (prevVersion = $.get 'previousversion', null) isnt g.VERSION + $.set 'lastupdate', Date.now() $.set 'previousversion', g.VERSION - $.on d, '4chanXInitFinished', Settings.open + $.on d, '4chanXInitFinished', Settings.open unless prevVersion Settings.addSection 'Main', Settings.main Settings.addSection 'Filter', Settings.filter diff --git a/src/main.coffee b/src/main.coffee index d6639bce2..317c7e7af 100644 --- a/src/main.coffee +++ b/src/main.coffee @@ -457,6 +457,7 @@ Main = Main.callbackNodes Post, posts $.event '4chanXInitFinished' + Main.checkUpdate() callbackNodes: (klass, nodes) -> # get the nodes' length only once @@ -480,13 +481,39 @@ Main = obj = e.detail unless typeof obj.callback.name is 'string' throw new Error "Invalid callback name: #{obj.callback.name}" - Klass = if obj.type is 'Post' - Post - else - Thread + switch obj.type + when 'Post' + Klass = Post + when 'Thread' + Klass = Thread + else + return obj.callback.isAddon = true Klass::callbacks.push obj.callback + checkUpdate: -> + return unless Main.isThisPageLegit() + # Check for updates after: + # - 6 hours since the last update on Opera because it lacks auto-updating. + # - 7 days since the last update on Chrome/Firefox. + # After that, check for updates every day if we still haven't updated. + now = Date.now() + freq = <% if (type === 'userjs') { %>6 * $.HOUR<% } else { %>7 * $.DAY<% } %> + if $.get('lastupdate', 0) > now - freq and $.get('lastchecked') > now - $.DAY + return + $.ajax '<%= meta.page %><%= meta.buildsPath %>version', onload: -> + return unless @status is 200 + version = @response + return unless /^\d\.\d+\.\d+$/.test version + if g.VERSION is version + # Don't check for updates too frequently if there wasn't one in a 'long' time. + $.set 'lastupdate', now + return + $.set 'lastchecked', now + el = $.el 'span', + innerHTML: "Update: <%= meta.name %> v#{version} is out, get it target=_blank>here." + new Notification 'info', el, 2 * $.MINUTE + handleErrors: (errors) -> unless 'length' of errors error = errors diff --git a/src/metadata.js b/src/metadata.js index 810a371bc..62ffec242 100644 --- a/src/metadata.js +++ b/src/metadata.js @@ -16,7 +16,7 @@ // @grant GM_deleteValue // @grant GM_openInTab // @run-at document-start -// @updateURL <%= meta.page %>builds/<%= name %>.meta.js -// @downloadURL <%= meta.page %>builds/<%= name %>.user.js +// @updateURL <%= meta.page %><%= meta.buildsPath %><%= name %>.meta.js +// @downloadURL <%= meta.page %><%= meta.buildsPath %><%= name %>.user.js // @icon data:image/png;base64,<%= grunt.file.read('img/icon48.png', {encoding: 'base64'}) %> // ==/UserScript==