将控制台输出行上的 link 重定向到包装函数的调用者

Redirect link on console output row to the callee of wrapper function

我已经为 window.console 编写了一个 decorator/wrapper,这样我就可以在我的生产环境中禁用杂散的 console.log。 我遇到的是我的包装器现在显示为实际日志命令的来源。这使得通过控制台进行调试有点麻烦,因为单击控制台最右侧的 link 只会导致我自己的输出功能。

以下代码是真实脚本的简化版本,其中我删除了一些功能,例如 enabling/disabling 和 caching/flushing 已隐藏的行。

    //Save reference to original function
    var oConsole = window.console;

    //Create custom console output method
    var wConsole = function (method) {
        return function () {
            if (!window.console[method].enabled) {
                //Apply log command to original console method
                oConsole[method].apply(oConsole, Array.from(arguments)); //This is the row i get linked to
            }
        };
    };

    //Create a new console object for overriding original functions
    var overrides = {
        o: oConsole,
        log: wConsole("log"),
        debug: wConsole("debug"),
        info: wConsole("info"),
        warn: wConsole("warn"),
        error: wConsole("error")
    }

    //Using jQuery i create a new instance and extend my defined overrides onto the original version
    window.console = $.extend({}, window.console, overrides);

    console.log("test 123"); //This is the row i want to link to

当我点击右边的 link 时...
...我link进入这一行。


有没有办法以 link 引用我的包装函数的被调用者的方式创建函数 "transparent"?

该解决方案只需要在 Google Chrome 中工作,因为我在那里执行了大部分开发工作。

Chrome 可以选择 "blackbox" 脚本文件。虽然这似乎主要是为了在调试时忽略框架脚本(单步执行代码时将跳过黑框脚本),但它也会对您的情况有所帮助,因为它不会显示为控制台输出的来源。

  1. 打开 DevTools
  2. 转到设置(F1 或通过主菜单)
  3. 打开 Blackboxing 选项卡
  4. 启用复选框
  5. 添加一个模式,使文件与您的控制台覆盖相匹配
  6. 开心

来源:https://developers.google.com/web/tools/chrome-devtools/javascript/guides/blackbox-chrome-extension-scripts