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.

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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀