|
1 | 1 | import { app } from "electron"; |
2 | 2 | import { processLog } from "../logger"; |
| 3 | +import mainWindow from "../windows/main-window"; |
3 | 4 |
|
4 | 5 | export const registerCustomProtocol = () => { |
5 | 6 | app.setAsDefaultProtocolClient("orpheus"); |
6 | 7 | processLog.info("🔗 Registered custom protocol"); |
7 | 8 | }; |
8 | 9 |
|
9 | | -export const openCustomProtocol = (str: string): boolean => { |
10 | | - switch (true) { |
11 | | - case str.startsWith("orpheus://"): |
12 | | - handleOpenOrpheus(str); |
| 10 | +export const trySendCustomProtocol = (str: string): boolean => { |
| 11 | + try { |
| 12 | + if (str.startsWith("orpheus://")) { |
| 13 | + mainWindow.getWin()!.webContents.send("protocol-url", str); |
13 | 14 | return true; |
14 | | - default: |
15 | | - return false; |
| 15 | + } |
| 16 | + return false; |
| 17 | + } catch (e) { |
| 18 | + processLog.error("❌ Failed to send protocol url", e); |
| 19 | + return false; |
16 | 20 | } |
17 | 21 | } |
18 | 22 |
|
19 | 23 | export const processProtocolFromCommand = (command: string[]): boolean => { |
20 | 24 | // 这里第一个参数是程序名称 忽略此 仅遍历参数 |
21 | 25 | for (let i = 1; i < command.length; i++) { |
22 | 26 | const arg = command[i]; |
23 | | - if (openCustomProtocol(arg)) return true; |
| 27 | + if (trySendCustomProtocol(arg)) return true; |
24 | 28 | } |
25 | 29 | return false; |
26 | 30 | } |
27 | | - |
28 | | -export const handleOpenOrpheus = (url: string) => { |
29 | | - // 这里的协议是从网页端打开官方客户端的协议 |
30 | | - // 形如 `orpheus://eyJ0eXBlIjoic29uZyIsImlkIjoiMTgyNjM2MTcxMiIsImNtZCI6InBsYXkifQ==` |
31 | | - // URI 的 Path 部分是 Base64 编码过的,解码后得到 Json |
32 | | - // 形如 `{"type":"song","id":"1826361712","cmd":"play"}` |
33 | | - |
34 | | - if (!url.startsWith("orpheus://")) return; |
35 | | - const path = url.replace("orpheus://", ""); |
36 | | - const data = atob(path); |
37 | | - let json: any; |
38 | | - try { |
39 | | - json = JSON.parse(data); |
40 | | - } catch (e) { |
41 | | - processLog.error("❌ Invalid JSON:", e); |
42 | | - return; |
43 | | - } |
44 | | - processLog.info("🚀 Open Orpheus:", json); |
45 | | - // TODO 处理 |
46 | | -}; |
0 commit comments