// ==UserScript== // @name Microsoft Rewards Auth Code Capture // @namespace https://github.com/geoisam/FuckScripts // @version 1.0.2 // @description 捕获微软授权码 // @author geoisam@qq.com // @match https://login.live.com/* // @run-at document-end // @grant GM_setValue // @grant GM_getValue // @grant GM_notification // @grant GM_log // @grant GM_cookie // @grant GM_addValueChangeListener // @connect login.live.com // @connect live.com // @storageName FuckScripts_Shared_Auth // ==/UserScript== (function() { 'use strict'; const captureAuthCode = () => { try { const urlParams = new URLSearchParams(window.location.search); const authCode = urlParams.get("code"); if (authCode && authCode.length > 10) { GM_log("✅ 检测到授权码: " + authCode.substring(0, 20) + "..."); const fullCode = "https://login.live.com/oauth20_desktop.srf?code=" + authCode.trim(); GM_setValue("Config.code", fullCode); GM_setValue("Config.token", false); // 同时写入 cookie 通道:主脚本轮询时可通过 GM_cookie/document.cookie 实时读到, // 作为共享存储跨脚本可见性的备援通道 try { if (typeof GM_cookie !== 'undefined') { GM_cookie.set({ name: "microsoft_rewards_auth_code", value: encodeURIComponent(fullCode), domain: ".live.com", path: "/", secure: true, expirationDate: Date.now() / 1000 + 600 }, function() {}); } } catch (e) {} try { document.cookie = "microsoft_rewards_auth_code=" + encodeURIComponent(fullCode) + "; path=/; max-age=600; secure"; } catch (e) {} GM_notification({ text: "授权码获取成功!", title: "Microsoft Rewards Auth", onclick: function() {} }); return true; } } catch (e) { GM_log("捕获授权码异常: " + e.message); } return false; }; const scheduleCapture = () => { GM_log("🔍 授权页面检测到,开始监听授权码..."); if (captureAuthCode()) { GM_log("✅ 立即捕获成功"); return; } // 授权页可能同时注入多个实例(authorize 页与重定向后的 desktop 页), // 若共享存储中已有授权码(其他实例已捕获),本实例无需再轮询 if (GM_getValue("Config.code", "")) { GM_log("✅ 授权码已被捕获,停止监听"); return; } const tryAgain = () => { if (captureAuthCode()) { GM_log("✅ 延迟捕获成功"); } }; if (document.readyState === 'complete') { tryAgain(); } else { document.addEventListener('DOMContentLoaded', tryAgain); window.addEventListener('load', tryAgain); } let attempt = 0; const maxAttempts = 120; const checkInterval = setInterval(() => { attempt++; // 其他实例捕获成功后立即停止空转 if (GM_getValue("Config.code", "")) { clearInterval(checkInterval); GM_log("✅ 授权码已被捕获,停止监听"); return; } GM_log("🔍 第 " + attempt + " 次检查..."); if (captureAuthCode()) { clearInterval(checkInterval); GM_log("✅ 定时检查捕获成功"); return; } if (attempt >= maxAttempts) { clearInterval(checkInterval); GM_log("⏰ 授权码捕获超时"); } }, 500); // 跨实例实时通知:任一实例写入授权码后,其余空转实例立即停止 if (typeof GM_addValueChangeListener !== 'undefined') { try { GM_addValueChangeListener("Config.code", (name, oldValue, newValue) => { if (newValue) { clearInterval(checkInterval); GM_log("✅ 授权码已被捕获,停止监听"); } }); } catch (e) {} } window.addEventListener('hashchange', () => { GM_log("🔍 hashchange 事件触发"); captureAuthCode(); }); window.addEventListener('popstate', () => { GM_log("🔍 popstate 事件触发"); captureAuthCode(); }); }; scheduleCapture(); })();