Skip to content

Commit 4974cb0

Browse files
committed
更新[BoxJs]: 页面完全独立化, 修复应用会话、清空会话属性无效问题
1 parent 5d434c1 commit 4974cb0

2 files changed

Lines changed: 128 additions & 37 deletions

File tree

‎box/chavy.boxjs.html‎

Lines changed: 79 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
<v-menu bottom left v-if="isMainView">
6969
<template #activator="{ on }">
7070
<v-btn icon v-on="on">
71-
<v-avatar size="26"><img :src="env.icon" /></v-avatar>
71+
<v-avatar size="26"><img :src="env.icons[iconEnvThemeIdx]" /></v-avatar>
7272
</v-btn>
7373
</template>
7474
<v-list>
@@ -189,7 +189,6 @@
189189
label="透明图标"
190190
v-model="box.usercfgs.isTransparentIcons"
191191
@change="saveUserCfgs"
192-
:disabled="!isDarkMode"
193192
:hide-details="isDarkMode"
194193
:persistent-hint="true"
195194
hint="明亮主题下强制使用彩色图标"
@@ -204,8 +203,39 @@
204203
<v-switch
205204
dense
206205
class="mt-0"
207-
hide-details
208-
label="隐藏悬浮图标"
206+
label="勿扰模式"
207+
hint="不再发出系统通知 (仍记日志)"
208+
persistent-hint
209+
v-model="box.usercfgs.isMute"
210+
@change="saveUserCfgs"
211+
></v-switch>
212+
<v-spacer></v-spacer>
213+
<v-btn fab small text>
214+
<v-avatar size="32"><v-icon>mdi-volume-off</v-icon></v-avatar>
215+
</v-btn>
216+
</v-list-item>
217+
<v-list-item class="mt-4">
218+
<v-switch
219+
dense
220+
class="mt-0"
221+
label="隐藏帮助按钮"
222+
hint="隐藏悬浮按钮中的帮忙"
223+
persistent-hint
224+
v-model="box.usercfgs.isHideHelp"
225+
@change="saveUserCfgs"
226+
></v-switch>
227+
<v-spacer></v-spacer>
228+
<v-btn fab small text @click="window.open(box.syscfgs.boxjs.repo)">
229+
<v-avatar size="32"><v-icon>mdi-help</v-icon></v-avatar>
230+
</v-btn>
231+
</v-list-item>
232+
<v-list-item class="mt-4">
233+
<v-switch
234+
dense
235+
class="mt-0"
236+
label="隐藏悬浮按钮"
237+
hint="右下角悬浮按钮"
238+
persistent-hint
209239
v-model="box.usercfgs.isHideBoxIcon"
210240
@change="saveUserCfgs"
211241
></v-switch>
@@ -220,8 +250,9 @@
220250
<v-switch
221251
dense
222252
class="mt-0"
223-
hide-details
224253
label="隐藏我的标题"
254+
hint="底栏`我的`二字 (只显示图标)"
255+
persistent-hint
225256
v-model="box.usercfgs.isHideMyTitle"
226257
@change="saveUserCfgs"
227258
></v-switch>
@@ -233,39 +264,34 @@
233264
<v-icon v-else size="32">mdi-face-profile</v-icon>
234265
</v-btn>
235266
</v-list-item>
236-
<v-list-item class="mt-4">
237-
<v-switch dense class="mt-0" hide-details label="勿扰模式" v-model="box.usercfgs.isMute" @change="saveUserCfgs"></v-switch>
238-
<v-spacer></v-spacer>
239-
<v-btn fab small text>
240-
<v-avatar size="32"><v-icon>mdi-volume-off</v-icon></v-avatar>
241-
</v-btn>
242-
</v-list-item>
243267
<v-list-item class="mt-4">
244268
<v-switch
245269
dense
246270
class="mt-0"
247-
hide-details
248-
label="隐藏帮助按钮"
249-
v-model="box.usercfgs.isHideHelp"
271+
label="隐藏刷新按钮"
272+
hint="双击悬浮按钮也可刷新页面"
273+
persistent-hint
274+
v-model="box.usercfgs.isHideRefresh"
250275
@change="saveUserCfgs"
251276
></v-switch>
252277
<v-spacer></v-spacer>
253-
<v-btn fab small text @click="window.open(box.syscfgs.boxjs.repo)">
254-
<v-avatar size="32"><v-icon>mdi-help</v-icon></v-avatar>
278+
<v-btn fab small text>
279+
<v-avatar size="32"><v-icon>mdi-refresh</v-icon></v-avatar>
255280
</v-btn>
256281
</v-list-item>
257282
<v-list-item class="mt-4">
258283
<v-switch
259284
dense
260285
class="mt-0"
261-
hide-details
262-
label="隐藏刷新按钮"
263-
v-model="box.usercfgs.isHideRefresh"
286+
label="调试模式 (页面)"
287+
hint="每次访问都获取最新的页面"
288+
persistent-hint
289+
v-model="box.usercfgs.isDebugWeb"
264290
@change="saveUserCfgs"
265291
></v-switch>
266292
<v-spacer></v-spacer>
267293
<v-btn fab small text>
268-
<v-avatar size="32"><v-icon>mdi-refresh</v-icon></v-avatar>
294+
<v-avatar size="32"><v-icon>mdi-language-html5</v-icon></v-avatar>
269295
</v-btn>
270296
</v-list-item>
271297
<!-- <v-list-item class="mt-4">
@@ -675,7 +701,7 @@ <h2>{{curapp.name}}</h2>
675701
</v-btn>
676702
</v-subheader>
677703
<template v-for="(sub, subIdx) in appSubs" :key="sub.id">
678-
<v-list-item dense two-line @click="">
704+
<v-list-item dense two-line @click="reloadAppSub(sub)">
679705
<v-list-item-avatar v-if="sub.icon"><v-img :src="sub.icon" /></v-list-item-avatar>
680706
<v-list-item-avatar v-else color="primary"><v-icon dark>mdi-account</v-icon></v-list-item-avatar>
681707
<v-list-item-content>
@@ -910,7 +936,7 @@ <h2>{{curbak.name}}</h2>
910936
<v-btn dark fab small color="success" @click="">
911937
<v-icon>mdi-export-variant</v-icon>
912938
</v-btn>
913-
<v-btn dark v-if="!box.usercfgs.isHideRefresh" fab small color="orange" @click="">
939+
<v-btn dark v-if="!box.usercfgs.isHideRefresh" fab small color="orange" @dblclick="window.location.reload()">
914940
<v-icon>mdi-refresh</v-icon>
915941
</v-btn>
916942
</v-speed-dial>
@@ -935,6 +961,7 @@ <h2>{{curbak.name}}</h2>
935961
data() {
936962
return {
937963
ui: {
964+
isCors: false,
938965
path: null,
939966
bfpath: null,
940967
view: null,
@@ -966,6 +993,10 @@ <h2>{{curbak.name}}</h2>
966993
version() {
967994
return this.box.syscfgs.version
968995
},
996+
// 判断是否需要跨域请求
997+
isCors() {
998+
return this.ui.isCors
999+
},
9691000
// 判断当前是否`WebApp`
9701001
isWebApp() {
9711002
return window.navigator.standalone
@@ -1038,6 +1069,13 @@ <h2>{{curbak.name}}</h2>
10381069
}
10391070
return 1
10401071
},
1072+
// 获取环境图标下标, 透明: 0, 彩色: 1 (默认)
1073+
iconEnvThemeIdx() {
1074+
if (this.isWebApp) {
1075+
return this.isTransparentIcons ? 0 : 1
1076+
}
1077+
return this.iconThemeIdx
1078+
},
10411079
isSaveUserCfgs: {
10421080
set(val) {
10431081
this.ui.isSaveUserCfgs = val
@@ -1161,7 +1199,7 @@ <h2>{{curbak.name}}</h2>
11611199
watch: {
11621200
'ui.path': {
11631201
handler(newval, oldval) {
1164-
if (!this.isWebApp) {
1202+
if (!this.isWebApp && !this.isCors) {
11651203
history.pushState({ title: 'BoxJs' }, '', newval)
11661204
}
11671205
const [, view, subview] = newval.split('/')
@@ -1200,10 +1238,21 @@ <h2>{{curbak.name}}</h2>
12001238
}
12011239
},
12021240
async created() {
1241+
// 如果 url 参数中指定的 baseURL, 则后续的请求都请求到指定的域
1242+
if (window.location.search) {
1243+
const [, baseURL] = /baseURL=(.*?)(&|$)/.exec(window.location.search)
1244+
axios.defaults.baseURL = baseURL || ''
1245+
this.ui.isCors = true
1246+
}
12031247
// 根据路径跳转视图
1204-
this.path = location.pathname
1248+
if (!this.isCors) {
1249+
const path = location.pathname
1250+
this.path = path === '/' ? '/app' : path
1251+
} else {
1252+
this.path = '/app'
1253+
}
12051254
// 监听浏览器后退事件
1206-
window.addEventListener('popstate', () => (this.path = location.pathname), false)
1255+
window.addEventListener('popstate', () => (this.path = path), false)
12071256
// 获取 BoxJs 数据, 取到数据再渲染页面
12081257
await axios.get('/query/boxdata').then((resp) => (this.box = resp.data))
12091258
// 获取全局备份
@@ -1223,6 +1272,8 @@ <h2>{{curbak.name}}</h2>
12231272
back() {
12241273
if (this.view === 'bak' && !this.bfpath) {
12251274
this.path = '/my'
1275+
} else if (this.view === 'app' && !this.bfpath) {
1276+
this.path = '/app'
12261277
} else {
12271278
this.path = this.bfpath
12281279
}
@@ -1449,7 +1500,7 @@ <h2>{{curbak.name}}</h2>
14491500
const curSessions = [{ key, val }]
14501501
const datas = [...session.datas, ...curSessions]
14511502
axios
1452-
.post('/api/save', [{ key, val }])
1503+
.post('/api/save', datas)
14531504
.then((resp) => {
14541505
delete resp.data.usercfgs
14551506
Object.assign(this.box, resp.data)
@@ -1461,7 +1512,7 @@ <h2>{{curbak.name}}</h2>
14611512
this.ui.overlay.show = true
14621513
const datas = this.curapp.datas
14631514
const data = datas.find((data) => data.key === key)
1464-
data.val = null
1515+
data.val = ''
14651516
axios
14661517
.post('/api/save', datas)
14671518
.then((resp) => {

‎box/chavy.boxjs.js‎

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const $ = new Env('BoxJs')
22

3-
$.version = '0.7.4'
3+
$.version = '0.7.5'
44
$.versionType = 'beta'
55

66
// 存储`用户偏好`
@@ -21,8 +21,6 @@ $.json = $.name // `接口`类请求的响应体
2121
$.html = $.name // `页面`类请求的响应体
2222

2323
$.web = `https://cdn.jsdelivr.net/gh/chavyleung/scripts@${$.version}/box/chavy.boxjs.html`
24-
// $.web = `http://192.168.50.109:8080/box/chavy.boxjs.html?_=${new Date().getTime()}`
25-
// $.web = `http://192.168.8.112:8080/box/chavy.boxjs.html?_=${new Date().getTime()}`
2624

2725
!(async () => {
2826
// 勿扰模式
@@ -35,6 +33,8 @@ $.web = `https://cdn.jsdelivr.net/gh/chavyleung/scripts@${$.version}/box/chavy.b
3533
$.isGet = $request.method === 'GET'
3634
// 请求类型: POST
3735
$.isPost = $request.method === 'POST'
36+
// 请求类型: OPTIONS
37+
$.isOptions = $request.method === 'OPTIONS'
3838

3939
// 请求类型: page、api、query
4040
$.type = 'page'
@@ -48,8 +48,12 @@ $.web = `https://cdn.jsdelivr.net/gh/chavyleung/scripts@${$.version}/box/chavy.b
4848
// 升级用户数据
4949
upgradeUserData()
5050

51+
// 处理预检请求
52+
if ($.isOptions) {
53+
await handleOptions()
54+
}
5155
// 处理`页面`请求
52-
if ($.isPage) {
56+
else if ($.isPage) {
5357
$.type = 'page'
5458
await handlePage()
5559
}
@@ -97,8 +101,10 @@ function getDomain(url) {
97101
* 处理`页面`请求
98102
*/
99103
async function handlePage() {
104+
// 调试模式: 是否每次都获取新的页面
105+
const isDebugWeb = [true, 'true'].includes($.getdata('@chavy_boxjs_userCfgs.isDebugWeb'))
100106
const cache = $.getjson($.KEY_web_cache, null)
101-
if (cache && cache.version === $.version) {
107+
if (!isDebugWeb && cache && cache.version === $.version) {
102108
$.html = cache.cache
103109
} else {
104110
await $.http.get($.web).then(
@@ -156,6 +162,8 @@ async function handleApi() {
156162
}
157163
}
158164

165+
async function handleOptions() {}
166+
159167
/**
160168
* ===================================
161169
* 获取基础数据
@@ -504,14 +512,46 @@ function upgradeUserData() {
504512
* ===================================
505513
*/
506514
function doneBox() {
507-
if ($.isPage) donePage()
515+
if ($.isOptions) doneOptions()
516+
else if ($.isPage) donePage()
508517
else if ($.isQuery) doneQuery()
509518
else if ($.isApi) doneApi()
510519
else $.done()
511520
}
512521

522+
function getBaseDoneHeaders(mixHeaders = {}) {
523+
return Object.assign(
524+
{
525+
'Access-Control-Allow-Origin': '*',
526+
'Access-Control-Allow-Methods': 'POST,GET,OPTIONS,PUT,DELETE',
527+
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept'
528+
},
529+
mixHeaders
530+
)
531+
}
532+
533+
function getHtmlDoneHeaders() {
534+
return getBaseDoneHeaders({
535+
'Content-Type': 'text/html;charset=UTF-8'
536+
})
537+
}
538+
function getJsonDoneHeaders() {
539+
return getBaseDoneHeaders({
540+
'Content-Type': 'text/json; charset=utf-8'
541+
})
542+
}
543+
544+
function doneOptions() {
545+
const headers = getBaseDoneHeaders()
546+
if ($.isSurge() || $.isLoon()) {
547+
$.done({ response: { headers } })
548+
} else if ($.isQuanX()) {
549+
$.done({ headers })
550+
}
551+
}
552+
513553
function donePage() {
514-
const headers = { 'Access-Control-Allow-Origin': '*', 'Content-Type': 'text/html;charset=UTF-8' }
554+
const headers = getHtmlDoneHeaders()
515555
if ($.isSurge() || $.isLoon()) {
516556
$.done({ response: { status: 200, headers, body: $.html } })
517557
} else if ($.isQuanX()) {
@@ -521,7 +561,7 @@ function donePage() {
521561

522562
function doneQuery() {
523563
$.json = $.toStr($.json)
524-
const headers = { 'Access-Control-Allow-Origin': '*', 'Content-Type': 'text/json; charset=utf-8' }
564+
const headers = getJsonDoneHeaders()
525565
if ($.isSurge() || $.isLoon()) {
526566
$.done({ response: { status: 200, headers, body: $.json } })
527567
} else if ($.isQuanX()) {
@@ -531,7 +571,7 @@ function doneQuery() {
531571

532572
function doneApi() {
533573
$.json = $.toStr($.json)
534-
const headers = { 'Access-Control-Allow-Origin': '*', 'Content-Type': 'text/json; charset=utf-8' }
574+
const headers = getJsonDoneHeaders()
535575
if ($.isSurge() || $.isLoon()) {
536576
$.done({ response: { status: 200, headers, body: $.json } })
537577
} else if ($.isQuanX()) {

0 commit comments

Comments
 (0)