「HTMLベースのAdobe AIRでF5を押したときにリロード」の続き

HTMLベースのAdobe AIRでF5を押したときにリロード - DenkiYagiの続きで、ごちゃごちゃやってたらこんなんできた。

/**
 * AutoReload.js
 *
 * @license MIT License
 * @author terurou terurou@gmail.com
 */
(function () {
    const File = runtime.flash.filesystem.File;

    const checker = (function () {
        const files = [];
        var time = 0;

        return {
            add: function (path) {
                if (typeof path !== 'string' || path.length <= 0) return;

                const f = new File(path);
                if (!f.exists || f.isDirectory) return;

                files.push(f);
                time = Math.max(time, f.modificationDate.getTime());
            },

            start: function (interval) {
                setInterval(function () {
                    for (var i = 0, len = files.length; i < len; i++) {
                        if (files[i].modificationDate > time) {
                            htmlLoader.reload();
                            return;
                        }
                    }
                }, interval);
            }
        };
    })();

    document.addEventListener('DOMContentLoaded', function (e) {
        checker.add(location.href);

        const scripts = document.getElementsByTagName('script');
        for (var i = 0, len = scripts.length; i < len; i++) {
            checker.add(scripts[i].src);
        }

        const links = document.getElementsByTagName('link');
        for (var i = 0, len = links.length; i < len; i++) {
            var link = links[i]
            if (link.rel === 'stylesheet') {
                checker.add(link.href);
            }
        }

        checker.start(1000);
    });

    document.addEventListener('keydown', function (e) {
        if (e.keyCode == runtime.flash.ui.Keyboard.F5) {
            htmlLoader.reload();
        }
    });
})();

このJSを一番最初にインクルードしておくと、表示中のHTML 及び そのHTMLでインクルードしているJS, CSSファイルが更新されるたびにリロードします。あとついでにF5でもリロードされるようになります。

ライセンスとかどうでも良かったんだけど、まぁ一応MITにしといた。