Fastmail - Folder Badge Controls

Turn badges on or off for specific folders, or show total # of messages.

  1. // Written by Michael Stepner, 23 April 2015.
  2. //
  3. /* The MIT License (MIT):
  4. Copyright (c) 2015 Michael Stepner
  5.  
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12.  
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15.  
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23. */
  24. // ==UserScript==
  25. // @name Fastmail - Folder Badge Controls
  26. // @description Turn badges on or off for specific folders, or show total # of messages.
  27. // @author Michael Stepner
  28. // @namespace https://michaelstepner.com
  29. // @license MIT License
  30. // @homepageURL https://github.com/michaelstepner/fastmail-FolderBadgeControls
  31. // @supportURL https://github.com/michaelstepner/fastmail-FolderBadgeControls/issues
  32. // @version 1.0.1
  33. // @id fastmail-FolderBadgeControls
  34. // @include https://www.fastmail.com/mail/*
  35. // @require https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
  36. // @grant GM_getValue
  37. // @grant GM_setValue
  38. // @grant GM_registerMenuCommand
  39. // @grant GM_addStyle
  40. // ==/UserScript==
  41.  
  42.  
  43. ///// Retrieve configuration
  44. var foldersShowAllOnBadge = GM_getValue ("foldersShowAllOnBadge", "");
  45. var foldersAlwaysShowBadge = GM_getValue ("foldersAlwaysShowBadge", "");
  46. var foldersNeverShowBadge = GM_getValue ("foldersNeverShowBadge", "");
  47. var firstConfig = GM_getValue ("firstConfig", "");
  48.  
  49.  
  50. ///// Create a configuration form in a popup box
  51.  
  52. // Insert HTML for the popup box
  53. $("body[contenteditable!='true']").append ( ' \
  54. <div id="gmPopupContainer"> \
  55. <form> <!-- For true form use method="POST" action="YOUR_DESIRED_URL" --> \
  56. <h1>Instructions:</h1> \
  57. <p>In each entry, list the folders whose badge you want to modify.</p> \
  58. <p>Separate each folder name by a semicolon.</p> \
  59. <br><p>You need to specify folders using the name in their URL:</p> \
  60. <p>&nbsp;&nbsp;&nbsp;https://www.fastmail.com/mail/<b>Folder_Name</b>/?u=0xx00000</p> \
  61. <br><p id="gmMenuCommandInstructions">&nbsp;</p> \
  62. <br><h1>Configuration:</h1> \
  63. <p>Show <strong>total</strong> number of messages (read and unread) on badge:</p> \
  64. <input type="text" id="gmConfigShowTotal"> \
  65. <p><strong>Always</strong> show badge:</p> \
  66. <input type="text" id="gmConfigShowAlways" value=""> \
  67. <p><strong>Never</strong> show badge:</p> \
  68. <input type="text" id="gmConfigShowNever" value=""> \
  69. <br><button id="gmClosePopupAndCancel" type="button">Cancel</button> \
  70. <button id="gmClosePopupAndSave" type="button"><strong>Submit</strong></button> \
  71. </form> \
  72. </div> \
  73. ' );
  74.  
  75. // Detect whether running in Greasemonkey or Tampermonkey
  76. var scriptEngine;
  77. if (typeof GM_info === "undefined") {
  78. scriptEngine = "plain";
  79. // plain Chrome, or Opera, or scriptish, or Safari, or rarer
  80. }
  81. else {
  82. scriptEngine = GM_info.scriptHandler || "Greasemonkey";
  83. }
  84.  
  85. // Write correct instructions for Greasemonkey or Tampermonkey
  86. if (scriptEngine=="Greasemonkey") {
  87. document.getElementById("gmMenuCommandInstructions").innerHTML = "You can always return to this configuration box via Tools > <br>&nbsp;&nbsp;&nbsp;> Greasemonkey > User script commands... <br>&nbsp;&nbsp;&nbsp;> Folder badge configuration";
  88. }
  89. else if (scriptEngine=="Tampermonkey") {
  90. document.getElementById("gmMenuCommandInstructions").innerHTML = "You can always return to this configuration box by clicking<br>&nbsp;&nbsp;&nbsp;the Tampermonkey menu button while in Fastmail, then<br>&nbsp;&nbsp;&nbsp;clicking 'Folder badge configuration'.";
  91. }
  92. else {
  93. document.getElementById("gmMenuCommandInstructions").innerHTML = "You seem to be running this userscript without Greasemonkey<br>&nbsp;&nbsp;&nbsp;or Tampermonkey. Even if it is working, you will be unable to<br>&nbsp;&nbsp;&nbsp;return to this configuration box later. You should use<br>&nbsp;&nbsp;&nbsp;Greasemonkey in Firefox or Tampermonkey otherwise.";
  94. }
  95.  
  96. // Hide the popup box unless config has never been done before
  97. if (firstConfig=="done") {
  98. $("#gmPopupContainer").hide ();
  99. }
  100.  
  101. // Set the input boxes to contain current config values
  102. function setPopupConfigValues () {
  103. document.getElementById('gmConfigShowTotal').value=foldersShowAllOnBadge ;
  104. document.getElementById('gmConfigShowAlways').value=foldersAlwaysShowBadge ;
  105. document.getElementById('gmConfigShowNever').value=foldersNeverShowBadge ;
  106. }
  107. setPopupConfigValues ();
  108.  
  109. // Save the new config values on 'Submit'
  110. $("#gmClosePopupAndSave").click ( function () {
  111. foldersShowAllOnBadge = document.getElementById('gmConfigShowTotal').value ;
  112. foldersAlwaysShowBadge = document.getElementById('gmConfigShowAlways').value ;
  113. foldersNeverShowBadge = document.getElementById('gmConfigShowNever').value ;
  114. updateConfig();
  115. updateSelectors();
  116. $("#gmPopupContainer").hide ();
  117. refreshCustomBadges(folderSelectors);
  118. } );
  119. function updateConfig () {
  120. GM_setValue ("foldersShowAllOnBadge", foldersShowAllOnBadge);
  121. GM_setValue ("foldersAlwaysShowBadge", foldersAlwaysShowBadge);
  122. GM_setValue ("foldersNeverShowBadge", foldersNeverShowBadge);
  123. GM_setValue ("firstConfig", "done");
  124. }
  125.  
  126. // Restore popup text entry values on 'Cancel'
  127. $("#gmClosePopupAndCancel").click ( function () {
  128. setPopupConfigValues ();
  129. $("#gmPopupContainer").hide ();
  130. } );
  131.  
  132. // Style the config popup box with CSS
  133. GM_addStyle ( " \
  134. #gmPopupContainer { \
  135. position: fixed; \
  136. top: 20%; \
  137. left: 20%; \
  138. padding: 1em 2em; \
  139. background: #C0C0C0; \
  140. border: 3px double black; \
  141. border-radius: 1ex; \
  142. z-index: 777; \
  143. } \
  144. #gmPopupContainer button{ \
  145. cursor: pointer; \
  146. margin: 1em 1em 0; \
  147. padding-top: 5px; \
  148. padding-right: 5px; \
  149. padding-bottom: 5px; \
  150. padding-left: 5px; \
  151. border: 2px outset buttonface; \
  152. margin-left: 75px; \
  153. } \
  154. #gmPopupContainer p{ \
  155. line-height: 20px; \
  156. } \
  157. #gmPopupContainer input{ \
  158. width: 350px; \
  159. margin-bottom: 10px; \
  160. } \
  161. #gmPopupContainer h1{ \
  162. font-weight: bold; \
  163. line-height: 30px; \
  164. padding-top: 0px; \
  165. } \
  166. #gmPopupContainer strong{ \
  167. font-weight: bold; \
  168. } \
  169. " );
  170.  
  171. ///// Add Greasemonkey context menu command to edit configuration
  172. GM_registerMenuCommand ("Folder badge configuration", changeConfig);
  173. function changeConfig () {
  174. $("#gmPopupContainer").show ();
  175. }
  176.  
  177.  
  178. ///////////////////////////////
  179.  
  180. ///// Define selectors
  181. function defineSelectorsFromFolders() {
  182. if (foldersShowAllOnBadge!="") {
  183. var selectorsShowAllOnBadge = "[href^='/mail/" + foldersShowAllOnBadge.split(";").join("/?u='],[href^='/mail/") + "/?u=']";
  184. } else {
  185. var selectorsShowAllOnBadge = null
  186. }
  187.  
  188. if (foldersAlwaysShowBadge!="") {
  189. var selectorsAlwaysShowBadge = "[href^='/mail/" + foldersAlwaysShowBadge.split(";").join("/?u='],[href^='/mail/") + "/?u=']";
  190. } else {
  191. var selectorsAlwaysShowBadge = null
  192. }
  193.  
  194. if (foldersNeverShowBadge!="") {
  195. var selectorsNeverShowBadge = "[href^='/mail/" + foldersNeverShowBadge.split(";").join("/?u='],[href^='/mail/") + "/?u=']";
  196. } else {
  197. var selectorsNeverShowBadge = null
  198. }
  199. return {
  200. showTotal: selectorsShowAllOnBadge,
  201. showAlways: selectorsAlwaysShowBadge,
  202. showNever: selectorsNeverShowBadge
  203. };
  204. }
  205. function updateSelectors() {
  206. folderSelectors = defineSelectorsFromFolders();
  207. }
  208.  
  209. var folderSelectors = null;
  210. updateSelectors();
  211.  
  212.  
  213. ///// Functions that control badges
  214. function showAllOnBadge (jNode) {
  215. // Display badge with total number of messages (read and unread).
  216. var nMessagesStr = jNode.prop('title');
  217. var nMessages = nMessagesStr.replace("This folder is empty.","0").replace(/conversation[s]*/, "");
  218. jNode.children(".v-FolderSource-badge").text(nMessages);
  219. if (nMessages!=0) {
  220. jNode.children(".v-FolderSource-badge").removeClass("u-hidden");
  221. } else {
  222. jNode.children(".v-FolderSource-badge").addClass("u-hidden");
  223. }
  224. }
  225. function alwaysShowBadge (jNode) {
  226. // Always display badge, even if it is 0.
  227. jNode.children(".v-FolderSource-badge").removeClass("u-hidden");
  228. }
  229. function neverShowBadge (jNode) {
  230. // Never display badge.
  231. jNode.children(".v-FolderSource-badge").addClass("u-hidden");
  232. }
  233. function refreshCustomBadges (selectors) {
  234. $(selectors.showTotal).each(function() {
  235. showAllOnBadge ($(this));
  236. });
  237. $(selectors.showAlways).each(function() {
  238. alwaysShowBadge ($(this));
  239. });
  240. $(selectors.showNever).each(function() {
  241. neverShowBadge ($(this));
  242. });
  243. }
  244.  
  245.  
  246. ///// Mutation Observer
  247. var targetNodes = $("*"); // Note: there is certainly a narrower observer that could be set. If you figure it out, let me know!
  248. var myObserver = new MutationObserver (mutationHandler);
  249. var obsConfig = { childList: false, characterData: false, attributes: true, subtree: true, attributeFilter:['class'] };
  250.  
  251. //--- Add a target node to the observer. Can only add one node at a time.
  252. targetNodes.each ( function () {
  253. myObserver.observe (this, obsConfig);
  254. } );
  255.  
  256. function mutationHandler (mutationRecords) {
  257. mutationRecords.forEach ( function (mutation) {
  258. if(mutation.target.classList.contains("v-FolderSource")) {
  259. //console.log (mutation.target);
  260. refreshCustomBadges(folderSelectors);
  261. }
  262. } );
  263. }
  264.