Notify users of available updates. #968

This commit is contained in:
Nicolas Stepien 2013-03-28 03:10:50 +01:00
parent 60737bd33e
commit 57d65aa22a
4 changed files with 37 additions and 8 deletions

View File

@ -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/*",

View File

@ -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

View File

@ -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 <a href=<%= meta.page %> target=_blank>here</a>."
new Notification 'info', el, 2 * $.MINUTE
handleErrors: (errors) ->
unless 'length' of errors
error = errors

View File

@ -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==