auto-click-checkbox-when-debug-mi

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name                auto-click-checkbox-when-debug-mi 
// @name:zh-CN          调试设备时自动选择对应checkbox
// @namespace           http://tampermonkey.net/
// @version             0.0.1
// @description         调试米家虚拟设备时,修改对应属性值,自动勾选上前面的checkbox
// @description:zh-CN   调试米家虚拟设备时,修改对应属性值,自动勾选上前面的checkbox
// @author              kkopite
// @match               https://iot.mi.com/fe-op/productCenter/config/extension/debugger
// @require             https://code.jquery.com/jquery-3.6.0.slim.min.js
// @icon                https://www.google.com/s2/favicons?sz=64&domain=github.com
// @grant               none
// ==/UserScript==

(function () {
  'use strict'

  const map = new Map()

  const observe = new MutationObserver((mutationsList, observer) => {
    mutationsList.forEach(mu => {
      if (mu.type === 'characterData') {
        const text = mu.target
        // 这里观察的是文字节点变化,需要去找到对应的父节点
        const parent = text.parentElement.closest('.ant-select')
        const label = map.get(parent)
        selectLable(label)
      } else if (mu.type === 'attributes') {
        if (mu.attributeName === 'value'
          || mu.attributeName === 'aria-checked') {
          const label = map.get(mu.target)
          selectLable(label)
        }
      }
    })
  })

  function selectLable(label) {
    if (label.classList.contains('ant-checkbox-wrapper-checked')) return
    label.click()
  }

  function init() {
    const eles = document.querySelectorAll('.ant-table-row');
    if (eles.length === 0) {
      setTimeout(() => {
        init()
      }, 1000)
      return
    }
    [...eles].forEach(ele => {
      const label = ele.querySelector('.ant-checkbox-wrapper')
      const target = ele.querySelector('.ant-switch')
        || ele.querySelector('.ant-select')
        || ele.querySelector('.ant-input')
        || ele.querySelector('.ant-input-number-input')
      map.set(target, label)
      if (target.classList.contains('ant-select')) {
        observe.observe(target, {
          characterData: true,
          subtree: true
        })
      } else {
        observe.observe(target, {
          attributes: true
        })
      }
    })
  }

  init()

})()