Case converter

swap the case of the selected text

  1. // ==UserScript==
  2. // @name Case converter
  3. // @version 0.2
  4. // @author Noneangel
  5. // @description swap the case of the selected text
  6. // @include http://*
  7. // @include https://*
  8. // @namespace https://gist.github.com/Noneangel/a865645a27e04fbff2843df560ca1dbb
  9. // ==/UserScript==
  10. /* do modify */
  11. // test if one of the key code is pressed
  12. var keyCodes = [114]; //F3
  13. // with the following modifier key pressed
  14. //(note : set to false if modifier should not be pressed)
  15. var modifierKey = {
  16. shiftKey: true
  17. }; // {altKey: false, shiftKey: true, ctrlKey = false}
  18. //which transformation to apply to the text
  19. var transform = invertCase; // invertCase, upperCase, lowerCase
  20.  
  21.  
  22. /* modify at your own risk */
  23.  
  24. /* case transformation */
  25. function invertCase(text) {
  26. return text.split('').map(function(c) {
  27. return c === c.toUpperCase() ? c.toLowerCase() : c.toUpperCase()
  28. }).join('');
  29. }
  30.  
  31. function upperCase(text) {
  32. return text.toUpperCase();
  33. }
  34.  
  35. function lowerCase(text) {
  36. return text.toLowerCase();
  37. }
  38.  
  39. /* get active selection */
  40. //source https://stackoverflow.com/questions/5379120/get-the-highlighted-selected-text
  41. function getSelectionText(activeEl) {
  42. var text = "";
  43. var activeElTagName = activeEl ? activeEl.tagName.toLowerCase() : null;
  44. if (
  45. (activeElTagName == "textarea") || (activeElTagName == "input" &&
  46. /^(?:text|search|password|tel|url)$/i.test(activeEl.type)) &&
  47. (typeof activeEl.selectionStart == "number")
  48. ) {
  49. text = activeEl.value.slice(activeEl.selectionStart, activeEl.selectionEnd);
  50. } else if (window.getSelection) {
  51. text = window.getSelection().toString();
  52. }
  53. return text;
  54. }
  55.  
  56. /* repalce selected text with a replacementText */
  57. //source https://stackoverflow.com/questions/3997659/replace-selected-text-in-contenteditable-div
  58. function replaceSelectedText(replacementText) {
  59. var sel, range;
  60. if (window.getSelection) {
  61. sel = window.getSelection();
  62. if (sel.rangeCount) {
  63. range = sel.getRangeAt(0);
  64. range.deleteContents();
  65. range.insertNode(document.createTextNode(replacementText));
  66. }
  67. } else if (document.selection && document.selection.createRange) {
  68. range = document.selection.createRange();
  69. range.text = replacementText;
  70. }
  71. }
  72. // same but for textarea only
  73. function setInputSelection(elem, text) {
  74. var activeElTagName = elem ? elem.tagName.toLowerCase() : null;
  75. if (
  76. (activeElTagName == "textarea") || (activeElTagName == "input" &&
  77. /^(?:text|search|password|tel|url)$/i.test(elem.type)) &&
  78. (typeof elem.selectionStart == "number")
  79. ) {
  80. var indiceStart = elem.selectionStart;
  81. var indiceEnd = elem.selectionEnd;
  82. var s = elem.selectionStart;
  83. var e = elem.selectionEnd;
  84. elem.value = elem.value.substring(0, s) + text + elem.value.substring(e);
  85. elem.setSelectionRange(indiceStart, indiceEnd);
  86. }
  87. }
  88.  
  89. /* test if obj has the every member of option with the same value */
  90. function validate(option, obj) {
  91. for (var member in option) {
  92. if (option[member] !== obj[member]) {
  93. return false;
  94. }
  95. }
  96. return true;
  97. }
  98.  
  99. function into(e, array) {
  100. return array.reduce(function(acc, val) {
  101. return acc || (e == val)
  102. }, false);
  103. }
  104.  
  105.  
  106. function shortcut(e) {
  107. if (validate(modifierKey, e) && into(e.keyCode, keyCodes)) {
  108. var id = document.activeElement;
  109. var selection = getSelectionText(id);
  110. var transformed = transform(selection);
  111. replaceSelectedText(transformed);
  112. setInputSelection(id, transformed);
  113. e.stopPropagation();
  114. e.preventDefault();
  115. }
  116. }
  117.  
  118. document.addEventListener('keydown', shortcut, true);

QingJ © 2025

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