// ==UserScript== // @name PTA编程初始化代码(Ctrl+M) // @namespace https://bbs.tampermonkey.net.cn/ // @version 1.0 // @description 使你在pta平台更方便初始化代码,只需Ctrl+M键 // @author 张应胜 // @match https://pintia.cn/* // @grant none // ==/UserScript== document.addEventListener('keydown', function(event) { if(event.ctrlKey && event.key === 'm') { event.preventDefault(); const text = `#include <stdio.h> // 标准输入输出库 #include <stdlib.h> // 标准库函数 #include <string.h> // 字符串处理库 #include <windows.h> // Windows系统相关库 #include <conio.h> // 控制台输入输出库 #include <math.h> // 数学库 #include <time.h> // 时间和日期库 #include <ctype.h> // 字符处理库 #include <stdbool.h> // 布尔类型库 #include <stddef.h> // 常用类型库 #include <stdint.h> // 固定长度整数类型库 #include <assert.h> // 断言库 #include <errno.h> // 错误码库 #include <limits.h> // 整数类型取值范围库 #include <stdarg.h> // 可变参数库 int main() { return 0; }`; const activeElement = document.activeElement; if(activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA' || activeElement.contentEditable === 'true') { let startPos, endPos; if (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA') { startPos = activeElement.selectionStart; endPos = activeElement.selectionEnd; } else { const selection = window.getSelection(); startPos = selection.focusOffset; endPos = selection.anchorOffset; } const currentValue = activeElement.value || activeElement.innerText || activeElement.textContent; const newValue = currentValue.substring(0, startPos) + text + currentValue.substring(endPos); if (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA') { activeElement.value = newValue; } else { activeElement.innerHTML = newValue; } if (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA') { activeElement.selectionStart = startPos + text.length; activeElement.selectionEnd = startPos + text.length; } else { const range = document.createRange(); range.setStart(activeElement.childNodes[0], startPos + text.length); range.setEnd(activeElement.childNodes[0], startPos + text.length); const sel = window.getSelection(); sel.removeAllRanges(); sel.addRange(range); } } } });