Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion Update.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
}
}
}
67 changes: 64 additions & 3 deletions XMOJ.user.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 <br>
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://')) {
Expand Down Expand Up @@ -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 = "复制";
Expand Down Expand Up @@ -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 = "复制";
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down