Как использовать поведение по умолчанию для тела форматера devtoolsFormatters в Chrome Инструменты разработчика - PullRequest
0 голосов
/ 25 января 2020

Я пробовал много вещей, в качестве основы:

(() => {
    function getLevelIndentation(level){
        return level * 20 + "px";
    }
    var multiKeyMapFormatter = {
        header: function(x) {
            if (!(x instanceof MultiKeyMap)){
                return null;
            }

            let textArray = [];
            x.forEach((r, u, t, mkm) => textArray.push("[" + t + ", " + u + "] => " + (r instanceof Object ? r.constructor.name : r)));
            const header = "MultiKeyMap - " + textArray.join("|").substr(0, 50);

            return ["div", {"style":'color: green'}, header]
        },
        hasBody: function(){
            return true;
        },
        body: function(obj, config){
            return undefined;   
        },
    };


    window.devtoolsFormatters = [multiKeyMapFormatter];

    console.log("defined window.devtoolsFormatters");

})();

например,

  • , заменяя значение null на undefined при возврате из функции body
  • опуская функцию тела
  • переписывая поведение по умолчанию (см. ниже)
  • возвращая obj в различных перестановках
  • Установка hasBody для возврата false
    // attempt at rewriting the default behavior
    // ref: https://www.mattzeunert.com/2016/02/19/custom-chrome-devtools-object-formatters.html
    body: function(obj, config){
        var level = config !== undefined ? config.level : 0;

        var elements = Object.keys(obj).map(function(key){
            var child;
            var childObj = obj[key];
            if (typeof childObj === "object"){
                child = ["object", {
                    object: childObj,
                    config: {
                        key: key,
                        level: level + 1
                    }
                }];
            } else {
                child = key + ": " + (childObj && childObj.toString ? childObj.toString() : childObj);

            }
            return ["div", {style: "margin-left: " + getLevelIndentation(level)}, child];
        })

        return ["div", {}].concat(elements);
    },

Есть ли способ указать Chrome использовать поведение тела по умолчанию?

...