Resolve MAC Addresses for TD-W8970 v1 web pages

Resolve MAC Addresses based on settings in Wireless MAC Filtering page. Tested on TD-W8970 v1 Firmware Version:0.6.0 2.12 v000c.0 Build 140613 Rel.31066n

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Resolve MAC Addresses for TD-W8970 v1 web pages
// @namespace    https://github.com/Neurone/hus
// @version      1.1
// @description  Resolve MAC Addresses based on settings in Wireless MAC Filtering page. Tested on TD-W8970 v1 Firmware Version:0.6.0 2.12 v000c.0 Build 140613 Rel.31066n
// @author       Giuseppe Bertone <[email protected]> (https://github.com/Neurone/hus)
// @copyright    2020+, Giuseppe Bertone (https://github.com/Neurone/hus)
// @license      MIT; https://github.com/Neurone/hus/raw/master/LICENSE
// @source       https://github.com/Neurone/hus
// @supportURL   https://github.com/Neurone/hus/issues
// @match        http://192.168.1.1
// @require      https://code.jquery.com/jquery-3.4.1.min.js
// @grant        none
// @run-at       document-end
// ==/UserScript==

(function () {
    'use strict';

    const REFRESH_RATE = 400; //milliseconds
    let macNames = {};

    function isEmpty(obj) {
        for (var key in obj) {
            if (obj.hasOwnProperty(key)) return false;
        }
        return true;
    }

    function isMacAddress(string) {
        return (17 === string.length);
    }

    function formatCellWithNickname(cell) {
        let macAddress = cell.text();
        let deviceNickname = macNames[macAddress] ? macNames[macAddress] : "";
        if(isMacAddress(macAddress)) cell.html(macAddress + "<br/>" + deviceNickname);
    }

    var checkPage = function () {
        let pageName = jQuery("#et").text();
        if ('Wireless Stations Status' === pageName) {
            if (isEmpty(macNames)) {
                jQuery("#t_info").html("This page displays the basic information of all stations in this wireless network. <span style='color:red;font-weight:bold'>To init the MAC address resolution please visit the 'Wireless MAC filtering' page at least once.</span>");
            } else {
                // Resolve MAC names
                jQuery("#staTbl tr").each(function () {
                    let cell = jQuery(this).find("td:eq(1)");
                    formatCellWithNickname(cell);
                });
            }
        } else if ('DHCP Clients List' === pageName) {
            if (isEmpty(macNames)) {
                jQuery("#t_info").html("This page displays the information of DHCP clients. <span style='color:red;font-weight:bold'>To init the MAC address resolution please visit the 'Wireless MAC filtering' page at least once.</span>");
            } else {
                // Resolve MAC names
                jQuery("#hostTbl tr").each(function () {
                    let cell = jQuery(this).find("td:eq(2)");
                    formatCellWithNickname(cell);
                });
            }
        } else if ('ARP List' === pageName) {
            if (isEmpty(macNames)) {
                let firstChild = jQuery(".con2 p:first");
                if(firstChild.hasClass("br")) firstChild.before('<p class="L1 T" id="t_info"><span style="color:red;font-weight:bold">To init the MAC address resolution please visit the \'Wireless MAC filtering\' page at least once.</span></p>');
            } else {
                // Resolve MAC names
                jQuery("#arptbl tr").each(function () {
                    let cell = jQuery(this).find("td:eq(1)");
                    formatCellWithNickname(cell);
                });
            }
        } else if ('Wireless MAC Filtering settings' === pageName) {
            // Update MAC name list
            macNames = {};
            jQuery("#macTbl tr").each(function () {
                let macAddress = jQuery(this).find("td:eq(1)").text();
                let macName = jQuery(this).find("td:eq(3)").text();
                macNames[macAddress] = macName;
            });
            // Save MAC names in local storage
            if (typeof (Storage) !== "undefined")
                localStorage.macNames = JSON.stringify(macNames);
        }
    };

    $.noConflict();
    jQuery.when(jQuery.ready).then(function () {
        // Retrieve saved MAC list
        if (typeof (Storage) !== "undefined" && localStorage.macNames)
            macNames = JSON.parse(localStorage.macNames);
        setInterval(checkPage, REFRESH_RATE);
    });

})();