diff --git a/Update.json b/Update.json index 00700095..da8cdebc 100644 --- a/Update.json +++ b/Update.json @@ -3477,6 +3477,17 @@ } ], "Notes": "No release notes were provided for this release." + }, + "3.3.6": { + "UpdateDate": 1774172635638, + "Prerelease": true, + "UpdateContents": [ + { + "PR": 955, + "Description": "fix: include image links as markdown when copying solution/problem content" + } + ], + "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 275ef481..daa9a554 100644 --- a/XMOJ.user.js +++ b/XMOJ.user.js @@ -1,6 +1,6 @@ // ==UserScript== // @name XMOJ -// @version 3.3.5 +// @version 3.3.6 // @description XMOJ增强脚本 // @author @XMOJ-Script-dev, @langningchen and the community // @namespace https://github/langningchen @@ -1039,6 +1039,67 @@ function replaceMarkdownImages(text, string) { return text.replace(/!\[.*?\]\(.*?\)/g, string); } +function GetMDText(element) { + let result = ''; + + function traverse(node) { + if (node.nodeType === Node.TEXT_NODE) { + result += node.textContent; + return; + } + + if (node.nodeType !== Node.ELEMENT_NODE) { + return; + } + + const tag = node.nodeName.toUpperCase(); + + // Preserve line breaks for
+ if (tag === 'BR') { + result += '\n'; + return; + } + + // Convert images to Markdown + if (tag === 'IMG') { + const src = node.getAttribute('src'); + if (src) { + let resolvedSrc = src; + try { + resolvedSrc = new URL(src, location.href).href; + } catch (e) { + // Fallback to the raw src if URL construction fails + } + result += `![](${resolvedSrc})`; + } + return; + } + + // Common block-level elements: add newlines around their content + const blockTags = new Set([ + 'P', 'DIV', 'SECTION', 'ARTICLE', 'HEADER', 'FOOTER', 'NAV', + 'UL', 'OL', 'LI', 'PRE', 'BLOCKQUOTE', + 'H1', 'H2', 'H3', 'H4', 'H5', 'H6' + ]); + const isBlock = blockTags.has(tag); + + if (isBlock && !result.endsWith('\n')) { + result += '\n'; + } + + for (let child of node.childNodes) { + traverse(child); + } + + if (isBlock && !result.endsWith('\n')) { + result += '\n'; + } + } + + traverse(element); + return result; +} + async function main() { try { if (location.href.startsWith('http://')) { @@ -2436,7 +2497,7 @@ async function main() { CopyMDButton.type = "button"; document.querySelectorAll(".cnt-row-head.title")[i].appendChild(CopyMDButton); CopyMDButton.addEventListener("click", () => { - GM_setClipboard(Temp[i].children[0].innerText.trim().replaceAll("\n\t", "\n").replaceAll("\n\n", "\n")); + GM_setClipboard(GetMDText(Temp[i].children[0]).trim().replaceAll("\n\t", "\n").replaceAll("\n\n", "\n")); CopyMDButton.innerText = "复制成功"; setTimeout(() => { CopyMDButton.innerText = "复制"; @@ -4464,7 +4525,7 @@ int main() CopyMDButton.type = "button"; document.querySelector("body > div > div.mt-3 > center > h2").appendChild(CopyMDButton); CopyMDButton.addEventListener("click", () => { - GM_setClipboard(ParsedDocument.querySelector("body > div > div > div").innerText.trim().replaceAll("\n\t", "\n").replaceAll("\n\n", "\n")); + GM_setClipboard(GetMDText(ParsedDocument.querySelector("body > div > div > div")).trim().replaceAll("\n\t", "\n").replaceAll("\n\n", "\n")); CopyMDButton.innerText = "复制成功"; setTimeout(() => { CopyMDButton.innerText = "复制"; diff --git a/package.json b/package.json index 6c733c70..515cd2e9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xmoj-script", - "version": "3.3.5", + "version": "3.3.6", "description": "an improvement script for xmoj.tech", "main": "AddonScript.js", "scripts": {