Greasy Fork

Speedport JSON Parser

Parse and display DSL information from JSON data

目前为 2023-05-27 提交的版本。查看 最新版本

// ==UserScript==
// @name         Speedport JSON Parser
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Parse and display DSL information from JSON data
// @match        http://192.168.0.1/data/Status.json
// @grant        GM_addStyle
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // Add custom CSS styles to hide the JSON input and output
    GM_addStyle(`
        pre:first-child {
            display: none !important;
        }

        body > pre {
            visibility: hidden !important;
        }

        #dsl-info-container {
            display: block !important;
        }

        #dsl-info-container h3 {
            font-weight: bold;
        }

        #dsl-info-container p {
            margin: 5px 0;
        }

        .dsl-info-title {
            font-weight: bold;
        }
    `);

    // Clear the screen
    document.body.innerHTML = '';

    // Fetch the JSON data
    fetch("http://192.168.0.1/data/Status.json")
        .then(response => response.json())
        .then(data => {
            // Find the DSL variables
            const dslMaxDownstream = data.find(item => item.varid === "dsl_max_downstream");
            const dslMaxUpstream = data.find(item => item.varid === "dsl_max_upstream");
            const firmwareVersion = data.find(item => item.varid === "firmware_version");
            const uptime = data.find(item => item.varid === "uptime");
            const dslSyncTime = data.find(item => item.varid === "dsl_sync_time");
            const dslDownstream = data.find(item => item.varid === "dsl_downstream");
            const dslUpstream = data.find(item => item.varid === "dsl_upstream");
            const snr = data.find(item => item.varid === "dsl_snr");
            const attn = data.find(item => item.varid === "dsl_atnu");

            // Create HTML elements to display the DSL information
            const container = document.createElement("div");
            container.id = "dsl-info-container";
            container.style.margin = "10px";
            container.style.padding = "10px";
            container.style.border = "1px solid #ccc";
            container.style.backgroundColor = "#f5f5f5";

            const title = document.createElement("h3");
            title.textContent = "Speedport VDSL Information";

            const firmware = document.createElement("p");
            firmware.innerHTML = "<span class='dsl-info-title'>Firmware Version:</span> " + (firmwareVersion ? firmwareVersion.varvalue : "N/A");

            const uptimeInfo = document.createElement("p");
            uptimeInfo.innerHTML = "<span class='dsl-info-title'>Uptime:</span> " + (uptime ? uptime.varvalue : "N/A");

            const syncTime = document.createElement("p");
            syncTime.innerHTML = "<span class='dsl-info-title'>DSL Sync Time:</span> " + (dslSyncTime ? dslSyncTime.varvalue : "N/A");

            const downstream = document.createElement("p");
            downstream.innerHTML = "<span class='dsl-info-title'>DSL Downstream:</span> " + (dslDownstream ? dslDownstream.varvalue + " Mbps" : "N/A");

            const upstream = document.createElement("p");
            upstream.innerHTML = "<span class='dsl-info-title'>DSL Upstream:</span> " + (dslUpstream ? dslUpstream.varvalue + " Mbps" : "N/A");

            const maxDownstream = document.createElement("p");
            maxDownstream.innerHTML = "<span class='dsl-info-title'>DSL Max Downstream:</span> " + (dslMaxDownstream ? dslMaxDownstream.varvalue + " Mbps" : "N/A");

            const maxUpstream = document.createElement("p");
            maxUpstream.innerHTML = "<span class='dsl-info-title'>DSL Max Upstream:</span> " + (dslMaxUpstream ? dslMaxUpstream.varvalue + " Mbps" : "N/A");

            const snrInfo = document.createElement("p");
            snrInfo.innerHTML = "<span class='dsl-info-title'>Signal-to-Noise Ratio:</span> " + (snr ? snr.varvalue : "N/A");

            const attnInfo = document.createElement("p");
            attnInfo.innerHTML = "<span class='dsl-info-title'>Attenuation:</span> " + (attn ? attn.varvalue : "N/A");

            // Append the HTML elements to the page
            container.appendChild(title);
            container.appendChild(firmware);
            container.appendChild(uptimeInfo);
            container.appendChild(syncTime);
            container.appendChild(downstream);
            container.appendChild(upstream);
            container.appendChild(maxDownstream);
            container.appendChild(maxUpstream);
            container.appendChild(snrInfo);
            container.appendChild(attnInfo);
            document.body.appendChild(container);
        })
        .catch(error => {
            console.error("An error occurred while fetching or parsing the JSON data:", error);
        });
})();