Custom Key Commands

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

目前为 2016-05-10 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Custom Key Commands
  3. // @namespace PXgamer
  4. // @version 0.1
  5. // @description Allows custom key commands in text fields, so far I have added Bold and Italic
  6. // @author PXgamer
  7. // @match *kat.cr/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. var arrShortCut = [{ name: 'Bold', key: 66, fx: 'bold' }, { name: 'Italic', key: 73, fx: 'italic'}];
  15.  
  16. var ctrl = 17; // CTRL Key
  17. var ctrlKeyActived = false;
  18.  
  19. $(document).keyup(function(e) {
  20.  
  21. var ta = $('textarea[name="content"');
  22. if (e.which == ctrl) ctrlKeyActived = false;
  23. }).keydown(function(e) {
  24. if (e.which == ctrl) ctrlKeyActived = true;
  25. if (ctrlKeyActived === true && ta.is(":focus")) {
  26. jQuery.each(arrShortCut, function(i) {
  27. if (arrShortCut[i].key == e.which) {
  28. exec(arrShortCut[i].fx, ta);
  29. return;
  30. }
  31. });
  32. }
  33. });
  34.  
  35. function exec(fx, ta) {
  36. console.info(fx);
  37. var strings = [];
  38. switch (fx) {
  39. case 'bold':
  40. strings[0] = "[b]";
  41. strings[1] = "[/b]";
  42. break;
  43. case 'italic':
  44. strings[0] = "[i]";
  45. strings[1] = "[/i]";
  46. break;
  47. default:
  48. strings[0] = "";
  49. strings[1] = "";
  50. }
  51.  
  52. }
  53. })();