Skip to content

Commit 29e5f78

Browse files
committed
添加[box.switcher.js]: 定时运行 switcher.js 即可自动切换会话
1 parent 1dd3dbe commit 29e5f78

2 files changed

Lines changed: 124 additions & 2 deletions

File tree

‎box/switcher/box.switcher.js‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
const $ = new Env('会话切换')
2+
$.KEY_sessions = 'chavy_boxjs_sessions'
3+
4+
!(async () => {
5+
$.log('', `🔔 ${$.name}, 开始!`, '')
6+
await execSwitch()
7+
})()
8+
.catch((e) => {
9+
$.log('', `❌ ${$.name}, 失败! 原因: ${e}!`, '')
10+
})
11+
.finally(() => {
12+
$.msg($.name, $.subt, $.desc), $.log('', `🔔 ${$.name}, 结束!`, ''), $.done()
13+
})
14+
15+
function execSwitch() {
16+
return new Promise((resove) => {
17+
// 会话排序: `创建时间`升序
18+
const sessions = getSessions()
19+
sessions.sort((a, b) => a.createTime.replace(/-|:| /g, '') - b.createTime.replace(/-|:| /g, ''))
20+
const apps = {}
21+
sessions.forEach((session) => {
22+
const appId = session.appId
23+
const appName = session.appName
24+
apps[appId] = apps[appId] ? apps[appId] : { id: appId, name: appName, sessions: [], skipedSessions: [] }
25+
const app = apps[appId]
26+
session.datas.sort((a, b) => a.key.localeCompare(b.key))
27+
const appDatas = []
28+
session.datas.forEach((data) => appDatas.push({ key: data.key, val: $.getdata(data.key) }))
29+
// 会话列表自重复
30+
const duplicateSession = app.sessions.find((_session) => JSON.stringify(_session.datas) === JSON.stringify(session.datas))
31+
if (duplicateSession) {
32+
$.log(`⚠️ ${$.name}, ${appName}: ${session.name} 与 ${duplicateSession.name} 重复, 建议删除!`)
33+
app.skipedSessions.push(session)
34+
return true
35+
}
36+
// 会话为当前会话
37+
else if (JSON.stringify(session.datas) === JSON.stringify(appDatas)) {
38+
app.sessions.push(session)
39+
app.curSessionId = session.id
40+
return true
41+
}
42+
// 继续添加会话
43+
else {
44+
app.sessions.push(session)
45+
}
46+
})
47+
Object.keys(apps).forEach((appId) => {
48+
const app = apps[appId]
49+
if (app.sessions.length <= 1) {
50+
$.log(`⚠️ ${$.name}, ${app.name}: 跳过!`)
51+
return true
52+
}
53+
const curSessionIdx = app.sessions.findIndex((session) => session.id === app.curSessionId)
54+
const curSession = app.sessions[curSessionIdx]
55+
const isNewRound = curSessionIdx + 1 === app.sessions.length
56+
const nextSessionIdx = isNewRound ? 0 : curSessionIdx + 1
57+
const nextSession = app.sessions[nextSessionIdx]
58+
nextSession.datas.forEach((_data) => $.setdata(_data.val, _data.key) || true)
59+
$.log(`❕ ${$.name}, ${curSession.appName}: ${curSession.name} => ${nextSession.name} ${isNewRound ? '(新一轮)' : ''}`)
60+
})
61+
62+
resove()
63+
})
64+
}
65+
66+
function getSessions() {
67+
const sessionstr = $.getdata($.KEY_sessions)
68+
const sessions = sessionstr ? JSON.parse(sessionstr) : []
69+
return Array.isArray(sessions) ? sessions : []
70+
}
71+
72+
// prettier-ignore
73+
function Env(t){this.name=t,this.logs=[],this.isSurge=(()=>"undefined"!=typeof $httpClient),this.isQuanX=(()=>"undefined"!=typeof $task),this.log=((...t)=>{this.logs=[...this.logs,...t],t?console.log(t.join("\n")):console.log(this.logs.join("\n"))}),this.msg=((t=this.name,s="",i="")=>{this.isSurge()&&$notification.post(t,s,i),this.isQuanX()&&$notify(t,s,i),this.log("==============\ud83d\udce3\u7cfb\u7edf\u901a\u77e5\ud83d\udce3=============="),t&&this.log(t),s&&this.log(s),i&&this.log(i)}),this.getdata=(t=>this.isSurge()?$persistentStore.read(t):this.isQuanX()?$prefs.valueForKey(t):void 0),this.setdata=((t,s)=>this.isSurge()?$persistentStore.write(t,s):this.isQuanX()?$prefs.setValueForKey(t,s):void 0),this.get=((t,s)=>this.send(t,"GET",s)),this.wait=((t,s=t)=>i=>setTimeout(()=>i(),Math.floor(Math.random()*(s-t+1)+t))),this.post=((t,s)=>this.send(t,"POST",s)),this.send=((t,s,i)=>{if(this.isSurge()){const e="POST"==s?$httpClient.post:$httpClient.get;e(t,(t,s,e)=>{s.body=e,s.statusCode=s.status,i(t,s,e)})}this.isQuanX()&&(t.method=s,$task.fetch(t).then(t=>{t.status=t.statusCode,i(null,t,t.body)},t=>i(t.error,t,t)))}),this.done=((t={})=>$done(t))}

‎chavy.box.js‎

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ function getPath(url) {
4646
return new RegExp($.domain).test(url) ? fullpath.split($.domain)[1] : undefined
4747
}
4848

49+
function getSystemCfgs() {
50+
return {
51+
env: $.isSurge() ? 'Surge' : $.isQuanX ? 'QuanX' : 'Loon',
52+
envs: [
53+
{ id: 'Surge', icon: 'https://is3-ssl.mzstatic.com/image/thumb/Purple123/v4/21/0a/0d/210a0df9-cbe1-b9dc-7549-4c10cd996279/AppIcon-0-0-1x_U007emarketing-0-0-0-6-0-0-sRGB-0-0-0-GLES2_U002c0-512MB-85-220-0-0.png/492x0w.png' },
54+
{ id: 'QuanX', icon: 'https://is4-ssl.mzstatic.com/image/thumb/Purple123/v4/ae/7c/a2/ae7ca2fb-c4b6-d3a9-885a-935c1ea34cdb/AppIcon-1x_U007emarketing-0-7-0-0-85-220.png/492x0w.png' },
55+
{ id: 'Loon', icon: 'https://is2-ssl.mzstatic.com/image/thumb/Purple123/v4/59/2c/fb/592cfb3b-162e-83dc-4e96-7a2175e1d29f/AppIcon-0-1x_U007emarketing-0-7-0-0-85-220.png/492x0w.png' }
56+
]
57+
}
58+
}
59+
4960
function getSystemApps() {
5061
const sysapps = [
5162
{
@@ -72,6 +83,17 @@ function getSystemApps() {
7283
repo: 'https://github.com/chavyleung/scripts/tree/master/acfun',
7384
icon: 'https://is5-ssl.mzstatic.com/image/thumb/Purple123/v4/41/f0/11/41f011f8-1bbc-188f-220f-3db5164a9ae9/AppIcon-1x_U007emarketing-0-7-0-0-85-220.png/492x0w.png'
7485
},
86+
{
87+
id: 'ApkTw',
88+
name: 'ApkTw',
89+
keys: ['chavy_cookie_apktw'],
90+
author: '@chavyleung',
91+
repo: 'https://github.com/chavyleung/scripts/tree/master/apktw',
92+
url: 'https://apk.tw/',
93+
icon: 'https://raw.githubusercontent.com/Orz-3/mini/master/apktw.png',
94+
tasks: [{ cron: '3 0 * * *', script: 'noteyoudao.js' }],
95+
rewrites: [{ type: 'request', pattern: '^https://note.youdao.com/yws/mapi/user?method=checkin', script: 'noteyoudao.cookie.js', body: true }]
96+
},
7597
{
7698
id: 'BAIDU',
7799
name: '百度签到',
@@ -119,6 +141,17 @@ function getSystemApps() {
119141
author: '@chavyleung',
120142
repo: 'https://github.com/chavyleung/scripts/tree/master/v2ex',
121143
icon: 'https://raw.githubusercontent.com/Orz-3/mini/master/v2ex.png'
144+
},
145+
{
146+
id: 'NoteYoudao',
147+
name: '有道云笔记',
148+
keys: ['chavy_signurl_noteyoudao', 'chavy_signbody_noteyoudao', 'chavy_signheaders_noteyoudao'],
149+
author: '@chavyleung',
150+
repo: 'https://github.com/chavyleung/scripts/tree/master/noteyoudao',
151+
url: 'https://apps.apple.com/cn/app/有道云笔记-扫描王版/id450748070',
152+
icon: 'https://is1-ssl.mzstatic.com/image/thumb/Purple113/v4/25/6a/00/256a002d-b5f0-46e1-ef55-841d41f8aafc/AppIcon-0-1x_U007emarketing-0-7-0-0-85-220.png/460x0w.png',
153+
tasks: [{ cron: '3 0 * * *', script: 'noteyoudao.js' }],
154+
rewrites: [{ type: 'request', pattern: '^https://note.youdao.com/yws/mapi/user?method=checkin', script: 'noteyoudao.cookie.js', body: true }]
122155
}
123156
]
124157
sysapps.forEach((app) => {
@@ -209,6 +242,7 @@ function handleHome() {
209242
sessions: getSessions(),
210243
sysapps: getSystemApps(),
211244
userapps: getUserApps(),
245+
syscfgs: getSystemCfgs(),
212246
colors: getSystemThemes()
213247
})
214248
$.html = `
@@ -225,13 +259,28 @@ function handleHome() {
225259
<div id="app">
226260
<v-app>
227261
<v-app-bar :color="ui.appbar.color" app dense>
228-
<v-menu bottom left v-if="['app', 'home', 'log', 'data'].includes(ui.curview)">
262+
<v-menu bottom left v-if="['app', 'home', 'log', 'data'].includes(ui.curview) && box.syscfgs.env === ''">
229263
<template v-slot:activator="{ on }">
230264
<v-btn icon v-on="on"><v-icon>mdi-palette</v-icon></v-btn>
231265
</template>
232266
<v-list>
233267
<v-list-item v-for="(color, colorIdx) in box.colors" :key="color.id" @click="ui.appbar.color=color.id">
234-
<v-list-item-title>{{ color.name }}</v-list-item-title>
268+
<v-list-item-title>22{{ color.name }}</v-list-item-title>
269+
</v-list-item>
270+
</v-list>
271+
</v-menu>
272+
<v-menu bottom left v-else-if="['app', 'home', 'log', 'data'].includes(ui.curview) && box.syscfgs.env !== ''">
273+
<template v-slot:activator="{ on }">
274+
<v-btn icon v-on="on">
275+
<v-avatar size="24">
276+
<img :src="box.syscfgs.envs.find(e=>e.id===box.syscfgs.env).icon" alt="box.syscfgs.env" />
277+
</v-avatar>
278+
</v-btn>
279+
</template>
280+
<v-list>
281+
<v-list-item v-for="(env, envIdx) in box.syscfgs.envs" :key="env.id" @click="box.syscfgs.env=env.id">
282+
<v-list-item-avatar size="24"><v-img :src="env.icon"></v-img></v-list-item-avatar>
283+
<v-list-item-title>{{ env.id }}</v-list-item-title>
235284
</v-list-item>
236285
</v-list>
237286
</v-menu>

0 commit comments

Comments
 (0)