Custom Key Commands

Allows custom key commands in text fields, so far I have added Bold and Italic

  1. // ==UserScript==
  2. // @name Custom Key Commands
  3. // @namespace PXgamer
  4. // @version 0.8
  5. // @description Allows custom key commands in text fields, so far I have added Bold and Italic
  6. // @author PXgamer
  7. // @include *kat.cr/*
  8. // @require https://greasyfork.org/scripts/19569-jquery-selection-jquery-plugin/code/jQueryselection%20-%20jQuery%20Plugin.js
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14. // Are you a translator/higher?
  15. var isHigh = false; /* Set to true if you're high... */
  16. // Do not edit below this line
  17. // ---------------------------
  18.  
  19. var arrShortCut = [{ name: 'Bold', key: 66, fx: 'bold' }, { name: 'Italic', key: 73, fx: 'italic'}, { name: 'Preview', key: 32, fx: 'preview'}];
  20.  
  21. var ctrl = 17; // CTRL Key
  22. var ctrlKeyActived = false;
  23. var ta = $('.quicksubmit');
  24. // Check if Trans+ and on FAQ pages
  25. if (isHigh && location.href.indexOf('/faq/') > -1) {
  26. ta = $('textarea#bbcode');
  27. }
  28. var isBBaction = false;
  29. var previewAction = false;
  30.  
  31. $(document).keyup(function(e) {
  32. if (e.which == ctrl) ctrlKeyActived = false;
  33. }).keydown(function(e) {
  34. if (e.which == ctrl) ctrlKeyActived = true;
  35. if (ctrlKeyActived === true && ta.is(":focus")) {
  36. jQuery.each(arrShortCut, function(i) {
  37. if (arrShortCut[i].key == e.which) {
  38. exec(arrShortCut[i].fx, ta);
  39. return;
  40. }
  41. });
  42. }
  43. });
  44.  
  45. function exec(fx, ta) {
  46. console.info(fx);
  47. var strings = [];
  48. switch (fx) {
  49. case 'bold':
  50. strings[0] = "[b]";
  51. strings[2] = "[/b]";
  52. isBBaction = true;
  53. previewAction = false;
  54. break;
  55. case 'italic':
  56. strings[0] = "[i]";
  57. strings[2] = "[/i]";
  58. isBBaction = true;
  59. previewAction = false;
  60. break;
  61. case 'preview':
  62. isBBaction = false;
  63. previewAction = true;
  64. break;
  65. default:
  66. strings[0] = "";
  67. strings[2] = "";
  68. isBBaction = false;
  69. previewAction = false;
  70. }
  71. if (isBBaction === true) {
  72. if (window.getSelection) {
  73. strings[1] = window.getSelection().toString();
  74. } else if (document.selection && document.selection.type != "Control") {
  75. strings[1] = document.selection.createRange().text;
  76. }
  77.  
  78. ta.selection('replace', { text: strings[0]+strings[1]+strings[2] });
  79. }
  80. if (previewAction === true) {
  81. $('span.ka.ka-preview.bbedit-preview').click();
  82. }
  83. }
  84. })();