1- import axios from 'axios' ;
21import luxon from 'luxon' ;
32import * as settings from '../../utils/settings/index.js' ;
43import style from '../../utils/style.js' ;
@@ -10,7 +9,7 @@ import * as menu from '../../utils/menu.js';
109import semver from '../../utils/version.js' ;
1110import { buttonCSS , scriptVersion } from '../../utils/1.variables.js' ;
1211import css from '../../utils/css.js' ;
13- import { captureError } from '../../utils/sentry .js' ;
12+ import { createParser } from '../../utils/updater/index .js' ;
1413
1514// Check for script updates
1615wrap ( ( ) => {
@@ -30,21 +29,19 @@ wrap(() => {
3029 font-size : 17px ;
3130 }
3231 ` ) ;
33- const baseURL = 'https://unpkg.com/' ;
32+ const checker = window . checker = createParser ( { updateURL : 'UCProjects/UnderScript' } ) ;
3433 const MINUTE = 60 * 1000 ;
3534 const HOUR = 60 * MINUTE ;
3635 const CHECKING = 'underscript.update.checking' ;
3736 const LAST = 'underscript.update.last' ;
3837 const DEBUG = 'underscript.debug.update' ;
3938 const LATEST = 'underscript.update.latest' ;
40- const base = axios . create ( { baseURL } ) ;
4139 const latest = { // TODO: can I get away with storing this in sessionStorage?
42- set ( { version, unpkg } ) {
43- if ( ! version || ! unpkg ) return ;
40+ set ( { version, ... data } ) {
41+ if ( ! version ) return ;
4442 localStorage . setItem ( LATEST , JSON . stringify ( {
43+ ...data ,
4544 version,
46- unpkg,
47- time : Date . now ( ) ,
4845 } ) ) ;
4946 } ,
5047 del ( ) {
@@ -61,21 +58,22 @@ wrap(() => {
6158 let toast ;
6259 let updateToast ;
6360 let autoTimeout ;
64- function check ( ) {
65- if ( sessionStorage . getItem ( CHECKING ) ) return Promise . resolve ( ) ;
61+ async function check ( ) {
62+ if ( sessionStorage . getItem ( CHECKING ) ) return undefined ;
6663 sessionStorage . setItem ( CHECKING , true ) ;
67- return base . get ( `underscript@latest/package.json` ) . then ( ( response ) => {
68- sessionStorage . removeItem ( CHECKING ) ;
69- localStorage . setItem ( LAST , Date . now ( ) ) ;
70- return response ;
71- } ) . catch ( ( error ) => {
72- captureError ( error ) ;
73- sessionStorage . removeItem ( CHECKING ) ;
64+ try {
65+ const data = checker . getUpdateData ( ) ;
66+ return {
67+ version : await checker . getVersion ( data ) ,
68+ url : await checker . getDownload ( data ) ,
69+ } ;
70+ } catch ( error ) {
7471 debugToast ( error ) ;
75- if ( toast ) {
76- toast . setText ( 'Failed to connect to server.' ) ;
77- }
78- } ) ;
72+ toast ?. setText ( 'Failed to retrieve update.' ) ;
73+ return undefined ;
74+ } finally {
75+ sessionStorage . setItem ( CHECKING , false ) ;
76+ }
7977 }
8078 function noUpdateFound ( ) {
8179 const ref = toast ;
@@ -87,7 +85,6 @@ wrap(() => {
8785 function isNewer ( data ) {
8886 const version = scriptVersion ;
8987 if ( version === 'L' && ! localStorage . getItem ( DEBUG ) ) return false ;
90- if ( data . time && data . time < GM_info . script . lastModified ) return false ; // Check if stored version is newer than script date
9188 if ( version . includes ( '-' ) ) return semver ( data . version , version ) ; // Allow test scripts to update to release
9289 return data . version !== version ; // Always assume that the marked version is better
9390 }
@@ -98,36 +95,18 @@ wrap(() => {
9895 }
9996 latest . set ( data ) ;
10097 if ( updateToast ) updateToast . close ( 'stale' ) ;
101- const path = `underscript@${ data . version } /${ data . unpkg } ` ;
10298 updateToast = BasicToast ( {
10399 title : '[UnderScript] Update Available!' ,
104100 text : `Version ${ data . version } .` ,
105101 className : 'dismissable' ,
106102 buttons : [ {
107- text : 'Update (github)' ,
108- className : 'dismiss' ,
109- css : buttonCSS ,
110- } , {
111- text : 'Update (unpkg)' ,
112- className : 'dismiss' ,
113- css : buttonCSS ,
114- onclick ( e ) {
115- location . href = `${ baseURL } /${ path } ` ;
116- updateToast . close ( 'update' ) ;
117- } ,
118- } , {
119- text : 'Update (jsdelivr)' ,
103+ text : 'Update' ,
120104 className : 'dismiss' ,
121105 css : buttonCSS ,
122- onclick : ( e ) => {
123- location . href = `https://cdn.jsdelivr.net/npm/${ path } ` ;
124- updateToast . close ( 'update' ) ;
106+ onclick ( ) {
107+ location . href = data . url || `https://github.com/UCProjects/UnderScript/releases/download/${ data . version } /undercards.user.js` ;
125108 } ,
126109 } ] ,
127- onClose ( reason ) {
128- if ( reason !== 'dismissed' ) return ;
129- location . href = `https://github.com/UCProjects/UnderScript/releases/download/${ data . version } /undercards.user.js` ;
130- } ,
131110 } ) ;
132111 return true ;
133112 }
0 commit comments