Lemmy Subscription Helper

In the list of communities, darken communities that you're already subscribed to

  1. // ==UserScript==
  2. // @name Lemmy Subscription Helper
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description In the list of communities, darken communities that you're already subscribed to
  6. // @author MikeFez
  7. // @match https://*/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=lemmy.world
  9. // @grant none
  10. // @license MIT
  11. // @require http://code.jquery.com/jquery-latest.js
  12. // ==/UserScript==
  13.  
  14. function waitForKeyElements (
  15. selectorTxt, /* Required: The jQuery selector string that
  16. specifies the desired element(s).
  17. */
  18. actionFunction, /* Required: The code to run when elements are
  19. found. It is passed a jNode to the matched
  20. element.
  21. */
  22. bWaitOnce, /* Optional: If false, will continue to scan for
  23. new elements even after the first match is
  24. found.
  25. */
  26. iframeSelector /* Optional: If set, identifies the iframe to
  27. search.
  28. */
  29. ) {
  30. var targetNodes, btargetsFound;
  31.  
  32. if (typeof iframeSelector == "undefined")
  33. targetNodes = $(selectorTxt);
  34. else
  35. targetNodes = $(iframeSelector).contents ()
  36. .find (selectorTxt);
  37.  
  38. if (targetNodes && targetNodes.length > 0) {
  39. btargetsFound = true;
  40. /*--- Found target node(s). Go through each and act if they
  41. are new.
  42. */
  43. targetNodes.each ( function () {
  44. var jThis = $(this);
  45. var alreadyFound = jThis.data ('alreadyFound') || false;
  46.  
  47. if (!alreadyFound) {
  48. //--- Call the payload function.
  49. var cancelFound = actionFunction (jThis);
  50. if (cancelFound)
  51. btargetsFound = false;
  52. else
  53. jThis.data ('alreadyFound', true);
  54. }
  55. } );
  56. }
  57. else {
  58. btargetsFound = false;
  59. }
  60.  
  61. //--- Get the timer-control variable for this selector.
  62. var controlObj = waitForKeyElements.controlObj || {};
  63. var controlKey = selectorTxt.replace (/[^\w]/g, "_");
  64. var timeControl = controlObj [controlKey];
  65.  
  66. //--- Now set or clear the timer as appropriate.
  67. if (btargetsFound && bWaitOnce && timeControl) {
  68. //--- The only condition where we need to clear the timer.
  69. clearInterval (timeControl);
  70. delete controlObj [controlKey];
  71. }
  72. else {
  73. //--- Set a timer, if needed.
  74. if ( ! timeControl) {
  75. timeControl = setInterval ( function () {
  76. waitForKeyElements ( selectorTxt,
  77. actionFunction,
  78. bWaitOnce,
  79. iframeSelector
  80. );
  81. },
  82. 300
  83. );
  84. controlObj [controlKey] = timeControl;
  85. }
  86. }
  87. waitForKeyElements.controlObj = controlObj;
  88. }
  89.  
  90. (function() {
  91. 'use strict';
  92.  
  93. var isLemmy;
  94. try {
  95. isLemmy = document.head.querySelector("[name~=Description][content]").content === "Lemmy";
  96. } catch (_er) {
  97. isLemmy = false;
  98. }
  99. function isCommunityPage() {
  100. return window.location.pathname.includes("/communities");
  101. }
  102.  
  103. function GM_addStyle(css) {
  104. const style = document.getElementById("GM_addStyleBy8626") || (function() {
  105. const style = document.createElement('style');
  106. style.type = 'text/css';
  107. style.id = "GM_addStyleBy8626";
  108. document.head.appendChild(style);
  109. return style;
  110. })();
  111. const sheet = style.sheet;
  112. sheet.insertRule(css, (sheet.rules || sheet.cssRules || []).length);
  113. }
  114.  
  115. function markSubscribedCommunities() {
  116. console.log("****RERUN")
  117. const communityRows = document.querySelectorAll('table#community_table > tbody > tr');
  118. communityRows.forEach(function(communityRow) {
  119. if (typeof communityRow.dataset['subscriptionChecked'] === 'string') {
  120. return;
  121. }
  122. if (communityRow.lastChild.innerText !== "Subscribe") {
  123. communityRow.setAttribute('data-is-subscribed-to', true);
  124. }
  125. communityRow.setAttribute('data-subscription-checked', true);
  126. });
  127. }
  128.  
  129. function waitForTableLoad() {
  130. waitForKeyElements ("table#community_table > tbody > tr", markSubscribedCommunities);
  131. }
  132.  
  133. if (isLemmy && isCommunityPage()) {
  134. GM_addStyle('[data-is-subscribed-to="true"] { background-color: rgba(0,0,0,0.5); opacity: 0.3; }');
  135. var pageURLCheckTimer = setInterval (
  136. function () {
  137. if (this.lastPathStr !== location.pathname
  138. || this.lastQueryStr !== location.search
  139. || this.lastPathStr === null
  140. || this.lastQueryStr === null
  141. ) {
  142. this.lastPathStr = location.pathname;
  143. this.lastQueryStr = location.search;
  144. waitForTableLoad ();
  145. }
  146. }
  147. , 222
  148. );
  149. }
  150. })();
  151.