about:newtab

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

目前為 2014-02-20 提交的版本,檢視 最新版本

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

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址