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

本文介绍了一种在Chrome浏览器中使用控制台保存数据的方法。通过创建并运行一个名为consoleSave.js的脚本,可以轻松地将控制台数据导出为JSON文件。此技巧对于开发者在调试过程中保存和分享数据非常实用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 

} else if (url.includes("/exam/study.do")) { plugMain.showPaidContent("自动下一集启动失败..."); // 移除多余注释符号 } async page_catelog() { plugMain.closeWaitConfrimWin(); plugMain.tipsMsg("等待脚本操作"); if (plugMain.pageData.index.videoFinish != null) return; console.log("%c page_catelog", "background:rgb(0,0,0);color:#fff"); try { await plugMain.waitOf((a) => $(".backHome").length > 0, 1000, 6, true); await plugMain.waitTimeout(500); const found = videoNodes.some(item => { const el = plugMain.getElByText($("a"), item); if (el?.length) { el[0].click(); return true; } return false; }); if (!found) { console.error('未找到任何有效视频节点'); } plugMain.page_catelog_video(); // 弹窗关闭优化 const closePopup = () => { const closeBtn = $('[class*="close"], em.pop_close'); if (closeBtn.length) { closeBtn.trigger('click'); console.log('弹窗关闭成功'); } else { setTimeout(closePopup, 1000); } }; setTimeout(closePopup, 5000); } catch (error) { console.error('目录页操作异常:', error); } }, async page_video() { console.log("%c page_video", "background:rgb(0,0,0);color:#fff"); let timeout = 2; if (plugMain.pageData.video.index != null) { return; } plugMain.pageData.video.index = setInterval(async () => { try { if (plugMain.pageData.waitTime > 0) { plugMain.pageData.waitTime -= timeout; return; } if (!location.href.includes("/com/video.html")) { return; } // let title = `进度:${plugMain.getCurTime().toFixed(0)}/${zfk // .getTotalTime() // .toFixed(0)}`; // $("title").text(title); console.log("%c video run", "background:rgb(255,0,0);color:#fff"); // let $tips = plugMain.getElByText( // ".layui-layer-content p", // "您好,本平台要求实时在线学习,点击按钮,继续学习课程。" // ); // if ($tips != null) { // $tips.parents(".layui-layer").find(".layui-layer-btn0")[0].click(); // } let isFinish = await plugMain.isPlayFinish(); if (isFinish) { plugMain.tipsMsg("视频即将结束,等待下一步操作", { time: 10 * 1000 }); plugMain.pageData.waitTime = 15; plugMain.nextVideo(); return; } let isPlay = await plugMain.videoIsPlay(); if (!isPlay) { if (!isFinish) { plugMain.play(); } } } catch (e) { console.error("视频页面定时器出错", e); } }, timeout * 1000); }, nextVideo() { plugMain.tipsMsg("视频即将结束,等待下一步操作", { time: 10 * 1000 }); setTimeout(() => { // // plugMain.showPaidContent(`自动下一集启动失败...`); }, 10 * 1000); }, async page_video_type2() { console.log("%c page_video_type2", "background:rgb(0,0,0);color:#fff"); GM_addValueChangeListener( "closeWindow", function (name, old_value, new_value, remote) { console.log("closeWindow"); window.close(); } ); 修改错误
最新发布
03-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值