FURSTREAM background toggle

Adds button to switch background to dark or light colored

当前为 2016-08-06 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name FURSTREAM background toggle
  3. // @namespace furstre.am
  4. // @description Adds button to switch background to dark or light colored
  5. // @include https://furstre.am/stream/*
  6. // @version 1
  7. // @grant none
  8. // ==/UserScript==
  9.  
  10. /* BEGIN INLINE CODE FROM: https://gist.githubusercontent.com/arantius/3123124/raw/grant-none-shim.js */
  11. /*
  12. The MIT License (MIT)
  13.  
  14. Copyright (c) 2014 Anthony Lieuallen
  15.  
  16. Permission is hereby granted, free of charge, to any person obtaining a copy
  17. of this software and associated documentation files (the "Software"), to deal
  18. in the Software without restriction, including without limitation the rights
  19. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  20. copies of the Software, and to permit persons to whom the Software is
  21. furnished to do so, subject to the following conditions:
  22.  
  23. The above copyright notice and this permission notice shall be included in
  24. all copies or substantial portions of the Software.
  25.  
  26. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  29. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  30. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  31. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  32. THE SOFTWARE.
  33.  
  34.  
  35. This script is intended to be used with @require, for Greasemonkey scripts
  36. using "@grant none". It emulates the GM_ APIs as closely as possible, using
  37. modern browser features like DOM storage.
  38.  
  39. Scripts should plan to remove usage of GM_ APIs, but this shim offers a
  40. short-term workaround to gain the benefits of running in the security
  41. restriction free "@grant none" mode before that is completed.
  42.  
  43. Read the comments on each function to learn if its emulation is good enough
  44. for your purposes.
  45.  
  46. NOT IMPLEMENTED:
  47. * GM_getResourceText
  48. * GM_openInTab
  49. * GM_registerMenuCommand
  50. */
  51. const __GM_STORAGE_PREFIX = [
  52. '', GM_info.script.namespace, GM_info.script.name, ''].join('***');
  53.  
  54. // All of the GM_*Value methods rely on DOM Storage's localStorage facility.
  55. // They work like always, but the values are scoped to a domain, unlike the
  56. // original functions. The content page's scripts can access, set, and
  57. // remove these values. A
  58. function GM_getValue(aKey, aDefault) {
  59. 'use strict';
  60. let val = localStorage.getItem(__GM_STORAGE_PREFIX + aKey)
  61. if (null === val && 'undefined' != typeof aDefault) return aDefault;
  62. return val;
  63. }
  64.  
  65. function GM_setValue(aKey, aVal) {
  66. 'use strict';
  67. localStorage.setItem(__GM_STORAGE_PREFIX + aKey, aVal);
  68. }
  69. /* END INLINE CODE FROM: https://gist.githubusercontent.com/arantius/3123124/raw/grant-none-shim.js */
  70.  
  71. function addGlobalStyle(css, id) {
  72. var style = $(document.createElement("style")).attr("type", "text/css").html(css);
  73. if(id) style.attr("id", id);
  74. $("head").append(style);
  75. }
  76.  
  77. function toggleBackground() {
  78. var style = $("#dark-background");
  79. if(style.length) {
  80. do {
  81. style.remove();
  82. style = $("#dark-background");
  83. } while (style.length);
  84. GM_setValue("Background", "light");
  85. }
  86. else {
  87. addGlobalStyle("body { color: #ddd !important; }", "dark-background");
  88. addGlobalStyle(".body .center { background-color: rgba(0, 0, 0, 0.8) !important; }", "dark-background");
  89. addGlobalStyle(".comunica{ color: #fff !important; }", "dark-background");
  90. addGlobalStyle(".comunica .comunica-top { background-color: #444 !important; }", "dark-background");
  91. addGlobalStyle(".comunica .comunica-top .button { color: rgba(255, 255, 255, 0.5) !important; }", "dark-background");
  92. addGlobalStyle(".comunica #comunica-chat-pane { background-color: #222 !important; color: #fff !important; }", "dark-background");
  93. addGlobalStyle(".comunica #comunica-chat-pane .msg .content { background-color: #444 !important; }", "dark-background");
  94. addGlobalStyle(".comunica .comunica-msg-input { background-color: #444 !important; color: #eee; }", "dark-background");
  95. addGlobalStyle(".comunica .comunica-smiles { opacity: 0.8 !important; }", "dark-background");
  96. addGlobalStyle(".comunica .comunica-menu { background-color: #444 !important; }", "dark-background");
  97. GM_setValue("Background", "dark");
  98. }
  99. }
  100.  
  101. $(window).load(function(){
  102. $("#chat .comunica .comunica-top").append($(document.createElement("i")).addClass("fa fa-adjust fa-2x fa-fw button").click(toggleBackground));
  103. if (GM_getValue("Background", "light") === "dark") {
  104. toggleBackground ();
  105. }
  106. })
  107.