简体   繁体   English

将方法添加到Chrome浏览器控制台

[英]Add methods to Chrome Browser Console

I found a method which can create files from browser console in Chrome. 我找到了一种可以从Chrome浏览器控制台中创建文件的方法。 I want to add this function to browsers defaults methods so that, next time I want to use that function, I dont need to paste that function and work with it again. 我想将此功能添加到浏览器的默认方法中,以便下次使用该功能时,不需要粘贴该功能并再次使用它。 I use this multiple times a day. 我每天多次使用。 Like 60 - 100 times. 大概60-100次

Is there any way to add this function to chrome? 有什么办法可以将此功能添加到chrome吗? I want this load each time I open the browser. 我每次打开浏览器时都希望加载此文件。

(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)

As Barmar told, bookmarlet helped me. 正如Barmar所说,bookmarlet帮助了我。

I am adding full code for reference. 我正在添加完整的代码以供参考。

javascript:void((function(d){var e=d.createElement('script');e.innerHTML="
(function(console){

    console.save = function(data, filename){

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

        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)
";d.body.appendChild(e)})(document));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM