InstaSynchP Name Completion

Autocomplete usernames by hitting tab

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

QingJ © 2025

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