From dd706934e0c0af8719515e42ef920e3118723bae Mon Sep 17 00:00:00 2001 From: Zhu Chenrui Date: Tue, 19 Aug 2025 14:26:06 +0800 Subject: [PATCH 1/4] feat: use credential management api --- XMOJ.user.js | 51 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/XMOJ.user.js b/XMOJ.user.js index 7b47f15e..2d3f8c1a 100644 --- a/XMOJ.user.js +++ b/XMOJ.user.js @@ -441,6 +441,35 @@ let UtilityEnabled = (Name) => { } } }; +let storeCredential = async (username, password) => { + if ('credentials' in navigator && window.PasswordCredential) { + try { + const credential = new PasswordCredential({ id: username, password: password }); + await navigator.credentials.store(credential); + } catch (e) { + console.error(e); + } + } +}; +let getCredential = async () => { + if ('credentials' in navigator && window.PasswordCredential) { + try { + return await navigator.credentials.get({ password: true, mediation: 'optional' }); + } catch (e) { + console.error(e); + } + } + return null; +}; +let clearCredential = async () => { + if ('credentials' in navigator && window.PasswordCredential) { + try { + await navigator.credentials.preventSilentAccess(); + } catch (e) { + console.error(e); + } + } +}; let RequestAPI = (Action, Data, CallBack) => { try { let Session = ""; @@ -962,8 +991,7 @@ async function main() { location.href = "https://www.xmoj.tech/modifypage.php?ByUserScript=1"; }); PopupUL.children[5].addEventListener("click", () => { - localStorage.removeItem("UserScript-Username"); - localStorage.removeItem("UserScript-Password"); + clearCredential(); document.cookie = "PHPSESSID=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/"; //This is how you remove a cookie? location.href = "https://www.xmoj.tech/logout.php"; }); @@ -3217,8 +3245,7 @@ async function main() { if (UtilityEnabled("LoginFailed")) { if (Response.indexOf("history.go(-2);") != -1) { if (UtilityEnabled("SavePassword")) { - localStorage.setItem("UserScript-Username", Username); - localStorage.setItem("UserScript-Password", Password); + await storeCredential(Username, Password); } let NewPage = localStorage.getItem("UserScript-LastPage"); if (NewPage == null) { @@ -3227,8 +3254,7 @@ async function main() { location.href = NewPage; } else { if (UtilityEnabled("SavePassword")) { - localStorage.removeItem("UserScript-Username"); - localStorage.removeItem("UserScript-Password"); + clearCredential(); } Response = Response.substring(Response.indexOf("alert('") + 7); Response = Response.substring(0, Response.indexOf("');")); @@ -3244,10 +3270,13 @@ async function main() { }); } }); - if (UtilityEnabled("SavePassword") && localStorage.getItem("UserScript-Username") != null && localStorage.getItem("UserScript-Password") != null) { - document.querySelector("#login > div:nth-child(1) > div > input").value = localStorage.getItem("UserScript-Username"); - document.querySelector("#login > div:nth-child(2) > div > input").value = localStorage.getItem("UserScript-Password"); - LoginButton.click(); + if (UtilityEnabled("SavePassword")) { + let Credential = await getCredential(); + if (Credential) { + document.querySelector("#login > div:nth-child(1) > div > input").value = Credential.id; + document.querySelector("#login > div:nth-child(2) > div > input").value = Credential.password; + LoginButton.click(); + } } } else if (location.pathname == "/contest_video.php" || location.pathname == "/problem_video.php") { let ScriptData = document.querySelector("body > div > div.mt-3 > center > script").innerHTML; @@ -3814,7 +3843,7 @@ int main() AddUser.disabled = true; RequestAPI("SendMail", { "ToUser": String(UsernameData), - "Content": String("您好,我是" + localStorage.getItem("UserScript-Username")) + "Content": String("您好,我是" + CurrentUsername) }, (ResponseData) => { AddUser.children[0].style.display = "none"; AddUser.disabled = false; From eb61aa3f0053cc5878f5a167ecc8e0b31902bc5f Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Tue, 19 Aug 2025 06:26:22 +0000 Subject: [PATCH 2/4] 2.1.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 9e30be84..98b80f3f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xmoj-script", - "version": "2.1.1", + "version": "2.1.2", "description": "an improvement script for xmoj.tech", "main": "AddonScript.js", "scripts": { From 6d3d356083d0bdfa291e0aaa134dc0292b222daf Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Tue, 19 Aug 2025 06:26:27 +0000 Subject: [PATCH 3/4] Update version info to 2.1.2 --- Update.json | 11 +++++++++++ XMOJ.user.js | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Update.json b/Update.json index 5a0f68a6..1147276c 100644 --- a/Update.json +++ b/Update.json @@ -2980,6 +2980,17 @@ } ], "Notes": "No release notes were provided for this release." + }, + "2.1.2": { + "UpdateDate": 1755584782884, + "Prerelease": true, + "UpdateContents": [ + { + "PR": 833, + "Description": "feat: use credential management api" + } + ], + "Notes": "No release notes were provided for this release." } } } \ No newline at end of file diff --git a/XMOJ.user.js b/XMOJ.user.js index 2d3f8c1a..78f9f6d0 100644 --- a/XMOJ.user.js +++ b/XMOJ.user.js @@ -1,6 +1,6 @@ // ==UserScript== // @name XMOJ -// @version 2.1.1 +// @version 2.1.2 // @description XMOJ增强脚本 // @author @XMOJ-Script-dev, @langningchen and the community // @namespace https://github/langningchen From e8381d70324f9b8542c4ec8626fececfc6f448a3 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Tue, 19 Aug 2025 06:27:18 +0000 Subject: [PATCH 4/4] Update time and description of 2.1.2 --- Update.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Update.json b/Update.json index 1147276c..b807bf57 100644 --- a/Update.json +++ b/Update.json @@ -2982,7 +2982,7 @@ "Notes": "No release notes were provided for this release." }, "2.1.2": { - "UpdateDate": 1755584782884, + "UpdateDate": 1755584833374, "Prerelease": true, "UpdateContents": [ {