DH3 VarViewer

Easy way to view var_ values in DH3

目前为 2020-07-11 提交的版本。查看 最新版本

// ==UserScript==
// @name         DH3 VarViewer
// @namespace    com.anwinity.dh3
// @version      1.0.0
// @description  Easy way to view var_ values in DH3
// @author       Anwinity
// @match        dh3.diamondhunt.co
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const VarViewer = {
        init: function() {
            // overwrite navigate to handle new tab
            const oldNavigate = window.navigate;
            window.navigate = function(a,b,c,d,e,f,g,h,i,j,k,l,m) {
                oldNavigate(a,b,c,d,e,f,g,h,i,j,k,l,m);
                if(a=="right-vars") {
                    VarViewer.refresh();
                }
                else {
                    $("#navigation-right-vars").hide();
                }
            };

            // add some styling (cause we fancy)
            const styles = document.createElement("style");
            styles.textContent = `
              table#vars-table {
                border-collapse: collapse;
              }
              table#vars-table tr, table#vars-table th, table#vars-table td {
                border: 1px solid grey;
              }
              table#vars-table th, table#vars-table td {
                padding: 0.25em;
              }
            `;
            $("head").append(styles);

            // add the button
            $("#navigation-area-buttons").append(`
            <div onclick="navigate('right-vars')" id="navigation-right-vars-button" class="navigate-button" style="color: white;">
              <img src="images/fireIcon.png" class="img-50" />
              <br />
              <div style="font-size: 10pt; text-align: center;">Vars</div>
            </div>`);

            // add the content
            $("#right-panel").append(`
            <div id="navigation-right-vars" style="display: none; padding: 1em;">
              <div>
                <button type="button" onclick="refreshVarsTab()">Refresh</button><br />
                <table id="vars-table">
                  <thead>
                    <tr>
                      <th style="text-align: left">Var Name</th>
                      <th style="text-align: left">Var Value</th>
                    </tr>
                  </thead>
                  <tbody></tbody>
                </table>
              </div>
            </div>
            `);
        },
        refresh: function() {
            const tbody = $("#navigation-right-vars tbody");
            tbody.empty();
            tbody.append('<tr><td id="vars-table-one-sec" colspan="2">One Sec...</td></tr>');
            setTimeout(() => {
                Object.keys(window)
                    .filter(v=>v.startsWith("var_"))
                    .sort()
                    .forEach(function(v) {
                    tbody.append(`
                        <tr>
                          <td>${v}</td>
                          <td>${window[v]}</td>
                        </tr>
                    `);
                });
                $("#vars-table-one-sec").remove();
            }, 1);

        }
    };

    window.refreshVarsTab = VarViewer.refresh;
    VarViewer.init();
})();