Greasy Fork 支持简体中文。

Fastmail - Folder Badge Controls

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

目前為 2015-04-23 提交的版本,檢視 最新版本

  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. // @namespace https://michaelstepner.com
  27. // @author Michael Stepner
  28. // @description Turn badges on or off for specific folders, or show total # of messages.
  29. // @include https://www.fastmail.com/mail/*
  30. // @version 1.0.0
  31. // @require https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
  32. // @grant GM_getValue
  33. // @grant GM_setValue
  34. // @grant GM_registerMenuCommand
  35. // @grant GM_addStyle
  36. // ==/UserScript==
  37.  
  38.  
  39. ///// Retrieve configuration
  40. var foldersShowAllOnBadge = GM_getValue ("foldersShowAllOnBadge", "");
  41. var foldersAlwaysShowBadge = GM_getValue ("foldersAlwaysShowBadge", "");
  42. var foldersNeverShowBadge = GM_getValue ("foldersNeverShowBadge", "");
  43. var firstConfig = GM_getValue ("firstConfig", "");
  44.  
  45.  
  46. ///// Create a configuration form in a popup box
  47.  
  48. // Insert HTML for the popup box
  49. $("body[contenteditable!='true']").append ( ' \
  50. <div id="gmPopupContainer"> \
  51. <form> <!-- For true form use method="POST" action="YOUR_DESIRED_URL" --> \
  52. <h1>Instructions:</h1> \
  53. <p>In each entry, list the folders whose badge you want to modify.</p> \
  54. <p>Separate each folder name by a semicolon.</p> \
  55. <br><p>You need to specify folders using the name in their URL:</p> \
  56. <p>&nbsp;&nbsp;&nbsp;https://www.fastmail.com/mail/<b>Folder_Name</b>/?u=0xx00000</p> \
  57. <br><p id="gmMenuCommandInstructions">&nbsp;</p> \
  58. <br><h1>Configuration:</h1> \
  59. <p>Show <strong>total</strong> number of messages (read and unread) on badge:</p> \
  60. <input type="text" id="gmConfigShowTotal"> \
  61. <p><strong>Always</strong> show badge:</p> \
  62. <input type="text" id="gmConfigShowAlways" value=""> \
  63. <p><strong>Never</strong> show badge:</p> \
  64. <input type="text" id="gmConfigShowNever" value=""> \
  65. <br><button id="gmClosePopupAndCancel" type="button">Cancel</button> \
  66. <button id="gmClosePopupAndSave" type="button"><strong>Submit</strong></button> \
  67. </form> \
  68. </div> \
  69. ' );
  70.  
  71. // Detect whether running in Greasemonkey or Tampermonkey
  72. var scriptEngine;
  73. if (typeof GM_info === "undefined") {
  74. scriptEngine = "plain";
  75. // plain Chrome, or Opera, or scriptish, or Safari, or rarer
  76. }
  77. else {
  78. scriptEngine = GM_info.scriptHandler || "Greasemonkey";
  79. }
  80.  
  81. // Write correct instructions for Greasemonkey or Tampermonkey
  82. if (scriptEngine=="Greasemonkey") {
  83. 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";
  84. }
  85. else if (scriptEngine=="Tampermonkey") {
  86. 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'.";
  87. }
  88. else {
  89. 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.";
  90. }
  91.  
  92. // Hide the popup box unless config has never been done before
  93. if (firstConfig=="done") {
  94. $("#gmPopupContainer").hide ();
  95. }
  96.  
  97. // Set the input boxes to contain current config values
  98. function setPopupConfigValues () {
  99. document.getElementById('gmConfigShowTotal').value=foldersShowAllOnBadge ;
  100. document.getElementById('gmConfigShowAlways').value=foldersAlwaysShowBadge ;
  101. document.getElementById('gmConfigShowNever').value=foldersNeverShowBadge ;
  102. }
  103. setPopupConfigValues ();
  104.  
  105. // Save the new config values on 'Submit'
  106. $("#gmClosePopupAndSave").click ( function () {
  107. foldersShowAllOnBadge = document.getElementById('gmConfigShowTotal').value ;
  108. foldersAlwaysShowBadge = document.getElementById('gmConfigShowAlways').value ;
  109. foldersNeverShowBadge = document.getElementById('gmConfigShowNever').value ;
  110. updateConfig();
  111. updateSelectors();
  112. $("#gmPopupContainer").hide ();
  113. refreshCustomBadges(folderSelectors);
  114. } );
  115. function updateConfig () {
  116. GM_setValue ("foldersShowAllOnBadge", foldersShowAllOnBadge);
  117. GM_setValue ("foldersAlwaysShowBadge", foldersAlwaysShowBadge);
  118. GM_setValue ("foldersNeverShowBadge", foldersNeverShowBadge);
  119. GM_setValue ("firstConfig", "done");
  120. }
  121.  
  122. // Restore popup text entry values on 'Cancel'
  123. $("#gmClosePopupAndCancel").click ( function () {
  124. setPopupConfigValues ();
  125. $("#gmPopupContainer").hide ();
  126. } );
  127.  
  128. // Style the config popup box with CSS
  129. GM_addStyle ( " \
  130. #gmPopupContainer { \
  131. position: fixed; \
  132. top: 20%; \
  133. left: 20%; \
  134. padding: 1em 2em; \
  135. background: #C0C0C0; \
  136. border: 3px double black; \
  137. border-radius: 1ex; \
  138. z-index: 777; \
  139. } \
  140. #gmPopupContainer button{ \
  141. cursor: pointer; \
  142. margin: 1em 1em 0; \
  143. padding-top: 5px; \
  144. padding-right: 5px; \
  145. padding-bottom: 5px; \
  146. padding-left: 5px; \
  147. border: 2px outset buttonface; \
  148. margin-left: 75px; \
  149. } \
  150. #gmPopupContainer p{ \
  151. line-height: 20px; \
  152. } \
  153. #gmPopupContainer input{ \
  154. width: 350px; \
  155. margin-bottom: 10px; \
  156. } \
  157. #gmPopupContainer h1{ \
  158. font-weight: bold; \
  159. line-height: 30px; \
  160. padding-top: 0px; \
  161. } \
  162. #gmPopupContainer strong{ \
  163. font-weight: bold; \
  164. } \
  165. " );
  166.  
  167. ///// Add Greasemonkey context menu command to edit configuration
  168. GM_registerMenuCommand ("Folder badge configuration", changeConfig);
  169. function changeConfig () {
  170. $("#gmPopupContainer").show ();
  171. }
  172.  
  173.  
  174. ///////////////////////////////
  175.  
  176. ///// Define selectors
  177. function defineSelectorsFromFolders() {
  178. if (foldersShowAllOnBadge!="") {
  179. var selectorsShowAllOnBadge = "[href^='/mail/" + foldersShowAllOnBadge.split(";").join("/?u='],[href^='/mail/") + "/?u=']";
  180. } else {
  181. var selectorsShowAllOnBadge = null
  182. }
  183.  
  184. if (foldersAlwaysShowBadge!="") {
  185. var selectorsAlwaysShowBadge = "[href^='/mail/" + foldersAlwaysShowBadge.split(";").join("/?u='],[href^='/mail/") + "/?u=']";
  186. } else {
  187. var selectorsAlwaysShowBadge = null
  188. }
  189.  
  190. if (foldersNeverShowBadge!="") {
  191. var selectorsNeverShowBadge = "[href^='/mail/" + foldersNeverShowBadge.split(";").join("/?u='],[href^='/mail/") + "/?u=']";
  192. } else {
  193. var selectorsNeverShowBadge = null
  194. }
  195. return {
  196. showTotal: selectorsShowAllOnBadge,
  197. showAlways: selectorsAlwaysShowBadge,
  198. showNever: selectorsNeverShowBadge
  199. };
  200. }
  201. function updateSelectors() {
  202. folderSelectors = defineSelectorsFromFolders();
  203. }
  204.  
  205. var folderSelectors = null;
  206. updateSelectors();
  207.  
  208.  
  209. ///// Functions that control badges
  210. function showAllOnBadge (jNode) {
  211. // Display badge with total number of messages (read and unread).
  212. var nMessagesStr = jNode.prop('title');
  213. var nMessages = nMessagesStr.replace("This folder is empty.","0").replace(/conversation[s]*/, "");
  214. jNode.children(".v-FolderSource-badge").text(nMessages);
  215. if (nMessages!=0) {
  216. jNode.children(".v-FolderSource-badge").removeClass("u-hidden");
  217. } else {
  218. jNode.children(".v-FolderSource-badge").addClass("u-hidden");
  219. }
  220. }
  221. function alwaysShowBadge (jNode) {
  222. // Always display badge, even if it is 0.
  223. jNode.children(".v-FolderSource-badge").removeClass("u-hidden");
  224. }
  225. function neverShowBadge (jNode) {
  226. // Never display badge.
  227. jNode.children(".v-FolderSource-badge").addClass("u-hidden");
  228. }
  229. function refreshCustomBadges (selectors) {
  230. $(selectors.showTotal).each(function() {
  231. showAllOnBadge ($(this));
  232. });
  233. $(selectors.showAlways).each(function() {
  234. alwaysShowBadge ($(this));
  235. });
  236. $(selectors.showNever).each(function() {
  237. neverShowBadge ($(this));
  238. });
  239. }
  240.  
  241.  
  242. ///// Mutation Observer
  243. var targetNodes = $("*"); // Note: there is certainly a narrower observer that could be set. If you figure it out, let me know!
  244. var myObserver = new MutationObserver (mutationHandler);
  245. var obsConfig = { childList: false, characterData: false, attributes: true, subtree: true, attributeFilter:['class'] };
  246.  
  247. //--- Add a target node to the observer. Can only add one node at a time.
  248. targetNodes.each ( function () {
  249. myObserver.observe (this, obsConfig);
  250. } );
  251.  
  252. function mutationHandler (mutationRecords) {
  253. mutationRecords.forEach ( function (mutation) {
  254. if(mutation.target.classList.contains("v-FolderSource")) {
  255. //console.log (mutation.target);
  256. refreshCustomBadges(folderSelectors);
  257. }
  258. } );
  259. }
  260.