// ==UserScript== // @name cph-submit.background // @namespace http://tampermonkey.net/ // @version 0.1.0 // @description cph-submit 后台脚本 // @author Andy_hpy // @grant GM_xmlhttpRequest // @grant GM_setValue // @grant GM_openInTab // @grant GM_log // @background // @storageName cph-submit // ==/UserScript== const LOOP_INTERVAL = 3000; const CPH_URL = 'http://localhost:27121/getSubmit'; function getSubmitUrl(problemUrl) { if (!problemUrl.includes('contest')) { return 'https://codeforces.com/problemset/submit'; } const match = problemUrl.match(/contest\/(\d+)/); if (match) { return `https://codeforces.com/contest/${match[1]}/submit`; } const match2 = problemUrl.match(/gym\/(\d+)/); if (match2) { return `https://codeforces.com/gym/${match2[1]}/submit`; } return 'https://codeforces.com/problemset/submit'; } function poll() { GM_xmlhttpRequest({ method: 'GET', url: CPH_URL, headers: { 'cph-submit': 'true' }, onload: function(res) { if (res.status === 200) { try { const data = JSON.parse(res.responseText); if (!data.empty) { const task = { problemName: data.problemName, languageId: data.languageId, sourceCode: data.sourceCode, url: data.url, timestamp: Date.now() }; GM_setValue('pendingSubmit', JSON.stringify(task)); const submitUrl = getSubmitUrl(data.url); GM_openInTab(submitUrl, { active: true, insert: true }); } else { GM_log('服务器返回 empty,无任务', "error"); } } catch (e) { GM_log('解析 JSON 失败: ' + e.message, "error"); } } else { GM_log('HTTP 错误: ' + res.status, "error"); } }, onerror: function(err) { GM_log('请求失败: ' + JSON.stringify(err), "error"); } }); } setInterval(poll, LOOP_INTERVAL);