-
Notifications
You must be signed in to change notification settings - Fork 155
/
path.js
37 lines (34 loc) · 937 Bytes
/
path.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
export const ROOT = getRootPath()
export const HOME = ROOT + 'index.html'
export const CONF = ROOT + 'conf.js'
export const ICON = ROOT + 'favicon.ico'
export const HELPER = ROOT + '__sys__/helper.js'
export const ASSETS = ROOT + '__sys__/assets/'
export const PREFIX = ROOT + '-----'
function getRootPath() {
//
// 如果运行在代理页面,当前路径:
// https://example.com/path/to/-----url
// 如果运行在 SW,当前路径:
// https://example.com/path/to/sw.js
// 如果运行在 Worker,当前路径:
// __PATH__
// 返回:
// https://example.com/path/to/
//
/** @type {string} */
const envPath = self['__PATH__']
if (envPath) {
return envPath
}
let url = location.href
const pos = url.indexOf('/-----http')
if (pos === -1) {
// sw
url = url.replace(/[^/]+$/, '')
} else {
// page
url = url.substr(0, pos)
}
return url.replace(/\/*$/, '/')
}