Gab Autocomplete for Users

provides autocomplete for users from following and followers list

  1. // ==UserScript==
  2. // @name Gab Autocomplete for Users
  3. // @namespace https://gab.ai/Jeremy20_9
  4. // @version 0.4
  5. // @description provides autocomplete for users from following and followers list
  6. // @author Jeremiah 20:9
  7. // @match https://gab.ai/*/following
  8. // @match https://gab.ai/*/followers
  9. // @match https://gab.ai/home
  10. // @match https://gab.ai/
  11. // @match https://gab.ai
  12. // @require https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js
  13. // @grant none
  14. // ==/UserScript==
  15.  
  16. var itvautocompleteusers = -1;
  17. var itvgatherusers = -1;
  18.  
  19. $(document).ready(function(){
  20. var profileurl = $(".header__link--profile").attr("href");
  21. if(window.location.href == profileurl + "/following" || window.location.href == profileurl + "/followers")
  22. {
  23. itvgatherusers = setInterval(gatherUsers, 200);
  24. }
  25. else if(window.location.href == "https://gab.ai/home" || window.location.href == "https://gab.ai/" || window.location.href == "https://gab.ai")
  26. {
  27. itvautocompleteusers = setInterval(setupUserAutocomplete, 200);
  28. }
  29. });
  30.  
  31. function gatherUsers()
  32. {
  33. if($(".profile-badge.fal-panel").length === 0)
  34. return;
  35. clearInterval(itvgatherusers);
  36. var users = [];
  37. $(".profile-badge.fal-panel").each(function(idx){
  38. var userlink = $(this).find(".profile-badge__name");
  39. var href = $(userlink).attr("href");
  40. var hrefparts = href.split("/");
  41. var atname = hrefparts[hrefparts.length - 1];
  42. var username = $(userlink).text();
  43. var regex = /[^A-Z0-9 ]/gi;
  44. username = username.replace(regex, "").trim();
  45. var userpic = $(this).find(".profile-badge__picture").attr("src");
  46. users.push({name:username, atname:atname , pic:userpic});
  47. });
  48. if(window.location.href.indexOf("/followers") != -1)
  49. {
  50. localStorage.setItem("gabfollowers", JSON.stringify(users));
  51. }
  52. else if(window.location.href.indexOf("/following") != -1)
  53. {
  54. localStorage.setItem("gabfollowing", JSON.stringify(users));
  55. }
  56. }
  57.  
  58. function setupUserAutocomplete()
  59. {
  60. clearInterval(itvautocompleteusers);
  61. window.autocomplete_users = [];
  62. if(localStorage.getItem("gabfollowers"))
  63. {
  64. window.autocomplete_users = window.autocomplete_users.concat(JSON.parse(localStorage.getItem("gabfollowers")));//JSON.parse($.cookie("gabfollowers")));
  65. }
  66. if(localStorage.getItem("gabfollowing"))
  67. {
  68. window.autocomplete_users = window.autocomplete_users.concat(JSON.parse(localStorage.getItem("gabfollowing")));//JSON.parse($.cookie("gabfollowing")));
  69. }
  70. if(window.autocomplete_users.length === 0)
  71. {
  72. window.autocomplete_users = [{name:"Visit your followers and following pages to populate list", pic:null}];
  73. }
  74. // remove duplicates
  75. var usertable = {};
  76. for(var u = 0; u < window.autocomplete_users.length; u++)
  77. {
  78. var user = window.autocomplete_users[u];
  79. if(usertable[user.atname] === undefined)
  80. usertable[user.atname] = true;
  81. else
  82. {
  83. window.autocomplete_users.splice(u, 1);
  84. u--;
  85. }
  86. }
  87. $(composer.$el).add(composerModal.$el).find("textarea").atwho(
  88. {
  89. at: "@",
  90. displayTpl: "<li><img width='20' height='20' src='${pic}' align='left' style='margin-right:5px' />${name}</li>",
  91. insertTpl: "@${atname}",
  92. data: window.autocomplete_users
  93. }
  94. );
  95. }