简体   繁体   中英

Add methods to Chrome Browser Console

I found a method which can create files from browser console in 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.

Is there any way to add this function to 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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