Data Point (DP) list extractor for Tuya IOT platform

Script that helps to get DPs for your tuya devices

Mint 2024.02.17.. Lásd a legutóbbi verzió

// ==UserScript==
// @name         Data Point (DP) list extractor for Tuya IOT platform
// @namespace    https://etcho.github.io/
// @version      1.0
// @description  Script that helps to get DPs for your tuya devices
// @author       Everton Leite
// @match        *.iot.tuya.com/cloud/device/detail/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=tuya.com
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    let dp_list = document.createElement('ul');

    function waitForElm(selector) {
        return new Promise(resolve => {
            if (document.querySelector(selector)) return resolve(document.querySelector(selector));

            const observer = new MutationObserver(mutations => {
                if (document.querySelector(selector)) {
                    resolve(document.querySelector(selector));
                    observer.disconnect();
                }
            });

            observer.observe(document.body, {childList: true, subtree: true});
        });
    }

    function insertIdleItem() {
        let idle_item = document.createElement('li');
        idle_item.style.padding = '1px 4px';
        idle_item.innerHTML = 'Mouse over the options of <b>Select DP ID</b> to extract the DP list';
        dp_list.appendChild(idle_item);
    }

    function createListElement() {
        waitForElm('#rc-tabs-0-panel-deviceLogs').then((elm) => {
            dp_list.id = 'extracted_dp_list';
            dp_list.style.position = 'absolute';
            dp_list.style.maxHeight = '300px';
            dp_list.style.overflow = 'auto';
            dp_list.style.width = '200px';
            dp_list.style.top = '60px';
            dp_list.style.right = '15px';
            dp_list.style.background = '#FF4800';
            dp_list.style.boxShadow = '2px 2px 4px rgba(0, 0, 0, 0.1)';
            dp_list.style.color = '#FFF';
            dp_list.style.border = '2px solid #cc3a00';
            dp_list.style.fontSize = '12px';
            dp_list.style.zIndex = 1;

            document.getElementById('rc-tabs-0-panel-deviceLogs').appendChild(dp_list);
            insertIdleItem();
        });
    }

    function createListener() {
        let codes = {};
        waitForElm('#code_list').then((elm) => {
            var observer = new MutationObserver(mutations => {
                let code_items = document.getElementById('code_list').getElementsByTagName('div');
                for (const code_item of code_items) {
                    codes[code_item.textContent] = code_item.getAttribute('aria-label');
                }

                dp_list.replaceChildren();
                let clear_list = document.createElement('li');
                clear_list.innerHTML = '<a href="#" style="display: block; text-align: right; font-style: italic; color: #FFF; text-decoration: underline; background: #FF6820; padding-right: 4px;">Clear list</a>';
                clear_list.addEventListener('click', (e) => {
                    e.preventDefault();
                    codes = {};
                    dp_list.replaceChildren();
                    insertIdleItem();
                });
                dp_list.appendChild(clear_list);
                for (const dp in codes) {
                    let item = document.createElement('li');
                    item.innerHTML = '<b>' + dp + '</b>&nbsp; &#8594; &nbsp;' + codes[dp];
                    item.style.padding = '1px 4px';
                    if (Object.keys(codes).pop() != dp) {
                        item.style.borderBottom = '1px solid rgb(229, 65, 0)';
                    }
                    dp_list.appendChild(item);
                }
            });
            observer.observe(document.querySelector('#code_list'), {childList: true});
        });
    }

    createListElement();
    createListener();
})();