Star Citizen RSI Specturm - Auto Dark Mode

2024-07-28

  1. // ==UserScript==
  2. // @name Star Citizen RSI Specturm - Auto Dark Mode
  3. // @namespace Kyan Violentmonkey Scripts
  4. // @match *://robertsspaceindustries.com/spectrum/*
  5. // @grant none
  6. // @version 1.0.2
  7. // @license MIT
  8. // @author Kyan
  9. // @description 2024-07-28
  10. // ==/UserScript==
  11. ;(function () {
  12. 'use strict';
  13.  
  14. const is_dark = () => document.querySelector("#app").classList.contains("theme-dark")
  15.  
  16. const is_light = () => document.querySelector("#app").classList.contains("theme-light")
  17.  
  18. const to_dark = (switch_ele) => { if (is_light()) switch_ele.click() }
  19.  
  20. const to_light = (switch_ele) => { if (is_dark()) switch_ele.click() }
  21.  
  22. const is_prefer_dark = () => window.matchMedia("(prefers-color-scheme: dark)").matches
  23.  
  24. const is_prefer_light = () => window.matchMedia("(prefers-color-scheme: light)").matches
  25.  
  26. const when_element_exist = (selector, callback) => {
  27. let timer = window.setInterval(() => {
  28. if (document.querySelectorAll(selector).length > 0) {
  29. clearInterval(timer)
  30. callback()
  31. }
  32. }, 1000)
  33. window.setTimeout(() => clearInterval(timer), 15000)
  34. }
  35.  
  36. const theme_switch_selector = "label[for='toggle-button-checkbox']"
  37. when_element_exist(theme_switch_selector, () => {
  38. // Find elements that needed
  39. if (document.querySelector("#app") === null) {
  40. console.error("Cannot find #app element")
  41. return
  42. }
  43.  
  44. var theme_switch = document.querySelector(theme_switch_selector)
  45. if (theme_switch === null) {
  46. console.error(`Connot find ${theme_switch_selector} element`)
  47. return
  48. }
  49.  
  50. if (is_dark() && is_prefer_light()) {
  51. to_light(theme_switch)
  52. } else if (is_light() && is_prefer_dark()) {
  53. to_dark(theme_switch)
  54. } else {
  55. console.log(`[Auto Dark Mode] Prefer: ${is_prefer_light()?"✓":"✗"}Light, ${is_prefer_dark()?"✓":"✗"}Dark`)
  56. console.log(`[Auto Dark Mode] Current: ${is_light()?"✓":"✗"}Light, ${is_dark()?"✓":"✗"}Dark`)
  57. }
  58. })
  59. })()

QingJ © 2025

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