|
| 1 | +import logger from '../utils/logger.js'; |
| 2 | +import { parseUserScriptMetadata } from './metadataParser.js'; |
| 3 | + |
| 4 | +export class ScriptUpdater { |
| 5 | + constructor(storageApi) { |
| 6 | + this.storageApi = storageApi; |
| 7 | + } |
| 8 | + |
| 9 | + compareVersions(v1, v2) { |
| 10 | + if (!v1 || !v2) return 0; |
| 11 | + const parts1 = String(v1) |
| 12 | + .split(/[.-]/) |
| 13 | + .map((p) => parseInt(p, 10) || 0); |
| 14 | + const parts2 = String(v2) |
| 15 | + .split(/[.-]/) |
| 16 | + .map((p) => parseInt(p, 10) || 0); |
| 17 | + const len = Math.max(parts1.length, parts2.length); |
| 18 | + |
| 19 | + for (let i = 0; i < len; i++) { |
| 20 | + const p1 = parts1[i] || 0; |
| 21 | + const p2 = parts2[i] || 0; |
| 22 | + if (p1 > p2) return 1; |
| 23 | + if (p1 < p2) return -1; |
| 24 | + } |
| 25 | + return 0; |
| 26 | + } |
| 27 | + |
| 28 | + async checkAndPerformUpdates() { |
| 29 | + logger.info('Starting automatic script update check...'); |
| 30 | + const { scripts = [] } = await this.storageApi.local.get('scripts'); |
| 31 | + let anyUpdated = false; |
| 32 | + |
| 33 | + for (let i = 0; i < scripts.length; i++) { |
| 34 | + const script = scripts[i]; |
| 35 | + const updateUrl = |
| 36 | + script.updateURL || |
| 37 | + script.downloadURL || |
| 38 | + (script.updateInfo && script.updateInfo.updateUrl); |
| 39 | + |
| 40 | + if (!updateUrl) continue; |
| 41 | + |
| 42 | + try { |
| 43 | + logger.info(`Checking update for script: ${script.name} at ${updateUrl}`); |
| 44 | + const response = await fetch(updateUrl); |
| 45 | + if (!response.ok) { |
| 46 | + logger.warn(`Failed to fetch update for ${script.name}: ${response.status}`); |
| 47 | + continue; |
| 48 | + } |
| 49 | + |
| 50 | + const newCode = await response.text(); |
| 51 | + const newMetadata = parseUserScriptMetadata(newCode); |
| 52 | + |
| 53 | + if (!newMetadata.version) { |
| 54 | + logger.warn(`Could not find version in updated script ${script.name}`); |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | + if (this.compareVersions(newMetadata.version, script.version) > 0) { |
| 59 | + logger.info( |
| 60 | + `Updating script ${script.name} from ${script.version} to ${newMetadata.version}` |
| 61 | + ); |
| 62 | + scripts[i] = { |
| 63 | + ...script, |
| 64 | + ...newMetadata, |
| 65 | + code: newCode, |
| 66 | + updatedAt: new Date().toISOString(), |
| 67 | + }; |
| 68 | + |
| 69 | + if (newMetadata.updateURL) scripts[i].updateURL = newMetadata.updateURL; |
| 70 | + if (newMetadata.downloadURL) scripts[i].downloadURL = newMetadata.downloadURL; |
| 71 | + |
| 72 | + anyUpdated = true; |
| 73 | + } else { |
| 74 | + logger.info(`Script ${script.name} is up to date (${script.version})`); |
| 75 | + } |
| 76 | + } catch (error) { |
| 77 | + logger.error(`Error updating script ${script.name}:`, error); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if (anyUpdated) { |
| 82 | + await this.storageApi.local.set({ scripts }); |
| 83 | + chrome.runtime.sendMessage({ action: 'scriptsUpdated' }); |
| 84 | + logger.info('Automatic updates completed. Some scripts were updated.'); |
| 85 | + } else { |
| 86 | + logger.info('Automatic update check finished. No updates found.'); |
| 87 | + } |
| 88 | + |
| 89 | + await this.storageApi.local.set({ lastUpdateCheck: Date.now() }); |
| 90 | + } |
| 91 | + |
| 92 | + setupAlarm() { |
| 93 | + const ALARM_NAME = 'codetweak-auto-update'; |
| 94 | + chrome.alarms.onAlarm.addListener((alarm) => { |
| 95 | + if (alarm.name === ALARM_NAME) { |
| 96 | + this.checkAndPerformUpdates(); |
| 97 | + } |
| 98 | + }); |
| 99 | + |
| 100 | + chrome.alarms.get(ALARM_NAME, (alarm) => { |
| 101 | + if (!alarm) { |
| 102 | + chrome.alarms.create(ALARM_NAME, { |
| 103 | + periodInMinutes: 24 * 60, // Once a day |
| 104 | + }); |
| 105 | + } |
| 106 | + }); |
| 107 | + |
| 108 | + this.checkIfInitialCheckNeeded(); |
| 109 | + } |
| 110 | + |
| 111 | + async checkIfInitialCheckNeeded() { |
| 112 | + const { lastUpdateCheck } = await this.storageApi.local.get('lastUpdateCheck'); |
| 113 | + const now = Date.now(); |
| 114 | + const ONE_DAY = 24 * 60 * 60 * 1000; |
| 115 | + |
| 116 | + if (!lastUpdateCheck || now - lastUpdateCheck > ONE_DAY) { |
| 117 | + setTimeout(() => { |
| 118 | + this.checkAndPerformUpdates(); |
| 119 | + }, 5000); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments