// ==UserScript== // @name sxz.school Article HTML editor // @namespace local.sxz.article-editor // @version 1.1.0 // @description Add an HTML editor to article pages and submit updates through the site's API. // @match http://sxz.school.zykj.org/index.html* // @match https://sxz.school.zykj.org/index.html* // @run-at document-idle // @grant GM_xmlhttpRequest // @connect sxz.api.zykj.org // ==/UserScript== (function () { 'use strict'; const API_ROOT = '/api/services/app/appWebSite'; const BUTTON_ID = 'tm-sxz-article-edit-button'; const MODAL_ID = 'tm-sxz-article-edit-modal'; const STYLE_ID = 'tm-sxz-article-edit-style'; const FLOATING_CLASS = 'tm-sxz-floating-button'; function currentArticleId() { const [route, query = ''] = location.hash.slice(1).split('?', 2); if (!route.includes('ArticlePage')) return ''; const id = new URLSearchParams(query).get('id') || ''; return /^\d+$/.test(id) ? id : ''; } function readCookie(name) { const prefix = `${name}=`; const entry = document.cookie .split(';') .map((part) => part.trim()) .find((part) => part.startsWith(prefix)); if (!entry) return ''; let value = entry.slice(prefix.length); try { value = decodeURIComponent(value); } catch (_) { // Keep the original value if it is not valid URI encoding. } return value.replace(/^Bearer\s+/i, '').replace(/^"|"$/g, '').trim(); } function randomHex() { const bytes = new Uint8Array(16); crypto.getRandomValues(bytes); return Array.from(bytes, (byte) => byte.toString(16).padStart(2, '0')).join(''); } async function requestJson(path, options = {}) { const token = readCookie('apiToken'); if (!token) { throw new Error('没有读取到 apiToken Cookie;请确认该 Cookie 对当前页面可见。'); } const headers = { accept: 'application/json, text/plain, */*', 'accept-language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7', appname: 'WebClient', appversion: '0', authorization: `Bearer ${token}`, clientcode: 'null', ...(options.headers || {}) }; return new Promise((resolve, reject) => { GM_xmlhttpRequest({ method: options.method || 'GET', url: `${location.protocol}//sxz.api.zykj.org${path}`, headers, data: options.body, anonymous: false, timeout: 30000, onload(response) { const responseText = response.responseText || ''; let result = null; try { result = responseText ? JSON.parse(responseText) : null; } catch (_) { // Keep non-JSON response text for the error message below. } if (response.status < 200 || response.status >= 300 || result?.success === false) { const message = result?.error?.message || result?.message || responseText || `HTTP ${response.status}`; reject(new Error(message)); return; } resolve(result); }, onerror() { reject(new Error('Tampermonkey 跨域请求失败,请确认已启用对 sxz.api.zykj.org 的访问权限。')); }, ontimeout() { reject(new Error('接口请求超时。')); }, onabort() { reject(new Error('接口请求已取消。')); } }); }); } function addStyles() { if (document.getElementById(STYLE_ID)) return; const style = document.createElement('style'); style.id = STYLE_ID; style.textContent = ` #${BUTTON_ID} { margin-left: 12px; } #${BUTTON_ID}.${FLOATING_CLASS} { position: fixed !important; right: calc(14px + env(safe-area-inset-right, 0px)) !important; bottom: calc(84px + env(safe-area-inset-bottom, 0px)) !important; z-index: 99999 !important; margin: 0 !important; min-width: 64px; height: 40px; padding: 0 15px; border-radius: 20px; box-shadow: 0 3px 10px rgba(0,0,0,.24); } #${MODAL_ID} { position: fixed; inset: 0; z-index: 100000; } #${MODAL_ID} .tm-sxz-mask { position: absolute; inset: 0; background: rgba(0,0,0,.45); } #${MODAL_ID} .tm-sxz-wrap { position: relative; z-index: 1; display: flex; align-items: center; justify-content: center; width: 100%; height: 100%; padding: 16px; box-sizing: border-box; } #${MODAL_ID} .tm-sxz-dialog { width: min(760px, 100%); max-height: 90vh; overflow: auto; background: #fff; border-radius: 2px; box-shadow: 0 3px 6px -4px rgba(0,0,0,.12), 0 6px 16px 0 rgba(0,0,0,.08), 0 9px 28px 8px rgba(0,0,0,.05); } #${MODAL_ID} .tm-sxz-header { display: flex; align-items: center; justify-content: space-between; padding: 16px 24px; border-bottom: 1px solid #f0f0f0; font-size: 16px; font-weight: 600; } #${MODAL_ID} .tm-sxz-close { border: 0; background: transparent; color: rgba(0,0,0,.45); font-size: 22px; line-height: 1; cursor: pointer; } #${MODAL_ID} .tm-sxz-body { padding: 20px 24px 8px; } #${MODAL_ID} .tm-sxz-label { display: block; margin: 0 0 6px; color: rgba(0,0,0,.85); } #${MODAL_ID} .tm-sxz-input { display: block; width: 100%; box-sizing: border-box; margin: 0 0 16px; padding: 8px 11px; border: 1px solid #d9d9d9; border-radius: 2px; color: rgba(0,0,0,.85); font: inherit; outline: none; } #${MODAL_ID} .tm-sxz-input:focus { border-color: #40a9ff; box-shadow: 0 0 0 2px rgba(24,144,255,.2); } #${MODAL_ID} textarea.tm-sxz-input { min-height: 280px; resize: vertical; font-family: Consolas, Monaco, monospace; font-size: 13px; } #${MODAL_ID} .tm-sxz-status { min-height: 22px; margin: 0 0 10px; color: rgba(0,0,0,.65); overflow-wrap: anywhere; } #${MODAL_ID} .tm-sxz-status[data-error="true"] { color: #cf1322; } #${MODAL_ID} .tm-sxz-footer { display: flex; justify-content: flex-end; gap: 8px; padding: 10px 24px 20px; } #${MODAL_ID} .tm-sxz-btn { height: 32px; padding: 4px 15px; border: 1px solid #d9d9d9; border-radius: 2px; background: #fff; color: rgba(0,0,0,.85); cursor: pointer; font: inherit; } #${MODAL_ID} .tm-sxz-btn:hover { border-color: #40a9ff; color: #40a9ff; } #${MODAL_ID} .tm-sxz-btn-primary { border-color: #1890ff; background: #1890ff; color: #fff; } #${MODAL_ID} .tm-sxz-btn-primary:hover { border-color: #40a9ff; background: #40a9ff; color: #fff; } #${MODAL_ID} .tm-sxz-btn:disabled { cursor: not-allowed; opacity: .6; } @media (max-width: 600px) { #${MODAL_ID} .tm-sxz-wrap { padding: 8px; } #${MODAL_ID} .tm-sxz-body { padding: 16px 16px 4px; } #${MODAL_ID} .tm-sxz-footer { padding: 8px 16px 16px; } } `; document.head.appendChild(style); } function setStatus(status, text, isError) { status.textContent = text; status.dataset.error = isError ? 'true' : 'false'; } function closeModal() { document.getElementById(MODAL_ID)?.remove(); } function openModal() { if (document.getElementById(MODAL_ID)) return; addStyles(); const root = document.createElement('div'); root.id = MODAL_ID; root.innerHTML = `