// ==UserScript== // @name NodeSeek自动上传到屋舍图床并插入md链接 // @namespace http://tampermonkey.net/ // @version 1.1 // @description 在nodeseek.com的论坛中自动上传图片到屋舍图床并插入Markdown链接 // @author WiseScripts // @match https://www.nodeseek.com/* // @grant GM_xmlhttpRequest // @downloadURL none // ==/UserScript== (function() { 'use strict'; document.addEventListener('paste', (event) => handlePaste(event)); async function handlePaste(event) { const items = (event.clipboardData || event.originalEvent.clipboardData).items; let imageFiles = []; for (let item of items) { if (item.kind === 'file' && item.type.indexOf('image/') !== -1) { let blob = item.getAsFile(); imageFiles.push(blob); } } if (imageFiles.length > 0) { event.preventDefault(); for (let file of imageFiles) { let formData = new FormData(); formData.append('file', file); await uploadToImageHost(formData); } } } function uploadToImageHost(formData) { return new Promise((resolve, reject) => { GM_xmlhttpRequest({ method: 'POST', url: 'https://uhsea.com/Frontend/upload', data: formData, onload: (response) => { let jsonResponse = JSON.parse(response.responseText); if (response.status === 200 && jsonResponse && jsonResponse.data) { insertToEditor(jsonResponse.data); } else { mscAlert('图片上传成功,但未获取到Markdown链接'); } resolve(); }, onerror: (error) => { mscAlert('图片上传遇到错误,请检查网络或稍后重试。'); reject(error); } }); }); } function insertToEditor(Link) { const codeMirrorElement = document.querySelector('.CodeMirror'); if (codeMirrorElement) { const codeMirrorInstance = codeMirrorElement.CodeMirror; if (codeMirrorInstance) { const cursor = codeMirrorInstance.getCursor(); let markdownLink = '![](' + Link + ')'; copyTextToClipboard(markdownLink); codeMirrorInstance.replaceRange(markdownLink + '\n', cursor); } } } function copyTextToClipboard(text) { var textArea = document.createElement("textarea"); textArea.style.position = 'fixed'; textArea.style.top = 0; textArea.style.left = 0; textArea.style.width = '2em'; textArea.style.height = '2em'; textArea.style.padding = 0; textArea.style.border = 'none'; textArea.style.outline = 'none'; textArea.style.boxShadow = 'none'; textArea.style.background = 'transparent'; textArea.value = text; document.body.appendChild(textArea); textArea.focus(); textArea.select(); try { var successful = document.execCommand('copy'); var msg = successful ? 'successful' : 'unsuccessful'; console.log('Copying text command was ' + msg); } catch (err) { console.log('Oops, unable to copy'); } document.body.removeChild(textArea); } })();