MCPcopy Create free account
hub / github.com/zxlie/FeHelper / checkVersionUpdate

Function checkVersionUpdate

apps/options/index.js:621–693  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

619
620 // 检查版本更新
621 async checkVersionUpdate() {
622 try {
623 // 获取已安装的版本号
624 const currentVersion = this.manifest.version;
625
626 // 尝试从本地存储获取最新版本信息,避免频繁请求
627 const cachedData = await new Promise(resolve => {
628 chrome.storage.local.get('fehelper_latest_version_data', data => {
629 resolve(data.fehelper_latest_version_data || null);
630 });
631 });
632
633 // 检查是否需要重新获取版本信息:
634 // 1. 缓存不存在
635 // 2. 缓存已过期(超过24小时)
636 // 3. 缓存的当前版本与实际版本不同(说明插件已更新)
637 const now = Date.now();
638 const cacheExpired = !cachedData || !cachedData.timestamp || (now - cachedData.timestamp > 24 * 60 * 60 * 1000);
639 const versionChanged = cachedData && cachedData.currentVersion !== currentVersion;
640
641 this.versionChecked = !(cacheExpired || versionChanged);
642 if (!this.versionChecked) {
643 try {
644 // 使用shields.io的JSON API获取最新版本号
645 const response = await fetch('https://img.shields.io/chrome-web-store/v/pkgccpejnmalmdinmhkkfafefagiiiad.json');
646 if (!response.ok) {
647 throw new Error(`HTTP错误:${response.status}`);
648 }
649 this.versionChecked = true;
650
651 const data = await response.json();
652 // 提取版本号 - shields.io返回的数据中包含版本信息
653 let latestVersion = '';
654 if (data && data.value) {
655 // 去掉版本号前的'v'字符(如果有)
656 latestVersion = data.value.replace(/^v/, '');
657 }
658
659 // 比较版本号
660 const needUpdate = this.compareVersions(currentVersion, latestVersion) < 0;
661
662 // 保存到本地存储中
663 await chrome.storage.local.set({
664 'fehelper_latest_version_data': {
665 timestamp: now,
666 currentVersion, // 保存当前检查时的版本号
667 latestVersion,
668 needUpdate
669 }
670 });
671
672 this.latestVersion = latestVersion;
673 this.needUpdate = needUpdate;
674 } catch (fetchError) {
675 // 获取失败时不显示更新按钮
676 this.needUpdate = false;
677
678 // 如果是版本变更导致的重新检查,但获取失败,则使用缓存数据

Callers

nothing calls this directly

Calls 3

setMethod · 0.80
resolveFunction · 0.70
getMethod · 0.45

Tested by

no test coverage detected