geek dark mode

dark mode

当前为 2024-05-07 提交的版本,查看 最新版本

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