Darken

Add menuitem into browser context menu to darken the web page. For Firefox 11.0 and newer only.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

/*
    Darken
    Add menuitem into browser context menu to darken the web page.
    Compatibility: Firefox 11.0 or newer.
    Copyright (C) 2012 LouCypher

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program. If not, see <http://www.gnu.org/licenses/>
*/

// ==UserScript==
// @name            Darken
// @namespace       http://userscripts.org/users/12
// @description     Add menuitem into browser context menu to darken the web page. For Firefox 11.0 and newer only.
// @version         3.0.1
// @author          LouCypher
// @contributor     luckymouse (CSS)
// @license         GPL
// @homepageURL     https://greasyfork.org/scripts/14
// @supportURL      https://greasyfork.org/scripts/14/feedback
// @resource        css https://raw.github.com/LouCypher/userscripts/master/darken/darken.css
// @resource        license https://raw.github.com/LouCypher/userscripts/master/licenses/GPL/LICENSE.txt
// @include         *
// ==/UserScript==

if (frameElement) return;

const STYLE_ID = "userscript-darken-style";
var isDark = sessionStorage.getItem("dark-site");
if (isDark) darken();

if (!(document.head && document.body)) return;
var menu = document.body.appendChild(document.createElement("menu"));
var html = document.documentElement;
if (html.hasAttribute("contextmenu")) {
  // We don't want to override web page context menu if any
  var contextmenu = $("#" + html.getAttribute("contextmenu"));
  contextmenu.appendChild(menu); // Append to web page context menu
} else {
  html.setAttribute("contextmenu", "userscript-darken-context-menu");
}

menu.outerHTML = '<menu id="userscript-darken-context-menu"\
                        type="context">\
                    <menuitem id="userscript-darken-menuitem"\
                              type="checkbox"\
                              label="Darken">\
                    </menuitem>\
                  </menu>';

if ("contextMenu" in html && "HTMLMenuItemElement" in window) {
  var menuitem = $("#userscript-darken-menuitem");
  if (isDark) menuitem.setAttribute("checked", "true");
  // Executed on clicking a menuitem
  menuitem.addEventListener("click", toggleDarken, false);
}

function darken() {
  var style = $("head").appendChild(document.createElement("style"));
  style.id = STYLE_ID;
  style.textContent = GM_getResourceText("css");
  sessionStorage.setItem("dark-site", true);
}

function toggleDarken() {
  var x = pageXOffset;
  var y = pageYOffset
  var styleNode = document.getElementById(STYLE_ID);
  if (styleNode) {
    styleNode.parentNode.removeChild(styleNode);
    sessionStorage.removeItem("dark-site");
  } else {
    darken();
  }
  scrollTo(x, y); // Restore scroll positions
}

function $(aSelector, aNode) {
  return (aNode || document).querySelector(aSelector);
}