Skip to content

Commit

Permalink
feat: add automatic update checker
Browse files Browse the repository at this point in the history
- Add GitHub releases API integration
- Add notification system for new versions
- Check for updates daily
- Allow one-click access to latest release
  • Loading branch information
LightL99 committed Nov 6, 2024
1 parent 6eacdff commit 661697f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
39 changes: 39 additions & 0 deletions background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const GITHUB_REPO = "fangyuan99/cookie-share";
const CHECK_INTERVAL = 24 * 60 * 60 * 1000; // 24 hours

// 检查更新函数
async function checkForUpdates() {
try {
const response = await fetch('https://api.github.com/repos/fangyuan99/cookie-share/releases/latest');
const data = await response.json();

const currentVersion = chrome.runtime.getManifest().version;
const latestVersion = data.tag_name.replace("v", "");

return {
hasUpdate: latestVersion > currentVersion,
currentVersion,
latestVersion,
releaseUrl: data.html_url,
};
} catch (error) {
console.error("Error checking for updates:", error);
return {
hasUpdate: false,
currentVersion: chrome.runtime.getManifest().version,
error: error.message,
};
}
}

// 导出函数供popup使用
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "checkUpdate") {
checkForUpdates().then(sendResponse);
return true; // 保持消息通道开启
}
});

// 启动时检查一次,然后每24小时检查一次
checkForUpdates();
setInterval(checkForUpdates, CHECK_INTERVAL);
36 changes: 36 additions & 0 deletions popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ document.addEventListener("DOMContentLoaded", function () {
const errorMessageDiv = document.getElementById("errorMessage");
const cookieIdInput = document.getElementById("cookieId");
const customUrlInput = document.getElementById("customUrl");
const currentVersionSpan = document.getElementById("currentVersion");
const updateCheckSpan = document.getElementById("updateCheck");

sendButton.addEventListener("click", handleSendCookies);
receiveButton.addEventListener("click", handleReceiveCookies);
Expand Down Expand Up @@ -211,4 +213,38 @@ document.addEventListener("DOMContentLoaded", function () {
});
});
}

// 显示当前版本
const manifest = chrome.runtime.getManifest();
currentVersionSpan.textContent = `v${manifest.version}`;

// 检查更新函数
function checkForUpdates() {
updateCheckSpan.textContent = "Checking...";
chrome.runtime.sendMessage({ action: "checkUpdate" }, (response) => {
if (response.error) {
updateCheckSpan.textContent = "Check for updates";
showError("Failed to check for updates: " + response.error);
return;
}

if (response.hasUpdate) {
updateCheckSpan.classList.add("update-available");
updateCheckSpan.textContent = `Update available: v${response.latestVersion}`;
updateCheckSpan.onclick = () => {
chrome.tabs.create({ url: response.releaseUrl });
};
showMessage(`New version ${response.latestVersion} is available!`);
} else {
updateCheckSpan.textContent = "Check for updates";
showMessage("You have the latest version!");
}
});
}

// 添加更新检查点击事件
updateCheckSpan.addEventListener("click", checkForUpdates);

// 初始检查更新
checkForUpdates();
});

0 comments on commit 661697f

Please sign in to comment.