LemmyTools

A small suite of tools to make Lemmy easier.

当前为 2023-06-21 提交的版本,查看 最新版本

  1.  
  2. // ==UserScript==
  3. // @name LemmyTools
  4. // @namespace https://thesimplecorner.org/c/lemmytools
  5. // @version 0.1
  6. // @description A small suite of tools to make Lemmy easier.
  7. // @author howdy@thesimplecorner.org
  8. // @match https://*/*
  9. // ==/UserScript==
  10. const homeString = `
  11. // !!! - EDIT YOUR HOME INSTANCE HERE - !!!
  12. const theHomeinstance = 'https://thesimplecorner.org';
  13. // !!! -------------------------------- !!!
  14. /* LemmyTools 0.1a - greasemonkey addon release
  15. Current Features:
  16. - Adds "Easy Subscribe" button to remote instance communities.
  17. - Adds a collapsable sidebar on the left side of screen that shows
  18. a searchable list of your subscribed communities.
  19. - Adds link back to home instance for easy navigation when on remote
  20. instances.
  21. - Remote instance side bar is very much useless currently
  22. Upocoming features:
  23. - When off site collect a list of communities and display in sidebar
  24. for easy subscription.
  25. - Create option page for saving homeinstance and other options.
  26. - Create sub communities grouping function.
  27. - Create remote instance search of communities and integrate with
  28. easy subscribe method.
  29. - create firefox addon.
  30. /* ---------------------- */
  31. `;
  32. /* SCRIPT BELOW */
  33. const funcsString = `
  34. function update(comm, page, subString) {
  35. var xhttp = new XMLHttpRequest();
  36. xhttp.onreadystatechange = function() {
  37. if (this.readyState == 4 && this.status == 200) {
  38. console.log("LemmyTools: " + "update()" + subString);
  39. document.querySelectorAll('[role="alert"]').forEach(function(el) {
  40. el.innerHTML += "<br /><br /><a href=" + subString + " target='_blank'><button class='ltbutton'>Easy Subscribe</button></a>";
  41. });
  42. /*
  43. let commPageArray = [];
  44. let remoteLinks = [];
  45. if (url.includes("/communities")) {
  46. document.querySelectorAll('[class="overflow-wrap-anywhere"]').forEach(function(el) {
  47. commPageArray.push(el.innerText);
  48. });
  49. if (commPageArray.count > 1)
  50. {
  51. console.log("LemmyTools: " + "setting remoteComms to localstore");
  52. localStorage.setItem("remoteComms", commPageArray);
  53. site = site.replace("/communities", "");
  54. for (const comm of commPageArray) {
  55. var subString = homeInstance + "/search/q/!" + comm + "@" + site + "/type/All/sort/TopAll/listing_type/All/community_id/0/creator_id/0/page/1";
  56. remoteLinks.push(subString);
  57. //div.innerHTML += comm + "<br /><a href=" + subString + " target='_blank'><button class='ltbutton'>Easy Subscribe</button></a><hr />";
  58. }
  59. console.log("LemmyTools: " + "setting remoteLinks to localstore");
  60. localStorage.setItem("remoteLinks", remoteLinks);
  61. }
  62. }
  63. */
  64. }
  65. }
  66. xhttp.open("GET", page, true);
  67. xhttp.send(page);
  68. }
  69. function commupdate(id, page, data) {
  70. var count = -1;
  71. var xhttp = new XMLHttpRequest();
  72. xhttp.onreadystatechange = function() {
  73. if (this.readyState == 4 && this.status == 200) {
  74. console.log("LemmyTools: " + "updating " + id + " commsearch with: " + data);
  75. data.forEach(_ => count++);
  76. id.innerHTML = "";
  77. id.innerHTML += "Results: " + count + "<hr />";
  78. id.innerHTML += data;
  79. }
  80. }
  81. xhttp.open("GET", page, true);
  82. xhttp.send(page);
  83. }
  84. function Toggle(overide) {
  85. var s = document.getElementById("searchdiv");
  86. var size = s.getBoundingClientRect();
  87. var x = document.getElementById("myDiv");
  88. var b = document.getElementById("toggle");
  89. //on remote instance just hide the bar.
  90. if (overide = 0)
  91. {
  92. x.style.display = "none";
  93. }
  94. if (x.style.display === "none") {
  95. x.style.display = "block";
  96. b.innerHTML = "<<";
  97. s.style.left = "0%";
  98. } else {
  99. x.style.display = "none";
  100. b.innerHTML = ">>";
  101. if (size.width > 261)
  102. {
  103. s.style.left = "-7.33%";
  104. }
  105. else
  106. {
  107. s.style.left = "-200px";
  108. }
  109. }
  110. }
  111. function searchComms(id, full, commsdiv) {
  112. var url = window.location.href;
  113. var query = id.value.toLowerCase();
  114. console.log("LemmyTools: " + "Searching for:" + query)
  115. if (query == "") {
  116. commsdiv.innerHTML = full;
  117. } else {
  118. commsdiv.innerHTML = full;
  119. var children = commsdiv.getElementsByTagName("li"); // any tag could be used here..
  120. console.log("LemmyTools: " + "Children found: " + children.length);
  121. let data = [""];
  122. var found;
  123. for (var i = 0; i < children.length; i++) {
  124. if (children[i].innerHTML.toLowerCase().indexOf(query) !== -1) {
  125. found = children[i].innerHTML + "<hr />";
  126. console.log("LemmyTools: " + "Found " + query + " in " + found);
  127. data.push(found);
  128. }
  129. }
  130. let dup = [...new Set(data)];
  131. data = dup;
  132. data.sort();
  133. commupdate(commsdiv, url, data);
  134. }
  135. }
  136. // One liner function:
  137. const addCSS = css => document.head.appendChild(document.createElement("style")).innerHTML = css;
  138. // Usage:
  139. addCSS(".ltmenu {position: fixed; min-width: 240px; width: 8%; max-height: 7%; top: 0; left: 0; font-size: .75em; overflow: hidden; display: block; min-height:80px; }" + ".ltcommsbar {position: fixed; min-width: 240px; width: 8%; word-wrap: break-word; max-height: 93%; min-height:93%; top: 80px; left: 0; font-size: .75em; overflow: auto; display: block; }" + ".ltbutton {background-color: #ccffe5;}");
  140. `;
  141. const mainString = `
  142. // LemmyTools
  143. /* EDIT YOUR HOME INSTANCE */
  144. var homeInstance = theHomeinstance;
  145. /* ---------------------- */
  146. /* Script */
  147. var url = window.location.href;
  148. var currentPage = url;
  149. var broken = url.split('/c/');
  150. var site = broken[0];
  151. site = site.replace('https://', '');
  152. var community = broken[1];
  153. var subString = homeInstance + "/search/q/!" + community + "@" + site + "/type/All/sort/TopAll/listing_type/All/community_id/0/creator_id/0/page/1";
  154. var count = 0;
  155. //Easier Subscribe Buttons ---------------------------
  156. //Browsing remote instance
  157. setInterval(function() {
  158. url = window.location.href;
  159. if (currentPage != location.href) {
  160. broken = url.split('/c/');
  161. site = broken[0];
  162. site = site.replace('https://', '');
  163. community = broken[1];
  164. subString = homeInstance + "/search/q/!" + community + "@" + site + "/type/All/sort/TopAll/listing_type/All/community_id/0/creator_id/0/page/1";
  165. // page has changed, set new page as 'current'
  166. console.log("LemmyTools: " + "Easy Sub Running...");
  167. if (document.querySelector('meta[name="Description"]').content.includes("Lemmy")) {
  168. console.log("LemmyTools: " + "On lemmy");
  169. if ((url.includes(homeInstance) == false) && ((url.includes("/c/") || url.includes("/post/") || url.includes("/comment/") || url.includes("/communities")))) {
  170. console.log("LemmyTools: " + "On remote instance community" + "Button to: " + subString);
  171. update(community, url, subString);
  172. }
  173. }
  174. }
  175. currentPage = location.href;
  176. }, 500);
  177. // Direct to community
  178. if (document.querySelector('meta[name="Description"]').content.includes("Lemmy")) {
  179. console.log("LemmyTools: " + "On lemmy");
  180. if ((url.includes(homeInstance) == false) && ((url.includes("/c/") || url.includes("/post/") || url.includes("/comment/") || url.includes("/communities")))) {
  181. console.log("LemmyTools: " + "On remote instance community - DIRECT -" + "Button to: " + subString);
  182. update(community, url, subString);
  183. }
  184. }
  185. //Better Subscription List --------------------------
  186. //Build the divs
  187. var idiv = document.createElement("div");
  188. idiv.setAttribute("id", "searchdiv");
  189. idiv.classList.add("ltmenu", "border-secondary", "card");
  190. idiv.innerHTML = "<input type='text' id='commsearch' placeholder='Sub search' oninput='searchComms(commsearch, communityArray, div)' /><br />LemmyTools<span style='float:right;'><button class='ltbutton' id='toggle' onClick='Toggle()'" + "style='float:right;' /> << </button><br /><br /><b><a href=" + homeInstance + ">Home</a></b></span>";
  191. var div = document.createElement("div");
  192. div.setAttribute("id", "myDiv");
  193. div.classList.add("ltcommsbar", "border-secondary", "card");
  194. if (document.querySelector('meta[name="Description"]').content.includes("Lemmy")) {
  195. url = location.href;
  196. console.log("LemmyTools: " + "url is " + url)
  197. // -----------------------------------------------
  198. //Add divs to page;
  199. document.body.appendChild(idiv);
  200. document.body.appendChild(div);
  201. }
  202. var commsearch = document.getElementById("commsearch");
  203. // -----------------------------------------------
  204. let communityArray = new Array();
  205. if (url.includes(homeInstance)) {
  206. console.log("LemmyTools: " + "home instance do bar");
  207. document.querySelectorAll('[class="list-inline-item d-inline-block"]').forEach(function(el) {
  208. communityArray.push("<li>" + el.innerHTML + "</li>");
  209. });
  210. let dup = [...new Set(communityArray)];
  211. communityArray = dup;
  212. if ((count == 0) || (count == null)) {
  213. count = 0;
  214. communityArray.forEach(_ => count++);
  215. }
  216. div.innerHTML = "Communities: " + count + "<hr />";
  217. div.innerHTML += communityArray;
  218. if (div.innerHTML.length >= 20) {
  219. //make use of it:
  220. console.log("LemmyTools: Got Results >20");
  221. console.log("LemmyTools: " + "setting localcomms localstore");
  222. localStorage.setItem("localComms", communityArray);
  223. } else {
  224. console.log("LemmyTools: " + "get localcomms from localstore");
  225. communityArray = localStorage.getItem("localComms");
  226. div.innerHTML = "Communities: " + count + "<hr />"
  227. div.innerHTML += communityArray;
  228. searchComms(commsearch, communityArray, div);
  229. }
  230. } else {
  231. console.log("LemmyTools: On Remote Instance - Bar");
  232. //div.innerHTML = localStorage.getItem("remoteComms");
  233. Toggle(0);
  234. }
  235. `;
  236. const LTHome = document.head.appendChild(document.createElement("script")).innerHTML = homeString;
  237. const LTFuncs = document.head.appendChild(document.createElement("script")).innerHTML = funcsString;
  238. const LTMain = document.body.appendChild(document.createElement("script")).innerHTML = mainString;