auto-click-checkbox-when-debug-mi

调试米家虚拟设备时,修改对应属性值,自动勾选上前面的checkbox

  1. // ==UserScript==
  2. // @name auto-click-checkbox-when-debug-mi
  3. // @name:zh-CN 调试设备时自动选择对应checkbox
  4. // @namespace http://tampermonkey.net/
  5. // @version 0.0.1
  6. // @description 调试米家虚拟设备时,修改对应属性值,自动勾选上前面的checkbox
  7. // @description:zh-CN 调试米家虚拟设备时,修改对应属性值,自动勾选上前面的checkbox
  8. // @author kkopite
  9. // @match https://iot.mi.com/fe-op/productCenter/config/extension/debugger
  10. // @require https://code.jquery.com/jquery-3.6.0.slim.min.js
  11. // @icon https://www.google.com/s2/favicons?sz=64&domain=github.com
  12. // @grant none
  13. // ==/UserScript==
  14.  
  15. (function () {
  16. 'use strict'
  17.  
  18. const map = new Map()
  19.  
  20. const observe = new MutationObserver((mutationsList, observer) => {
  21. mutationsList.forEach(mu => {
  22. if (mu.type === 'characterData') {
  23. const text = mu.target
  24. // 这里观察的是文字节点变化,需要去找到对应的父节点
  25. const parent = text.parentElement.closest('.ant-select')
  26. const label = map.get(parent)
  27. selectLable(label)
  28. } else if (mu.type === 'attributes') {
  29. if (mu.attributeName === 'value'
  30. || mu.attributeName === 'aria-checked') {
  31. const label = map.get(mu.target)
  32. selectLable(label)
  33. }
  34. }
  35. })
  36. })
  37.  
  38. function selectLable(label) {
  39. if (label.classList.contains('ant-checkbox-wrapper-checked')) return
  40. label.click()
  41. }
  42.  
  43. function init() {
  44. const eles = document.querySelectorAll('.ant-table-row');
  45. if (eles.length === 0) {
  46. setTimeout(() => {
  47. init()
  48. }, 1000)
  49. return
  50. }
  51. [...eles].forEach(ele => {
  52. const label = ele.querySelector('.ant-checkbox-wrapper')
  53. const target = ele.querySelector('.ant-switch')
  54. || ele.querySelector('.ant-select')
  55. || ele.querySelector('.ant-input')
  56. || ele.querySelector('.ant-input-number-input')
  57. map.set(target, label)
  58. if (target.classList.contains('ant-select')) {
  59. observe.observe(target, {
  60. characterData: true,
  61. subtree: true
  62. })
  63. } else {
  64. observe.observe(target, {
  65. attributes: true
  66. })
  67. }
  68. })
  69. }
  70.  
  71. init()
  72.  
  73. })()