LemmyTools

A small suite of tools to make Lemmy easier.

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

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