// ==UserScript== // @name JsBuilder // @namespace https://docs.scriptcat.org/ // @version 2025.11.21.0.1 // @description try to take over the world! // @author You // @match https://www.baidu.com/* // @require https://code.jquery.com/jquery-3.7.1.min.js // ==/UserScript== $(function(){ new JsBuilder().Add(); }) class JsBuilder{ Add(){ this.Add_Box(); this.Add_Event(); } Add_Box(){ const bx = `
` $('body').append(bx); } Add_Event(){ const TC = TextareaControl; TC.ta = $('.text-block')[0]; $('.fast-input').on('click','button',function(){ const type = $(this).attr("code-type"); switch(type){ case "ab": TC.insertBeforAndAfter(this.innerText); break; case "i": TC.insertTextAtCursor(this.innerText); break; } }); $('.copy').click(()=>{ TC.copyTextareaToClipboard(); }) $('.switch').click(function(){ if($('.js-bulder:visible').length>0){ this.innerText = "^"; $('.js-bulder').fadeOut() }else{ this.innerText = "x"; $('.js-bulder').fadeIn() } }) // $('.control-dir').on('click','button',function(){ // const text = this.innerText; // switch(text){ // case "▲": // TC.moveCursorUp(); // break; // case "◀": // TC.moveCursorLeft(); // break; // case "▼": // TC.moveCursorDown(); // break; // case "▶": // TC.moveCursorLeft(-1); // break; // } // }); } } const TextareaControl = { ta:null, getCursorPosition : function() { if(TextareaControl.ta==null) return null; return TextareaControl.ta.selectionStart; }, insertTextAtCursor : function(text) { const textarea = TextareaControl.ta; const startPos = textarea.selectionStart; const endPos = textarea.selectionEnd; const before = textarea.value.substring(0, startPos); const after = textarea.value.substring(endPos, textarea.value.length); textarea.value = before + text + after; textarea.selectionStart = textarea.selectionEnd = startPos + text.length; textarea.focus(); }, getSelectedText:function() { const textarea = TextareaControl.ta; const startPos = textarea.selectionStart; const endPos = textarea.selectionEnd; return textarea.value.substring(startPos, endPos); }, replaceSelectedText : function (text) { const textarea = TextareaControl.ta; const startPos = textarea.selectionStart; const endPos = textarea.selectionEnd; const before = textarea.value.substring(0, startPos); const after = textarea.value.substring(endPos, textarea.value.length); textarea.value = before + text + after; textarea.selectionStart = textarea.selectionEnd = startPos + text.length; textarea.focus(); }, insertBeforAndAfter : function (code) { if(code.length!=2) return; let text = TextareaControl.getSelectedText(); TextareaControl.replaceSelectedText(code[0]+text+code[1]); const textarea = TextareaControl.ta; const startPos = textarea.selectionStart - 1; textarea.selectionStart = textarea.selectionEnd = startPos; textarea.focus(); }, moveCursorLeft: function(len) { const pos = TextareaControl.getCursorPosition(TextareaControl.ta); if (pos.start > 0) { TextareaControl.setCursorPosition(pos.start - len); } }, moveCursorUp: function() { const ctrl = TextareaControl.ta; const pos = TextareaControl.getCursorPosition(ctrl); const textBeforeCursor = ctrl.value.substring(0, pos.start); const lines = textBeforeCursor.split('\n'); if (lines.length > 1) { const currentLineLength = lines[lines.length - 1].length; const prevLineLength = lines[lines.length - 2].length; const newPos = pos.start - currentLineLength - prevLineLength - 1; TextareaControl.setCursorPosition(Math.max(0, newPosition)); } }, moveCursorDown:function() { const ctrl = TextareaControl.ta; const pos = TextareaControl.getCursorPosition(ctrl); const textAfterCursor = ctrl.value.substring(pos.start); const linesBefore = ctrl.value.substring(0, pos.start).split('\n'); const linesAfter = textAfterCursor.split('\n'); if (linesAfter.length > 1) { const currentLineLength = linesBefore[linesBefore.length - 1].length; const nextLineLength = linesAfter[1].length; const newPos = pos.start + currentLineLength + nextLineLength + 1; TextareaControl.setCursorPosition(ctrl, Math.min(ctrl.value.length, newPosition)); } }, setCursorPosition: function(pos) { const ctrl = TextareaControl.ta; if (ctrl.setSelectionRange) { ctrl.selectionStart = ctrl.selectionEnd = pos ctrl.focus(); } else if (ctrl.createTextRange) { const range = ctrl.createTextRange(); range.collapse(true); range.moveEnd('character', pos); range.moveStart('character', pos); range.select(); } }, copyTextareaToClipboard : async function () { const text = TextareaControl.ta.value; if (!text) { console.warn('textarea内容为空'); return false; } // 方法1: 使用现代 Clipboard API if (navigator.clipboard && window.isSecureContext) { return navigator.clipboard.writeText(text).then(() => { console.log('文本已复制到剪贴板'); return true; }).catch(err => { console.error('复制失败:', err); return false; }); } // 方法2: 使用传统的execCommand方法(已废弃但兼容旧浏览器) else { const textArea = document.createElement('textarea'); textArea.value = text; // 避免屏幕闪烁 textArea.style.position = 'fixed'; textArea.style.top = '0'; textArea.style.left = '0'; textArea.style.opacity = '0'; document.body.appendChild(textArea); textArea.focus(); textArea.select(); try { const successful = document.execCommand('copy'); document.body.removeChild(textArea); if (successful) { console.log('文本已复制到剪贴板'); return true; } else { console.error('复制失败'); return false; } } catch (err) { console.error('复制失败:', err); document.body.removeChild(textArea); return false; } } } }