-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpintia-markdown.user.js
More file actions
106 lines (90 loc) · 4.58 KB
/
pintia-markdown.user.js
File metadata and controls
106 lines (90 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// ==UserScript==
// @name Pintia Markdown
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Extracts the problem from Pintia OJ and downloads it as markdown file.
// @author eWloYW8
// @match *://pintia.cn/problem-sets/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
function fetchData() {
const problemSet = window.location.href.split('pintia.cn/problem-sets/')[1].split('/')[0];
const problemId = window.location.href.split('?problemSetProblemId=')[1].split('&')[0];
return fetch('https://pintia.cn/api/problem-sets/' + problemSet + '/exam-problems/' + problemId, {headers: {'accept': 'application/json;charset=UTF-8'}})
.then(response => response.json())
.catch(error => {
console.error("Failed to fetch data:", error);
return null;
});
}
async function init() {
const jsonData = await fetchData();
var data = "# " + jsonData.problemSetProblem.label + " " + jsonData.problemSetProblem.title + "\n\n" + "## 题目描述\n\n" + jsonData.problemSetProblem.description.split('$$').join('$').split('](~/').join('](https://images.ptausercontent.com/') + "\n\n";
if ('codeCompletionProblemConfig' in jsonData.problemSetProblem.problemConfig) {
data += "## 题目要求\n\n 代码长度限制:" + jsonData.problemSetProblem.problemConfig.codeCompletionProblemConfig.codeSizeLimit + " KB\n\n 运行时间限制:" + jsonData.problemSetProblem.problemConfig.codeCompletionProblemConfig.timeLimit + " ms\n\n 内存限制:" + jsonData.problemSetProblem.problemConfig.codeCompletionProblemConfig.memoryLimit + " KB\n\n";
}
else if ('programmingProblemConfig' in jsonData.problemSetProblem.problemConfig) {
data += "## 题目要求\n\n 代码长度限制:" + jsonData.problemSetProblem.problemConfig.programmingProblemConfig.codeSizeLimit + " KB\n\n 运行时间限制:" + jsonData.problemSetProblem.problemConfig.programmingProblemConfig.timeLimit + " ms\n\n 内存限制:" + jsonData.problemSetProblem.problemConfig.programmingProblemConfig.memoryLimit + " KB\n\n";
}
else{
throw new Error("Unknown problem type");
}
const downloadButton = document.createElement('button');
downloadButton.textContent = 'Download Markdown';
downloadButton.style.position = 'fixed';
downloadButton.style.bottom = '20px';
downloadButton.style.right = '20px';
downloadButton.style.zIndex = '9999';
downloadButton.style.padding = '12px 24px';
downloadButton.style.backgroundColor = '#0d6efd';
downloadButton.style.color = '#fff';
downloadButton.style.border = 'none';
downloadButton.style.borderRadius = '8px';
downloadButton.style.cursor = 'pointer';
downloadButton.style.boxShadow = '0 4px 6px rgba(0, 0, 0, 0.1)';
downloadButton.style.fontSize = '16px';
downloadButton.style.transition = 'background-color 0.3s ease, transform 0.2s ease';
downloadButton.addEventListener('mouseenter', () => {
downloadButton.style.backgroundColor = '#0b5ed7';
downloadButton.style.transform = 'scale(1.05)';
});
downloadButton.addEventListener('mouseleave', () => {
downloadButton.style.backgroundColor = '#0d6efd';
downloadButton.style.transform = 'scale(1)';
});
downloadButton.addEventListener('click', () => {
const blob = new Blob([data], { type: 'text/plain;charset=utf-8' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = jsonData.problemSetProblem.label + " " + jsonData.problemSetProblem.title + ".md";
document.body.appendChild(a);
a.click();
URL.revokeObjectURL(url);
document.body.removeChild(a);
});
document.body.appendChild(downloadButton);
}
init();
const originalPushState = history.pushState;
const originalReplaceState = history.replaceState;
history.pushState = function(...args) {
originalPushState.apply(this, args);
try {
init();
} catch {
console.log(e);
}
};
history.replaceState = function(...args) {
originalReplaceState.apply(this, args);
try {
init();
} catch {
console.log(e);
}
};
window.addEventListener('popstate', onUrlChange);
})();