【FastJump.js】简单的快速跳转技术

【FastJump.js】简单的快速跳转技术

Fgaoxing 2023-03-17 08:29:45 2024-04-03 13:23:54

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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,仅供测试,不可以在线上环境使用

总字数7.2k
文章数29
分类数9
标签数41

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