about:newtab

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

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

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