FastJump.js

动荡の初二少年 2023-03-17 08:29:45 2023-05-27 01:57:32

110行写了个小玩意,可以像sw那样离线缓存,和Pjax类似,不过用了自主缓存,fetch做请求,算了上代码吧:

self.CACHE_NAME = 'FastJumpCache'; // 默认的存储名称
self.db = {
    read: (key) => {
        return new Promise((resolve, reject) => {
            caches.match(new Request(`https://LOCALCACHE/${encodeURIComponent(key)}`)).then(function (res) {
                res.text().then(text => resolve(text));
            }).catch(() => {
                resolve(null);
            });
        })
    }, write: (key, value) => {
        return new Promise((resolve, reject) => {
            caches.open(CACHE_NAME).then(function (cache) {
                cache.put(new Request(`https://LOCALCACHE/${encodeURIComponent(key)}`), new Response(value));
                resolve();
            }).catch(() => {
                reject();
            });
        });
    }
}

document.addEventListener('DOMContentLoaded', function () {
    self.db.read(window.location.href).then(function (d) {
        if (!d) {
            fetch(window.location.href).then(function (d) {
                if (d.headers.get('content-type') === 'text/html') {
                    d.text().then(function (b) {
                        self.db.write(window.location.href, b)
                    })
                }
            })
        }
    });
});

document.addEventListener('DOMContentLoaded', function () {
    setInterval(function () {
        var doms = document.querySelectorAll('a[href]');
        Array.prototype.forEach.call(doms, function (dom) {
            if (dom.href && dom.href.indexOf(window.location.origin) === 0) {
                dom.onclick = function () {
                    history.pushState({}, '', dom.href);
                    self.db.read(dom.href).then(function (d) {
                        if (d) {
                            console.clear();
                            document.open();
                            try {
                                document.write(d);
                                let Event_timerun = window.setInterval(function () {
                                    if (document.body&&document.body.innerHTML) {
                                        let Event = new CustomEvent('onload')
                                        document.dispatchEvent(Event);
                                        window.dispatchEvent(Event);
                                        Event = new CustomEvent('DOMContentLoaded')
                                        document.dispatchEvent(Event);
                                        window.dispatchEvent(Event);
                                        window.clearInterval(Event_timerun)
                                    }
                                },500)
                                fetch(dom.href).then(function (d) {
                                    if (d.headers.get('content-type') === 'text/html') {
                                        d.text().then(function (b) {
                                            self.db.write(window.location.href, b)
                                        })
                                    }
                                })
                            } catch (e) {

                            }
                        } else {
                            fetch(dom.href).then(function (d) {
                                if (d.headers.get('content-type') === 'text/html') {
                                    d.text().then(function (b) {
                                        console.clear();
                                        document.open();
                                        try {
                                            document.write(b)
                                            let Event_timerun = window.setInterval(function () {
                                                if (document.body&&document.body.innerHTML) {
                                                    let Event = new CustomEvent('onload')
                                                    document.dispatchEvent(Event);
                                                    window.dispatchEvent(Event);
                                                    Event = new CustomEvent('DOMContentLoaded')
                                                    document.dispatchEvent(Event);
                                                    window.dispatchEvent(Event);
                                                    window.clearInterval(Event_timerun)
                                                }
                                            },500)
                                        } catch (e) {
                                        }
                                        self.db.write(dom.href, b)
                                    }).catch(function (e) {
                                        window.location.href = dom.href
                                    })
                                }
                            }).catch(function (e) {
                                window.location.href = dom.href
                            })
                        }
                    }).catch(function (e) {
                        window.location.href = dom.href
                    })
                    return false;
                }
            }
        });
    }, 500)
});

注意不能与Pjax一起使用!!
鄙人水平有限,请斧正
有不少Bug,仅供测试,不可以在线上环境使用

本站重新进行调试,开始使用自己的主题,过程中,可能会对使用略有影响

停更公告