-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsw.js
159 lines (158 loc) · 6.94 KB
/
sw.js
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
var APP_PREFIX = '545在线'
var VERSION = '2.2.2.20240407'
var VERSION_AZUSA_PATCH_USE = '2.2.1.20231213'
var AZUSA_PATCH_SKIP_LIST = [
'./cursor/normal.png',
'./icon.webp',
'./icon_mask.png',
'./icon_mono.png',
'./icon_badminton.webp',
'./icon_badminton_mask.webp',
'./icon_badminton_small.webp',
'./icon_badminton_mask_small.webp',
'./public.css',
'./manifest.json'
]
var CACHE_NAME = APP_PREFIX + VERSION;
var AZUSA_CACHE = APP_PREFIX + VERSION_AZUSA_PATCH_USE;
const getCacheName = url => {
if (url.indexOf("bcebos.com") > 0 || url.indexOf("cdn-cf.545.qinlili.bid") > 0) {
return "MusicCache";
};
if (url.indexOf("bytecdntp.com") > 0 || url.indexOf("googleapis.com") > 0 || url.indexOf("gstatic.com") > 0 || url.indexOf("baomitu.com") > 0 || url.indexOf("/lib/") > 0 || url.indexOf("/img/") > 0 || url.indexOf("/cursor/") > 0) {
return "StaticCache";
};
if (url.indexOf("hdslb.com") > 0 || url.indexOf("zhimg.com") > 0) {
return "ImageCache";
};
if (url.indexOf("/plugins/") > 0 && url.split("/").length >= 6) {
return "Plugins"
};
return CACHE_NAME;
};
self.addEventListener('fetch', event => {
if (event.request.url.indexOf("getVersionWorker") > 0) {
event.respondWith(new Response(VERSION));
return;
}
if (event.request.method == "GET" && (event.request.url.indexOf("http") == 0) && (event.request.url.indexOf("ForceNoCache") == -1) && (event.request.url.indexOf("webapi.ctfile.com") == -1)) {
event.respondWith(
caches.open(getCacheName(event.request.url)).then(async cache => {
return cache.match(event.request, { 'ignoreSearch': event.request.url.startsWith(location.origin) }).then(response => {
return response || fetch(event.request).then(response => {
if (response.status < 400) {
let tempUrl = new URL(event.request.url);
cache.put(event.request.url.startsWith(location.origin) ? (new Request(tempUrl.origin + tempUrl.pathname, {
method: event.request.method,
headers: event.request.headers,
mode: 'same-origin',
credentials: event.request.credentials,
redirect: event.request.redirect
})) : event.request, response.clone());
console.log('file cached : ' + event.request.url)
}
//修复不支持自动补足.html的平台无法访问的问题
if (response.status == 404) {
if (event.request.url.substring(event.request.url.lastIndexOf("/")).indexOf(".") == -1 && !event.request.url.endsWith("/")) {
event.request.url += ".html";
console.log("processing url:" + event.request.url);
alterRequest = new Request(event.request.url + ".html", {
method: event.request.method,
headers: event.request.headers,
mode: 'same-origin',
credentials: event.request.credentials,
redirect: 'manual'
});
return fetch(alterRequest).then(response => {
if (response.status < 400) {
cache.put(event.request, response.clone());
console.log('file cached : ' + event.request.url)
}
return response;
})
}
} else {
return response;
}
}).catch(error => {
console.log("failed to fetch :" + event.request.url)
console.log(error);
if (event.request.url.substring(event.request.url.lastIndexOf("/")).indexOf(".") == -1 && !event.request.url.endsWith("/")) {
return cache.match("/error").then(response => {
return response || new Response("加载出错了!");
})
} else {
return false;
}
});
});
})
);
} else {
event.respondWith(fetch(event.request))
}
});
self.addEventListener('install', e => {
self.skipWaiting();
const install = async () => {
const cache = await caches.open(CACHE_NAME)
console.log('installing cache : ' + CACHE_NAME)
if ((await caches.has(AZUSA_CACHE))) {
console.log("Found Old Cache! Azusa Patch Working...");
caches.open(AZUSA_CACHE).then(oldCache => {
AZUSA_PATCH_SKIP_LIST.forEach(async url => {
let tempResponse = await oldCache.match(url);
if (tempResponse) {
console.log("Azusa Success Transfer Old Cache : " + url)
cache.put(url, tempResponse);
}
})
})
};
return fetch("/error").then(response => {
cache.put("/error", new Response(response.body, { status: 200, header: { "content-type": "text/html; charset=utf-8" } }));
return true;
})
}
e.waitUntil(install());
});
self.addEventListener('activate', e => {
e.waitUntil(
caches.keys().then((keyList) => {
var cacheWhitelist = keyList.filter(key => {
return key.indexOf(APP_PREFIX)
})
cacheWhitelist.push(CACHE_NAME);
cacheWhitelist.push("MusicCache");
cacheWhitelist.push("StaticCache");
cacheWhitelist.push("ImageCache");
cacheWhitelist.push("Plugins");
return Promise.all(keyList.map((key, i) => {
if (cacheWhitelist.indexOf(key) === -1) {
console.log('deleting cache : ' + keyList[i])
return caches.delete(keyList[i])
}
}))
})
)
});
self.addEventListener('notificationclick', function (event) {
event.notification.close();
console.log(event)
switch (event.notification.tag) {
case "直播推送":
{
clients.openWindow("https://live.bilibili.com/545");
break;
};
case "直播推送客户端":
{
clients.openWindow("bilibili://live/545");
break;
};
default:
{
break;
}
}
}, false);