极客夜间模式

夜间模式

  1. // ==UserScript==
  2. // @name geek dark mode
  3. // @name:zh-CN 极客夜间模式
  4. // @description dark mode
  5. // @description:zh-cn 夜间模式
  6. // @version 1.1
  7. // @author Anc
  8. // @match *://*/*
  9. // @exclude *://*localhost*
  10. // @exclude *://*127.0.0.1*
  11. // @run-at document.start
  12. // @grant GM.addStyle
  13. // @grant GM_registerMenuCommand
  14. // @grant GM_setValue
  15. // @grant GM_getValue
  16. // @noframes
  17. // @namespace https://greasyfork.org/users/61607
  18. // ==/UserScript==
  19.  
  20. (function() {
  21. 'use strict';
  22. // Add meta tag
  23. let meta = document.createElement('meta');
  24. meta.name = "theme-color";
  25. meta.content = "#000";
  26. meta.media = "(prefers-color-scheme: dark)";
  27. document.head.append(meta);
  28.  
  29. function unsetFilter() {
  30. var elements = document.getElementsByTagName('*');
  31. for (var i = 0; i < elements.length; i++) {
  32. elements[i].style.filter = 'unset';
  33. }
  34. }
  35.  
  36. // Function to create a button
  37. function createButton() {
  38. var button = document.createElement('button');
  39. button.textContent = 'White';
  40.  
  41. button.style.position = 'fixed';
  42. button.style.top = '10px';
  43. button.style.right = '10px';
  44. button.style.padding = '10px';
  45. button.style.backgroundColor = '#007bff';
  46. button.style.color = '#ffffff';
  47. button.style.border = 'none';
  48. button.style.borderRadius = '5px';
  49. button.style.cursor = 'pointer';
  50. button.style.zIndex = '999';
  51.  
  52. document.body.appendChild(button);
  53.  
  54. button.addEventListener('click', unsetFilter);
  55. }
  56.  
  57.  
  58. // Function to add the current website's domain to the array and save it
  59. function addDomainToArray() {
  60. var domain = window.location.hostname;
  61. var trackedDomains = GM_getValue('trackedDomains', []);
  62.  
  63. // Check if domain already exists in the array
  64. var index = trackedDomains.indexOf(domain);
  65. if ( index === -1) {
  66. trackedDomains.push(domain);
  67. GM_setValue('trackedDomains', trackedDomains);
  68. console.log('Domain ' + domain + ' added to tracking list.');
  69. unsetFilter();
  70. } else {
  71. console.log('Domain ' + domain + ' is already in tracking list, remove it.');
  72. trackedDomains.splice(index, 1);
  73. GM_setValue('trackedDomains', trackedDomains);
  74. console.log("trackedDomains", trackedDomains);
  75. checkDomain();
  76. }
  77. }
  78.  
  79. // Function to check if the current domain is in the array and alert if so
  80. function checkDomain() {
  81. var domain = window.location.hostname;
  82. var trackedDomains = GM_getValue('trackedDomains', []);
  83. console.log("trackedDomains", trackedDomains);
  84.  
  85. if (trackedDomains.indexOf(domain) === -1){
  86. console.log('dark domain: ' + domain);
  87.  
  88. // Add style
  89. GM.addStyle(`
  90. @media (prefers-color-scheme: dark) {
  91. :root {
  92. filter: invert(1) hue-rotate(180deg);
  93. }
  94. figure,img,video,iframe,div[style*=image]{
  95. filter: invert(1) hue-rotate(180deg);
  96. opacity:1;
  97. }
  98.  
  99. figure img {
  100. filter: unset;
  101. }
  102. }
  103. `)
  104.  
  105. if(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
  106. createButton();
  107. }
  108.  
  109. }
  110. }
  111.  
  112. // Register a menu command to add the current domain to the tracking list
  113. GM_registerMenuCommand('Add/Reomove', addDomainToArray);
  114.  
  115. // Check the domain when the page loads
  116. checkDomain();
  117. })();