|
| 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))} |
0 commit comments