about:newtab

Add input fields to change rows and columns setting on about:newtab page. For Scriptish only.

当前为 2014-03-07 提交的版本,查看 最新版本

  1. /*
  2. * This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  5. *
  6. * Contributor(s):
  7. * - LouCypher (original code)
  8. */
  9.  
  10. // ==UserScript==
  11. // @id about-newtab@loucypher
  12. // @name about:newtab
  13. // @namespace http://mozilla.status.net/loucypher
  14. // @description Add input fields to change rows and columns setting on about:newtab page. For Scriptish only.
  15. // @version 3.2
  16. // @author LouCypher
  17. // @contributor Benjamin Humphrey - icons http://findicons.com/icon/554396/64_thumbnails
  18. // @license MPL 2.0
  19. // @icon https://raw.github.com/LouCypher/userscripts/master/scriptish/about-new-tab/icon32.png
  20. // @icon64URL https://raw.github.com/LouCypher/userscripts/master/scriptish/about-new-tab/icon64.png
  21. // @contributionURL http://loucypher.github.io/userscripts/donate.html?about%3Anewtab
  22. // @homepageURL https://greasyfork.org/scripts/7
  23. // @supportURL https://greasyfork.org/scripts/7/feedback
  24. // @resource favicon https://raw.github.com/LouCypher/userscripts/master/scriptish/about-new-tab/favicon.ico
  25. // @resource CSS https://raw.github.com/LouCypher/userscripts/master/scriptish/about-new-tab/about-new-tab.css
  26. // @resource HTML https://raw.github.com/LouCypher/userscripts/master/scriptish/about-new-tab/about-new-tab.html
  27. // @resource CHANGELOG https://raw.github.com/LouCypher/userscripts/master/scriptish/about-new-tab/CHANGELOG.txt
  28. // @resource LICENSE https://raw.github.com/LouCypher/userscripts/master/licenses/MPL/LICENSE.txt
  29. // @include about:newtab
  30. // @include chrome://browser/content/newtab/newTab.xul
  31. // @run-at document-end
  32. // @grant GM_getResourceText
  33. // @grant GM_getResourceURL
  34. // ==/UserScript==
  35.  
  36. /* I can't use `setAttribute` on `Application.activeWindow.activeTab` for the active tab
  37. so I'm using `Application.activeWindow.tabs[0]._tabbrowser.mCurrentTab` */
  38. let browser = Application.activeWindow.tabs[0]._tabbrowser;
  39. let tab = browser.mCurrentTab;
  40. if (browser.mCurrentBrowser.currentURI.spec == "about:newtab")
  41. tab.setAttribute("scriptish-url", "about:newtab");
  42.  
  43. // Set favicon. Couldn't be done using DOM method, so I cheated with nsIStyleSheetService.
  44. let css = '@namespace url("' + document.documentElement.namespaceURI + '");'
  45. + 'tab[scriptish-url="about:newtab"] .tab-icon-image {'
  46. + 'list-style-image: url(' + GM_getResourceURL("favicon") + ') !important;}'
  47. let uri = Services.io.newURI("data:text/css,/*about:newtab userscript*/%0A" +
  48. encodeURIComponent(css), null, null);
  49. let sss = Components.classes["@mozilla.org/content/style-sheet-service;1"]
  50. .getService(Ci.nsIStyleSheetService);
  51. if (!sss.sheetRegistered(uri, sss.USER_SHEET))
  52. sss.loadAndRegisterSheet(uri, sss.USER_SHEET);
  53.  
  54. /***** Begin initializations *****/
  55.  
  56. // Inject style. Can't use GM_addStyle here
  57. let style = document.documentElement.appendChild(document.createElementNS(HTML_NAMESPACE, "style"));
  58. style.type = "text/css";
  59. style.textContent = GM_getResourceText("CSS");
  60.  
  61. let divS = (new DOMParser).parseFromString(GM_getResourceText("HTML"), "application/xml")
  62. .documentElement;
  63.  
  64. if (typeof newTabTools === "object") { // If New Tab Tools extension is active
  65. gAllPages.enabled = true; // Always show thumbnails
  66.  
  67. $("#config-inner").insertBefore(divS, $("#config-morePrefs"));
  68. $("#config-inner").insertBefore(document.createElement("spacer"), $("#config-morePrefs"));
  69.  
  70. let label = $("#config-inner").insertBefore(document.createElement("label"), divS);
  71. label.className = "header";
  72. label.value = "Rows and Columns:";
  73.  
  74. let spacers = document.querySelectorAll("#config-inner > spacer");
  75. for (let i = 0; i < spacers.length; i++) {
  76. spacers[i].style.height = "2em";
  77. }
  78. $("#config-title-input") && $("#config-title-input").removeAttribute("flex");
  79. $("#config-morePrefs").style.color = "inherit";
  80. }
  81. else
  82. $("#newtab-horizontal-margin").insertBefore(divS, $(".newtab-side-margin:last-child"));
  83.  
  84. ["#setting-columns", "#setting-rows"].forEach(function(aSelector) {
  85. $(aSelector).value = getIntPref(aSelector.match(/[a-z]+$/).toString());
  86. $(aSelector).addEventListener("change", setValueFromInput);
  87. $(aSelector).addEventListener("DOMMouseScroll", mouseWheel); // Change value with mouse scroll
  88. })
  89.  
  90. $('#newtab-form input[type="reset"]').addEventListener("click", function() {
  91. setIntPref("columns", $("#setting-columns").value = 3);
  92. $("#setting-columns").classList.add("default-value");
  93. setIntPref("rows", $("#setting-rows").value = 3);
  94. $("#setting-rows").classList.add("default-value");
  95. })
  96.  
  97. $('#newtab-form input[type="button"]').addEventListener("click", function() {
  98. gAllPages.enabled = !gAllPages.enabled;
  99. })
  100.  
  101. /***** End initializations *****/
  102.  
  103. function setIntPref(aRowsOrColumns, aInt) {
  104. Services.prefs.setIntPref("browser.newtabpage." + aRowsOrColumns, aInt);
  105. }
  106.  
  107. function getIntPref(aRowsOrColumns, aInt) {
  108. return Services.prefs.getIntPref("browser.newtabpage." + aRowsOrColumns);
  109. }
  110.  
  111. // Save input value to preference
  112. function setValueFromInput(aEvent) {
  113. let input = aEvent.target;
  114. let value = input.value;
  115. if (isNaN(value)) input.value = 3;
  116. else if (value < 1) input.value = 1; // min = 1
  117. else if (value > 50) input.value = 50; // max = 50
  118.  
  119. if (input.value == 3)
  120. input.classList.add("default-value");
  121. else
  122. input.classList.remove("default-value");
  123. setIntPref(input.id.match(/[a-z]+$/).toString(), input.value);
  124. }
  125.  
  126. // Use mouse scroll to increase/decrease value in text input
  127. // https://developer.mozilla.org/DOM/DOM_event_reference/DOMMouseScroll
  128. function mouseWheel(aEvent) {
  129. let input = aEvent.target;
  130. if (aEvent.detail > 0) { // Scroll down
  131. if (input.value > 1) input.value--; // Decrease number if value > 1
  132. else aEvent.stopPropagation(); // else stop (min = 1)
  133. }
  134. else { // Scroll up
  135. if (input.value < 50) input.value++; // Increase number if value < 50
  136. else aEvent.stopPropagation(); // else stop (max = 50)
  137. }
  138. setValueFromInput(aEvent);
  139. }
  140.  
  141. function $(aSelector, aNode) {
  142. return (aNode || document).querySelector(aSelector);
  143. }