开发者

alias to chrome console.log

开发者 https://www.devze.com 2023-02-13 13:14 出处:网络
I would like to know why the follow code doesn\'t work in the Goog开发者_运维技巧le Chrome: // creates a xss console log

I would like to know why the follow code doesn't work in the Goog开发者_运维技巧le Chrome:

// creates a xss console log

var cl = ( typeof( console ) != 'undefined' ) ? console.log : alert;
cl('teste');

output: Uncaught TypeError: Illegal invocation

thanks.


When you write cl();, you're calling log in the global context.

Chrome's console.log doesn't want to be called on the window object.

Instead, you can write

cl = function() { return console.log.apply(console, arguments); };

This will call log in the context of console.


https://groups.google.com/a/chromium.org/d/msg/chromium-bugs/gGVPJ1T-qA0/F8uSupbO2R8J

Apparently you can also defined log:

 log = console.log.bind(console);

and then the line numbers also work


Unfortunately @SLaks answer isnt applied to IE because it uses window-object as context in console.log-method.

I would be suggest another way that doesnt depend on browser:

!window.console && (console = {});

console.debug = console.debug || $.noop;
console.info = console.info || $.noop;
console.warn = console.warn || $.noop;
console.log = console.log || $.noop;

var src = console, desc = {};
desc.prototype = src;
console = desc;

desc.log = function(message, exception) {
    var msg = message + (exception ? ' (exception: ' + exception + ')' : ''), callstack = exception && exception.stack;
    src.log(msg);
    callstack && (src.log(callstack));
    //logErrorUrl && $.post(logErrorUrl, { message: msg + (callstack || '') }); // Send clientside error message to serverside.
};
0

精彩评论

暂无评论...
验证码 换一张
取 消