// ==UserScript==
// @name        image size checker
// @namespace   http://inasphere.net/
// @description 画像本来のサイズとHTML/CSS/JSで指定されているサイズとが異なる画像のスタイルを変更する
// @include     *
// @author      inamenai
// @license     MIT License
// @version     0.2
// ==/UserScript==

(function() {

    // 対象の画像に適用するスタイル
    var BORDER_STYLE = '3px solid #ff0000';
    // 1px*1pxの画像を無視するかどうか
    var IGNORE_ONE_PIXEL_IMG = true;
    // サイズは異なるが縦横比が保持されている画像を無視するかどうか
    var IGNORE_SAME_ASPECT_RATIO_IMG = true;

    var imageList = document.getElementsByTagName('img');
    for (var i = 0; i < imageList.length; i++) {
        if (imageList[i].naturalWidth && imageList[i].naturalWidth != 0) {
            checkSize(imageList[i]);

        } else {
            imageList[i].addEventListener('load', function(event) {
                checkSize(event.currentTarget);
            }, false);
        }
    }

    function checkSize(img) {
        if (!(img.width == img.naturalWidth) || !(img.height == img.naturalHeight)) {

            if (IGNORE_ONE_PIXEL_IMG) {
                if (img.naturalWidth == 1 && img.naturalHeight == 1) {
                    return;
                }
            }
            if (IGNORE_SAME_ASPECT_RATIO_IMG) {
                if (img.height != 0 && img.naturalHeight != 0 &&
                    img.width / img.height == img.naturalWidth / img.naturalHeight) {
                    return;
                }
            }

            img.style.border = BORDER_STYLE;
        }
    }
})();
