JSON paste on textarea

Paste JSON on textarea

当前为 2021-06-12 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name JSON paste on textarea
  3. // @version 0.1
  4. // @description Paste JSON on textarea
  5. // @match https://*/*
  6. // @grant none
  7. // @namespace https://gf.qytechs.cn/users/371179
  8. // ==/UserScript==
  9. (function() {
  10. 'use strict';
  11.  
  12. /*
  13.  
  14. -- Example --
  15. https://jsonformatter.curiousconcept.com/
  16. "{\"a\":1,\"b\":2,\"c\":3,\"d\":\"A\",\"e\":\"B\",\"f\":\"C\"}"
  17.  
  18. */
  19.  
  20. function getClipText(evt) {
  21.  
  22. var text;
  23. var clp = (evt.originalEvent || evt).clipboardData;
  24. if (clp === undefined || clp === null) {
  25. text = window.clipboardData.getData("text") || null;
  26. } else {
  27. text = clp.getData('text/plain') || null;
  28. }
  29. return text;
  30.  
  31. }
  32.  
  33. function changeText(evt, callback) {
  34.  
  35.  
  36. var text, newText;
  37. var clp = (evt.originalEvent || evt).clipboardData;
  38. if (clp === undefined || clp === null) {
  39. text = window.clipboardData.getData("text") || "";
  40. if (text !== "") {
  41. newText = callback(text);
  42.  
  43. if (typeof newText == 'string' && newText != text) {
  44. evt.preventDefault();
  45. if (window.getSelection) {
  46. var newNode = document.createElement("span");
  47. newNode.innerHTML = newText;
  48. window.getSelection().getRangeAt(0).insertNode(newNode);
  49. } else {
  50. document.selection.createRange().pasteHTML(newText);
  51. }
  52. }
  53. }
  54. } else {
  55. text = clp.getData('text/plain') || "";
  56. if (text !== "") {
  57. newText = callback(text);
  58. if (typeof newText == 'string' && newText != text) {
  59. evt.preventDefault();
  60. document.execCommand('insertText', false, newText);
  61. }
  62. }
  63. }
  64.  
  65.  
  66. }
  67.  
  68. // =========================================================================================
  69. // https://stackoverflow.com/questions/7464282/javascript-scroll-to-selection-after-using-textarea-setselectionrange-in-chrome
  70.  
  71. function setSelectionRange(textarea, selectionStart, selectionEnd) {
  72. // First scroll selection region to view
  73. const fullText = textarea.value;
  74. textarea.value = fullText.substring(0, selectionEnd);
  75. // For some unknown reason, you must store the scollHeight to a variable
  76. // before setting the textarea value. Otherwise it won't work for long strings
  77. const scrollHeight = textarea.scrollHeight
  78. textarea.value = fullText;
  79. let scrollTop = scrollHeight;
  80. const textareaHeight = textarea.clientHeight;
  81. if (scrollTop > textareaHeight) {
  82. // scroll selection to center of textarea
  83. scrollTop -= textareaHeight / 2;
  84. } else {
  85. scrollTop = 0;
  86. }
  87. textarea.scrollTop = scrollTop;
  88.  
  89. // Continue to set selection range
  90. textarea.setSelectionRange(selectionStart, selectionEnd);
  91. }
  92. // =========================================================================================
  93.  
  94.  
  95. if (document.queryCommandSupported("insertText")) {
  96.  
  97. var object = {
  98.  
  99. callback: function(str) {
  100.  
  101. var targetElm = this.targetElm;
  102. var clipText = this.clipText;
  103.  
  104. var newClipText = typeof str == 'string' ? str : clipText;
  105. if (newClipText != "") {
  106.  
  107.  
  108. var oldText = targetElm.value
  109. document.execCommand("insertText", false, newClipText);
  110.  
  111. if ('selectionStart' in targetElm) {
  112. var afterChange = () => {
  113. var newText = targetElm.value;
  114. if (oldText == newText) return window.requestAnimationFrame(afterChange);
  115. setSelectionRange(targetElm, targetElm.selectionStart, targetElm.selectionEnd);
  116. };
  117.  
  118. window.requestAnimationFrame(afterChange);
  119.  
  120. }
  121. }
  122.  
  123. }
  124. };
  125.  
  126.  
  127. document.addEventListener('paste', function(evt) {
  128.  
  129. var clipText = getClipText(evt)
  130. if (clipText === null || typeof clipText != 'string') return;
  131.  
  132.  
  133. var targetElm = evt.target;
  134.  
  135. if (!targetElm) return;
  136.  
  137. switch (targetElm.tagName) {
  138. case 'TEXTAREA':
  139. break;
  140. default:
  141. return;
  142. }
  143.  
  144. var testP = clipText.replace(/[0-9a-zA-Z\u4E00-\u9FFF\/\%\-\+\_\.\;\$\#]+/g, '').trim();
  145. var testR = /^[\:\[\]\{\}\,\s\n\"\'\\\/]+$/
  146. if (!testP) {
  147. return;
  148. }
  149. if (!testR.test(testP)) return;
  150.  
  151.  
  152. object.targetElm = targetElm;
  153. object.clipText = clipText;
  154.  
  155. var res = null;
  156. try {
  157. res = new Function('return (' + clipText + ');')(); //backbracket to avoid return newline -> undefined
  158. } catch (e) {}
  159.  
  160. if(typeof res=='string'){
  161. console.log('userscript - there is a text convertion to your pasted content');
  162. evt.preventDefault();
  163. object.callback(res);
  164. }
  165.  
  166.  
  167. }, {
  168. passive: false
  169. });
  170. }
  171.  
  172. // Your code here...
  173. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址