Rotate custom functions in the browser console

5

It's a bit tiring to have to be writing console.log() all the time. I know that in my code I can set the log function that does just that. But outside of the application environment, I would like to have this always available in the browser, which is just open the console Ctrl + Alt + I and type log('alou') .

For example, the Nice Alert extension of Chrome completely replaces alert() with JavaScript . My intention is not to replace standard functions, but how to do something similar?

    
asked by anonymous 16.10.2014 / 00:32

1 answer

2

What Nice Alert does is basically:

var w = window;
if (!w.alert.is_nice) {
    w.alert = function alert(msg) {/*etc*}
}

They follow a solution like browser extension and as Userscript. An extension has more permissions and runs everywhere. Used as Userscript works on many websites, but not others (like Github), but I do not know exactly why.

To make a custom extension in Chrome or Opera, these would be the files:

manifest.json

{
    "name": "(SOPT) Console functions",
    "manifest_version": 2,
    "version": "0.1",
    "content_scripts": [{
        "matches": ["http://*/*","https://*/*"],
        "js": ["console.js"],
        "run_at": "document_start"
    }]
}

console.js

Warning for using the object arguments that the functions receive.

function main() {
    var w = window;
    if (!w.log) {
        w.log = function log() {
            for( var i=0; i<arguments.length;i++)
            console.log( arguments[i] );
        }
    }
}

if (!document.xmlVersion) {
    var script = document.createElement('script');
    script.appendChild(document.createTextNode('('+ main +')();'));
    document.documentElement.appendChild(script);
}

You can also use a Usercript that runs on Chrome and Firefox , just paste the following header followed by the code console.js above:

// ==UserScript==
// @name       (SOPT) Console functions
// @namespace  userscripts.pt.stackoverflow.com
// @version    0.1
// @match      http*://*/*
// ==/UserScript==

Result :

References:

16.10.2014 / 00:32