LoginSignup
2
2

More than 5 years have passed since last update.

古いディレクトリを漁ったら、アイデアスケッチみたいなファイルが居たので供養しておく

Posted at

多分JSでエラーが発生した時に、サーバーへ通知してJSのエラーログを保存したかったんだと思う。

<script type="text/javascript" src="ErrorHandler.js"></script>
<script>
    new __ErrorHandler().listen('gif.gif', 'img');
    null.createElement('hoge');
</script>
function __ErrorHandler () {
    self = this;
    this.listen = function(sendTo, sendType) {
        window.onerror = function(message, url, line) {
            // 再起防止
            window.onerror = null;
            if (console.log) {
                console.log(message);
                console.log(url);
                console.log(line);
            }
            if (sendTo) {
                if (!sendType) sendType = 'img';
                if (sendType == 'img') self.sendAsImage({
                    'sendTo' : sendTo,
                    'message' : message,
                    'url' : url,
                    'line' : line
                });
            }
        }
    }
    this.sendAsImage = function (obj) {
        var img = document.createElement('img');
        img.src = self.buildSendTo(obj);
        img.onabort = function () {document.getElementsByTagName('body')[0].removeChild(img);}
        img.onload  = function () {img.onabort();}
        document.getElementsByTagName('body')[0].appendChild(img);
    }
    this.buildSendTo = function (obj) {
        var sendTo = '';
        sendTo = obj.sendTo;
        if (!sendTo.match(/\?/)) sendTo += '?';
        else                     sendTo += '&' ;
        sendTo += 'url='     + encodeURIComponent(obj.url) + '&';
        sendTo += 'line='    + encodeURIComponent(obj.line) + '&';
        sendTo += 'message=' + encodeURIComponent(obj.message);
        return sendTo;
    }
    return this;
}
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2