Skip to content

Commit a3c1117

Browse files
authored
Merge pull request #963 from XMOJ-Script-dev/feat/periodic-cloud-sync
feat: Add hourly periodic cloud settings sync
2 parents 86b25a5 + 3c7f746 commit a3c1117

3 files changed

Lines changed: 40 additions & 2 deletions

File tree

Update.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3530,6 +3530,17 @@
35303530
}
35313531
],
35323532
"Notes": "No release notes were provided for this release."
3533+
},
3534+
"3.4.2": {
3535+
"UpdateDate": 1774706085890,
3536+
"Prerelease": true,
3537+
"UpdateContents": [
3538+
{
3539+
"PR": 963,
3540+
"Description": "feat: Add hourly periodic cloud settings sync"
3541+
}
3542+
],
3543+
"Notes": "Add hourly automatic cloud settings sync. Every hour, if CloudSync is enabled, settings are downloaded from the cloud and applied locally, then local settings are uploaded to the cloud. This keeps settings in sync across devices without requiring a visit to the settings page."
35333544
}
35343545
}
35353546
}

XMOJ.user.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ==UserScript==
22
// @name XMOJ
3-
// @version 3.4.1
3+
// @version 3.4.2
44
// @description XMOJ增强脚本
55
// @author @XMOJ-Script-dev, @langningchen and the community
66
// @namespace https://github/langningchen
@@ -560,6 +560,31 @@ let SyncSettingsToCloud = (CallBack) => {
560560
});
561561
};
562562

563+
let PeriodicCloudSync = () => {
564+
if (!CurrentUsername || !UtilityEnabled("CloudSync")) return;
565+
const lastSync = parseInt(localStorage.getItem("UserScript-CloudSync-LastSync") || "0");
566+
if (Date.now() - lastSync < 60 * 60 * 1000) return;
567+
RequestAPI("GetUserSettings", {}, (Response) => {
568+
if (Response.Success) {
569+
localStorage.setItem("UserScript-CloudSync-LastSync", String(Date.now()));
570+
const cloudSettings = (Response.Data && Response.Data.Settings) || {};
571+
if (Object.keys(cloudSettings).length > 0) {
572+
let themeChanged = false;
573+
for (let key in cloudSettings) {
574+
const rawValue = String(cloudSettings[key]);
575+
const localKey = "UserScript-Setting-" + key;
576+
if (localStorage.getItem(localKey) !== rawValue) {
577+
localStorage.setItem(localKey, rawValue);
578+
if (key === "Theme") themeChanged = true;
579+
}
580+
}
581+
if (themeChanged) initTheme();
582+
}
583+
SyncSettingsToCloud();
584+
}
585+
});
586+
};
587+
563588
unsafeWindow.GetContestProblemList = async function(RefreshList) {
564589
try {
565590
const contestReq = await fetch("https://www.xmoj.tech/contest.php?cid=" + SearchParams.get("cid"));
@@ -917,6 +942,8 @@ let initTheme = () => {
917942
}
918943
};
919944
initTheme();
945+
PeriodicCloudSync();
946+
setInterval(PeriodicCloudSync, 60 * 60 * 1000);
920947

921948

922949
class NavbarStyler {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "xmoj-script",
3-
"version": "3.4.1",
3+
"version": "3.4.2",
44
"description": "an improvement script for xmoj.tech",
55
"main": "AddonScript.js",
66
"scripts": {

0 commit comments

Comments
 (0)