Gmail Basic Select All

Add select all/none button to Gmail Basic

  1. // ==UserScript==
  2. // @name Gmail Basic Select All
  3. // @namespace mailto:badocelot@badocelot.com
  4. // @description Add select all/none button to Gmail Basic
  5. // @include https://mail.google.com/mail/*/h/*
  6. // @include https://mail.google.com/mail/h/*
  7. // @version 3
  8. // @license MIT/Expat
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. /* User script to add a "select all/none" button to Gmail Basic
  13. *
  14. * @licstart The following is the entire license notice for the JavaScript
  15. * code in this file.
  16. *
  17. * Copyright (C) 2016 James M. Jensen II
  18. *
  19. * Permission is hereby granted, free of charge, to any person obtaining
  20. * a copy of this software and associated documentation files (the
  21. * "Software"), to deal in the Software without restriction, including
  22. * without limitation the rights to use, copy, modify, merge, publish,
  23. * distribute, sublicense, and/or sell copies of the Software, and to
  24. * permit persons to whom the Software is furnished to do so, subject to
  25. * the following conditions:
  26. *
  27. * The above copyright notice and this permission notice shall be included
  28. * in all copies or substantial portions of the Software.
  29. *
  30. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  31. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  32. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  33. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  34. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  35. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  36. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  37. *
  38. * @licend The following is the entire license notice for the JavaScript
  39. * code in this file.
  40. */
  41.  
  42. // So, the way this works is we look up the buttons that start the bars
  43. // above and below the email list, and for each one we create a button to
  44. // inject before the archive button.
  45. //
  46. // TODO: Get this working on Sent Mail page, where the bars don't start
  47. // with a button or other named item. Contacts would also be nice.
  48. //
  49. // When the button is clicked, it loops through the checkboxes on each
  50. // conversation and counts how many are checked or unchecked. If the majority
  51. // are checked, it unchecks all of them, and vice versa.
  52.  
  53. function toggleBoxes () {
  54. var i;
  55. var checkboxes = document.getElementsByName('t');
  56.  
  57. // Check whether majority are checked or unchecked to determine
  58. // which way to toggle them
  59. var num_checked = 0;
  60. var num_unchecked = 0;
  61. for (i = 0; i < checkboxes.length; i++) {
  62. if (checkboxes[i].checked) {
  63. num_checked++;
  64. } else {
  65. num_unchecked++;
  66. }
  67. }
  68.  
  69. var shouldCheck;
  70. if (num_unchecked < num_checked) {
  71. shouldCheck = false;
  72. } else {
  73. shouldCheck = true;
  74. }
  75.  
  76. // (un)check all checkboxes
  77. for (i = 0; i < checkboxes.length; i++) {
  78. var c = checkboxes[i];
  79. c.checked = shouldCheck;
  80. }
  81. }
  82.  
  83. var i = 0;
  84.  
  85. // Figure out what the first button on the top and bottom bars is, checking
  86. // the following in order:
  87. // * Archive (nvp_a_arch)
  88. // * Remove Star (nvp_bu_rs)
  89. // * Discard Drafts (nvp_a_dd)
  90. // * Delete Forever (nvp_a_dl)
  91. // * Remove label (nvp_bu_rl)
  92. // * Move to Inbox (nvp_a_ib)
  93. var buttonNames = [
  94. 'nvp_a_arch',
  95. 'nvp_bu_rs',
  96. 'nvp_a_dd',
  97. 'nvp_a_dl',
  98. 'nvp_bu_rl',
  99. 'nvp_a_ib'
  100. ];
  101. var firstButtons = [];
  102. while (i < buttonNames.length && firstButtons.length === 0) {
  103. firstButtons = document.getElementsByName(buttonNames[i]);
  104. i++;
  105. }
  106.  
  107. for (i = 0; i < firstButtons.length; i++) {
  108. var btn = firstButtons[i];
  109. var toggle = document.createElement('button');
  110. toggle.type = 'button';
  111. toggle.innerHTML = '&#x2714;';
  112. // hack to fix the right-side gap -- I have no idea why this is necessary
  113. toggle.style.marginRight = '6px';
  114. toggle.onclick = toggleBoxes;
  115. var buttonRow = btn.parentElement;
  116. buttonRow.insertBefore(toggle, btn);
  117. }