InstaSynchP Autocomplete

Autocompletes emotes

当前为 2015-03-30 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name InstaSynchP Autocomplete
  3. // @namespace InstaSynchP
  4. // @description Autocompletes emotes
  5.  
  6. // @version 1.0.9
  7. // @author Zod-
  8. // @source https://github.com/Zod-/InstaSynchP-Autocomplete
  9. // @license MIT
  10.  
  11. // @include *://instasync.com/r/*
  12. // @include *://*.instasync.com/r/*
  13. // @grant none
  14. // @run-at document-start
  15.  
  16. // @require https://greasyfork.org/scripts/5647-instasynchp-library/code/InstaSynchP%20Library.js?version=37716
  17. // ==/UserScript==
  18.  
  19. function Autocomplete(version) {
  20. "use strict";
  21. this.version = version;
  22. this.name = 'InstaSynchP Autocomplete';
  23. this.menuActive = false;
  24. this.autocompleteEnabled = true;
  25. this.sources = [];
  26. this.selects = [];
  27. this.settings = [{
  28. 'label': 'Commands',
  29. 'id': 'autocomplete-commands',
  30. 'type': 'checkbox',
  31. 'default': true,
  32. 'section': ['Chat', 'Autocomplete']
  33. }, {
  34. 'label': 'Emotes',
  35. 'id': 'autocomplete-emotes',
  36. 'type': 'checkbox',
  37. 'default': true,
  38. 'section': ['Chat', 'Autocomplete']
  39. }, {
  40. 'label': 'Sort results',
  41. 'id': 'autocomplete-sort',
  42. 'type': 'checkbox',
  43. 'default': true,
  44. 'section': ['Chat', 'Autocomplete']
  45. }, {
  46. 'label': '# of results',
  47. 'id': 'autocomplete-results',
  48. 'type': 'int',
  49. 'min': 0,
  50. 'default': 7,
  51. 'size': 1,
  52. 'section': ['Chat', 'Autocomplete']
  53. }];
  54. var styles = [{
  55. 'name': 'autocomplete',
  56. 'url': 'https://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css',
  57. 'autoload': true
  58. }];
  59. }
  60.  
  61. Autocomplete.prototype.resetVariables = function () {
  62. "use strict";
  63. this.menuActive = false;
  64. this.autocompleteEnabled = true;
  65. };
  66.  
  67. Autocomplete.prototype.addSource = function (source, select) {
  68. "use strict";
  69. this.sources.push(source);
  70. this.selects.push(select);
  71. };
  72.  
  73. Autocomplete.prototype.executeOnce = function () {
  74. "use strict";
  75. var th = this;
  76. events.on(th, 'InputKeydown[9]', function (event) {
  77. if (!th.menuActive) {
  78. return;
  79. }
  80. event.keyCode = $.ui.keyCode.ENTER;
  81. $(this).trigger(event);
  82. });
  83. //emotes
  84. th.addSource(function (term) {
  85. if (!gmc.get('autocomplete-emotes')) {
  86. return [];
  87. }
  88. var lastIndex = term.lastIndexOf('/'),
  89. partToComplete = term.substring(lastIndex, term.length).toLowerCase();
  90. return $.map(Object.keys(window.$codes), function (item) {
  91. item = '/' + item;
  92. if (item.toLowerCase().startsWith(partToComplete)) {
  93. return item;
  94. }
  95. });
  96. },
  97. function (val, item) {
  98. //check if emote is at the beginning of the input
  99. if (window.$codes[item.substring(1, item.length)]) {
  100. return val.lastIndexOf(item) === 0;
  101. }
  102. return false;
  103. }
  104. );
  105.  
  106. //commands
  107. th.addSource(function (term) {
  108. term = term.toLowerCase();
  109. if (!gmc.get('autocomplete-commands')) {
  110. return [];
  111. }
  112. return $.map(Object.keys(commands.getAll()), function (item) {
  113. var command = commands.get(item);
  114. if (item.startsWith(term)) {
  115. if (command.type === 'mod' && !isMod()) {
  116. return undefined;
  117. }
  118. return command.name;
  119. }
  120. });
  121. },
  122. function (val, item) {
  123. var command = commands.get(item);
  124. if (command) {
  125. return val.lastIndexOf(item) === 0 && !command.hasArguments;
  126. }
  127. return false;
  128. }
  129. );
  130.  
  131. events.on(th, 'InputKeydown', function (event) {
  132. if (event.keyCode !== 40 && event.keyCode !== 38) {
  133. th.autocompleteEnabled = true;
  134. }
  135. });
  136. };
  137.  
  138. Autocomplete.prototype.preConnect = function () {
  139. "use strict";
  140. var th = this;
  141. //add the jquery autcomplete widget to InstaSynch's input field
  142. $("#cin").autocomplete({
  143. delay: 0,
  144. minLength: 0,
  145. source: function (request, response) {
  146. var result = [],
  147. i,
  148. words = request.term.split(' '),
  149. last = words[words.length - 1];
  150. //return if autocomplete has been turned off by other plugins
  151. if (!th.autocompleteEnabled || last.length === 0 || request.term.length !== $('#cin')[0].selectionStart) {
  152. response(result);
  153. return;
  154. }
  155. for (i = 0; i < th.sources.length; i += 1) {
  156. try {
  157. result = result.concat(th.sources[i].apply(this, [last]));
  158. } catch (err) {
  159. logger().error(th.name, "Source Error", err);
  160. }
  161. }
  162. if (gmc.get('autocomplete-sort')) {
  163. result.sort();
  164. }
  165.  
  166. response(result.slice(0, gmc.get('autocomplete-results')));
  167. },
  168. select: function (event, ui) {
  169. var val = this.value,
  170. uiVal = ui.item.value,
  171. i,
  172. instant = false;
  173. this.value = val.substring(0, val.lastIndexOf(uiVal[0])) + uiVal;
  174.  
  175. for (i = 0; i < th.selects.length; i += 1) {
  176. try {
  177. //check if the item can be sent instantly
  178. if (th.selects[i].apply(this, [this.value, uiVal])) {
  179. instant = true;
  180. break;
  181. }
  182. } catch (err) {
  183. logger().error(th.name, "Select Error", err);
  184. }
  185. }
  186. if (instant) {
  187. $(this).trigger(
  188. $.Event('keypress', {
  189. which: 13,
  190. keyCode: 13
  191. })
  192. );
  193. } else {
  194. this.value = this.value + ' ';
  195. }
  196. return false;
  197. },
  198. autoFocus: true,
  199. focus: function () {
  200. return false;
  201. },
  202. close: function () {
  203. th.menuActive = false;
  204. },
  205. open: function () {
  206. th.menuActive = true;
  207. }
  208. }).on('paste', function () {
  209. th.autocompleteEnabled = false;
  210. });
  211. };
  212.  
  213. window.plugins = window.plugins || {};
  214. window.plugins.autocomplete = new Autocomplete('1.0.9');