如何挂接到控制台错误和浏览器发布的警告。 由于内部浏览器错误。我的意思是我如何挂钩到安全违规日志等错误
******* ...... it violates the following Content Security Policy directive: "frame-src 'self' *.wholefoodsmarket.com https://*.google.com *.acquia-sites.com".
所以我想在一个变量中获取控制台中存在的所有错误消息。
我试图覆盖默认的控制台功能,但仍然无法挂钩到这些类型的日志中。
我现在的代码
var consoleLogFn = console.log;
var consoleWarnFn = console.warn;
var consoleErrorFn = console.error;
window.jsErrors = window.jsErrors || [];
console.log = function(){
consoleLogFn.apply(console, arguments);
var args = Array.prototype.slice.call(arguments);
for(var i=0;i<args.length;i++){
pushLog('log', args[i]);
}
}
console.warn = function(){
consoleWarnFn.apply(console, arguments);
var args = Array.prototype.slice.call(arguments);
for(var i=0;i<args.length;i++){
pushLog('warn', args[i]);
}
}
console.error = function(){
consoleErrorFn.apply(console, arguments);
var args = Array.prototype.slice.call(arguments);
for(var i=0;i<args.length;i++){
pushLog('error', args[i]);
}
}
console.debug = function(){
consoleErrorFn.apply(console, arguments);
var args = Array.prototype.slice.call(arguments);
for(var i=0;i<args.length;i++){
pushLog('debug', args[i]);
}
}
var pushLog = function(type, msg){
window.jsErrors.push({
'type': type,
'message': msg,
});
}
您可以通过添加列表器函数来侦听错误。
// You can also do (note the diferent arguments): window.onerror = function (message, source, lineno, colno, error)
window.addEventListener("error", function (event) {
// do something with the error
})
有关更详细的说明,请参阅:mdn。