Auto dark mode

Invert the whold website

  1. // ==UserScript==
  2. // @name Auto dark mode
  3. // @namespace https://mmis1000.me/
  4. // @version 0.4
  5. // @description Invert the whold website
  6. // @author MMis1000
  7. // @include http://*
  8. // @include https://*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. var inIframe = window.top !== window.self
  16.  
  17. function invert(color, strength = 0.9) {
  18. var gamma = 1
  19. var [r, g, b, a] = /rgba?\((.+?),(.+?),(.+?)(?:,(.+?))?\)/.exec(color).slice(1).map(Number)
  20. var invertedRUncompressed = (255 ** gamma - r ** gamma) * strength + (r ** gamma) * (1 - strength)
  21. var invertedGUncompressed = (255 ** gamma - g ** gamma) * strength + (g ** gamma) * (1 - strength)
  22. var invertedBUncompressed = (255 ** gamma - b ** gamma) * strength + (b ** gamma) * (1 - strength)
  23.  
  24. var invertedR = ~~(invertedRUncompressed ** (1 / gamma))
  25. var invertedG = ~~(invertedGUncompressed ** (1 / gamma))
  26. var invertedB = ~~(invertedBUncompressed ** (1 / gamma))
  27.  
  28. var newColor = `rgba(${invertedR},${invertedG},${invertedB},${a || 1})`
  29. console.log(newColor)
  30. return newColor
  31. }
  32.  
  33. var root = document.querySelector(':root')
  34. var body = document.querySelector('body')
  35. var rootStyle = window.getComputedStyle(root)
  36. var bodyStyle = window.getComputedStyle(body)
  37. var style = ''
  38.  
  39. var background = null
  40.  
  41. if (rootStyle.backgroundColor === 'rgba(0, 0, 0, 0)' && bodyStyle.backgroundColor === 'rgba(0, 0, 0, 0)') {
  42. background = invert('rgba(255,255,255,1)')
  43. } else if (rootStyle.backgroundColor !== 'rgba(0, 0, 0, 0)') {
  44. background = invert(rootStyle.backgroundColor)
  45. } else {
  46. background = invert(bodyStyle.backgroundColor)
  47. }
  48.  
  49. if (inIframe) {
  50. // we don't need background and yet another invert in iframe
  51. style = `
  52. svg, img, video, canvas {
  53. filter: hue-rotate(180deg) invert(100%);
  54. }
  55. `
  56. } else {
  57. style = `
  58. :root {
  59. background-color: ${background} !important;
  60. filter: invert(90%) hue-rotate(180deg);
  61. }
  62.  
  63. svg, img, video, canvas {
  64. filter: hue-rotate(180deg) invert(100%);
  65. }
  66. `
  67. }
  68.  
  69. var styleEl = document.createElement('style');
  70. styleEl.textContent = style
  71. document.head.appendChild(styleEl)
  72. })();

QingJ © 2025

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