InstaSynchP Name Completion

Autocomplete usernames by hitting tab

当前为 2014-11-29 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name InstaSynchP Name Completion
  3. // @namespace InstaSynchP
  4. // @description Autocomplete usernames by hitting tab
  5.  
  6. // @version 1
  7. // @author Zod-
  8. // @source https://github.com/Zod-/InstaSynchP-Name-Completion
  9. // @license MIT
  10.  
  11. // @include http://*.instasynch.com/*
  12. // @include http://instasynch.com/*
  13. // @include http://*.instasync.com/*
  14. // @include http://instasync.com/*
  15. // @grant none
  16. // @run-at document-start
  17.  
  18. // @require https://greasyfork.org/scripts/5647-instasynchp-library/code/InstaSynchP%20Library.js
  19. // @require https://greasyfork.org/scripts/6707-jquery-caret/code/jquerycaret.js?version=26377
  20. // ==/UserScript==
  21.  
  22. function NameCompletion(version) {
  23. "use strict";
  24. this.version = version;
  25. this.enabled = true;
  26. this.name = 'InstaSynchP Name Completion';
  27. this.doubleTabTimeoutId = undefined;
  28. }
  29.  
  30. NameCompletion.prototype.executeOnce = function () {
  31. var th = this;
  32.  
  33. function checkDoubleTab() {
  34. //check for double tabs within 500 ms
  35. if (typeof th.doubleTabTimeoutId !== 'undefined') {
  36. clearInterval(th.doubleTabTimeoutId);
  37. th.doubleTabTimeoutId = undefined;
  38. return true;
  39. }
  40.  
  41. th.doubleTabTimeoutId = setTimeout(function () {
  42. th.doubleTabTimeoutId = undefined;
  43. }, 500);
  44. return false;
  45. }
  46.  
  47. function getPartToComplete(str, caretPosition) {
  48. //go back from the caret position as long as it fits the username regex
  49. var temp, partToComplete = '';
  50. for (var i = caretPosition - 1; i >= 0; i -= 1) {
  51. temp = str.substr(i, caretPosition);
  52. if (isGreyname(temp)) {
  53. partToComplete = temp.toLowerCase();
  54. } else {
  55. break;
  56. }
  57. }
  58. return partToComplete;
  59. }
  60.  
  61. function getUsers(partToComplete, comp) {
  62. //get all users that start with or contain partToComplete
  63. return $.map(window.users, function (user) {
  64. return (user.username.toLowerCase()[comp](partToComplete)) ? user.username : undefined;
  65. });
  66. }
  67.  
  68. function printUsers(users) {
  69. var unique = [],
  70. output = '';
  71. //get unique users
  72. $.each(users, function (i, el) {
  73. if ($.inArray(el, unique) === -1) unique.push(el);
  74. });
  75. for (i = 0; i < unique.length; i += 1) {
  76. output += unique[i] + ' ';
  77. }
  78. addSystemMessage(output);
  79. }
  80.  
  81. events.on(th, 'InputKeydown[9]', function () {
  82. var str = $('#cin').val(),
  83. caretPosition = $('#cin').caret(),
  84. partToComplete = getPartToComplete(str, caretPosition),
  85. startIndex = caretPosition - partToComplete.length;
  86. if (str === '' || partToComplete === '') {
  87. return;
  88. }
  89. var startsWithArr = getUsers(partToComplete, 'startsWith'),
  90. containsArr = getUsers(partToComplete, 'contains'),
  91. result;
  92. if (containsArr.length === 0 && startsWithArr.length === 0) {
  93. return;
  94. }
  95. if (startsWithArr.length > 1 || (containsArr.length > 1 && startsWithArr.length !== 1)) {
  96. if (checkDoubleTab()) {
  97. printUsers(startsWithArr.length > 1 ? startsWithArr : containsArr);
  98. }
  99. return;
  100. }
  101.  
  102. result = startsWithArr[0] || containsArr[0];
  103. //put string back together with the found username
  104. str = '{0}{1}{2}'.format(
  105. str.substr(0, startIndex),
  106. result,
  107. str.substr(caretPosition)
  108. );
  109. //set string and cursor
  110. $('#cin').val(str).caret(startIndex + result.length);
  111. });
  112. };
  113.  
  114. window.plugins = window.plugins || {};
  115. window.plugins.nameCompletion = new NameCompletion('1');