duanjianxiu9400 2014-10-25 10:00
浏览 81
已采纳

通过AJAX / PHP进行Javascript全局错误处理:将日志限制为我自己的脚本

I have a script that detects Javascript errors on my website and sends them to my backend for reporting:

<script>
window.onerror = function(msg, url, line, col, error){

    msg   = msg || '';
    url   = url || '';
    line  = parseInt(line || 0);

    // Note that col & error are new to the HTML 5 spec and may not be supported in every browser.
    col   = parseInt(col || 0);
    error = error || '';

    try
    {
        // Ajax Request for IE 5.5+, Firefox, Opera, Chrome, Safari XHR object
        var x = new (this.XMLHttpRequest || ActiveXObject)('MSXML2.XMLHTTP.3.0');
        x.open('POST', '/log.php', 1);
        x.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
        x.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        x.send('msg='+encodeURIComponent(msg)+'&url='+encodeURIComponent(url)+'&line='+line+'&col='+col+'&error='+encodeURIComponent(error));

        x.onreadystatechange = function()
        {
            if( (x.readyState > 3) && (x.status > 0 && x.status < 400))
                window.console && console.log(x.responseText);
        };
    }
    catch(e)
    {
        window.console && console.log(e);
    }

    return true;
};
</script>

I have a server-side php script that will listen for Ajax request and writes a log file

<?php

if( $handle = fopen('log.txt', 'a') ) {

    $log = date('d/m/Y H:i:s').PHP_EOL;

    if( isset($_REQUEST['msg']) )
         $log .= 'msg:'.$_REQUEST['msg'].PHP_EOL;

    if( isset($_REQUEST['url']) )
         $log .= 'url:'.$_REQUEST['url'].PHP_EOL;

    if( isset($_REQUEST['line']) )
         $log .= 'line:'.$_REQUEST['line'].PHP_EOL;

    if( isset($_REQUEST['col']) )
         $log .= 'col:'.$_REQUEST['col'].PHP_EOL;

    if( isset($_REQUEST['error']) )
         $log .= 'error:'.$_REQUEST['error'].PHP_EOL;

    $log .= '---------------------------------------------'.PHP_EOL;

    fwrite($handle, $log);

    fclose($handle);

    echo 1;

} else {
    echo 0;
}

if in a page rise a javascript exception, eg:

<script> call_undefined_function(); </script>

write in the log file...

25/10/2014 11:31:08
msg:ReferenceError: call_undefined_function is not defined
url:http://www.test.it/
line:46
col:1
error:ReferenceError: call_undefined_function is not defined
---------------------------------------------

Everything works quite well!

But, i find a lot of logs raised by plug-ins, toolbars, worms or browser extensions used by the users...

eg

24/10/2014 10:20:32
msg:Unsafe JavaScript attempt to access frame
url: http://ads.XXXXXX.net/?XXXXXX
line:0
col:0
error:Unsafe JavaScript attempt to access frame
---------------------------------------------

Obviously this script is not my site and doing some research I found to be a worm of internet explorer

my questions is: How can I limit the logs to my own script?

  • 写回答

1条回答 默认 最新

  • douduocuima61392 2014-10-27 16:16
    关注

    You could always check against the URL:

    // If the script is not being loaded from my domain
    if(url.indexOf('http://mydomain') == -1)
    {
        // Allow the error to propagate normally
        return false;
    }
    

    This may not be effective however if some third party is injecting inline scripts as opposed to externally loaded ones.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效