【tips】将控制台console.log的内容保存为文件

1 篇文章 0 订阅

Need to export some data from the browser console?

Open Sources -> Snippets in the console, right click to make a new file. Call it consoleSave.js

打开调试工具,找到Sources -> Snippet,复制代码,保存为 consoleSave.js

Picture

Paste the JavaScript code below into the snippet (or get the latest version from http://bgrins.github.io/devtools-snippets/#console-save)

Right click on consoleSave.js and run the snippet.

运行脚本

Voila, now you can call console.save() to
download objects from the console!

Picture

 js脚本

(function(console){

    console.save = function(data, filename){

        if(!data) {
            console.error('Console.save: No data')
            return;
        }

        if(!filename) filename = 'console.json'

        if(typeof data === "object"){
            data = JSON.stringify(data, undefined, 4)
        }

        var blob = new Blob([data], {type: 'text/json'}),
            e    = document.createEvent('MouseEvents'),
            a    = document.createElement('a')

        a.download = filename
        a.href = window.URL.createObjectURL(blob)
        a.dataset.downloadurl =  ['text/json', a.download, a.href].join(':')
        e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
        a.dispatchEvent(e)
    }
})(console)

 把要保存的内容储存为global变量

然后执行console.save({" ":  }) 

保存为json文件 

参考:https://coderwall.com/p/prhwzg/add-console-save-to-chrome 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值