|
| 1 | +/** |
| 2 | + * @name ASUS 键盘背光联动 |
| 3 | + * @id winddrift.asus-keyboard-light |
| 4 | + * @version 1.0.0 |
| 5 | + * @description 监听当前播放曲目封面变化,通过本地桥接服务将封面主题色应用到华硕键盘背光 |
| 6 | + * @author WindDrift |
| 7 | + * @homepage https://github.com/WindDrift/SPlayer-ASUS-Keyboard-Light-Controller |
| 8 | + * @type control |
| 9 | + * @grant network |
| 10 | + * @apiLevel 2 |
| 11 | + * @updateUrl https://raw.githubusercontent.com/WindDrift/SPlayer-ASUS-Keyboard-Light-Controller/main/SPlayer-ASUS-Keyboard-Light-Controller.js |
| 12 | + */ |
| 13 | + |
| 14 | +splayer.register({ |
| 15 | + events: ["trackChange"], |
| 16 | + settings: [ |
| 17 | + { |
| 18 | + key: "enabled", |
| 19 | + type: "switch", |
| 20 | + label: "启用键盘背光联动", |
| 21 | + default: true, |
| 22 | + }, |
| 23 | + { |
| 24 | + key: "bridgeUrl", |
| 25 | + type: "text", |
| 26 | + label: "桥接服务地址", |
| 27 | + default: "http://127.0.0.1:38901", |
| 28 | + placeholder: "http://127.0.0.1:38901", |
| 29 | + }, |
| 30 | + { |
| 31 | + key: "mode", |
| 32 | + type: "select", |
| 33 | + label: "灯效类型", |
| 34 | + default: "0", |
| 35 | + options: [ |
| 36 | + { label: "恒亮", value: "0" }, |
| 37 | + { label: "呼吸", value: "1" }, |
| 38 | + { label: "色彩循环", value: "2" }, |
| 39 | + { label: "闪烁", value: "10" }, |
| 40 | + ], |
| 41 | + }, |
| 42 | + { |
| 43 | + key: "brightness", |
| 44 | + type: "select", |
| 45 | + label: "亮度", |
| 46 | + default: "3", |
| 47 | + options: [ |
| 48 | + { label: "低", value: "1" }, |
| 49 | + { label: "中", value: "2" }, |
| 50 | + { label: "高", value: "3" }, |
| 51 | + ], |
| 52 | + }, |
| 53 | + ], |
| 54 | +}); |
| 55 | + |
| 56 | +// 缓存当前曲目信息,用于设置变更时重新应用 |
| 57 | +let currentTrack = null; |
| 58 | + |
| 59 | +/** |
| 60 | + * 将封面主题色通过桥接服务应用到键盘背光 |
| 61 | + * @param {object} track - 当前曲目对象,需包含 cover 字段 |
| 62 | + */ |
| 63 | +const applyBacklight = (track) => { |
| 64 | + if (!splayer.getSetting("enabled")) return; |
| 65 | + const url = track && track.cover; |
| 66 | + if (!url || !/^https?:\/\//i.test(url)) { |
| 67 | + splayer.log.debug("仅支持 HTTP 封面,跳过:", url); |
| 68 | + return; |
| 69 | + } |
| 70 | + const bridgeUrl = splayer.getSetting("bridgeUrl"); |
| 71 | + const mode = Number(splayer.getSetting("mode")); |
| 72 | + const brightness = Number(splayer.getSetting("brightness")); |
| 73 | + splayer |
| 74 | + .request(`${bridgeUrl}/apply`, { |
| 75 | + method: "POST", |
| 76 | + headers: { "Content-Type": "application/json" }, |
| 77 | + body: JSON.stringify({ |
| 78 | + url, |
| 79 | + mode, |
| 80 | + brightness, |
| 81 | + title: track.title || "", |
| 82 | + artist: track.artist || "", |
| 83 | + album: track.album || "", |
| 84 | + }), |
| 85 | + timeout: 10000, |
| 86 | + }) |
| 87 | + .then(({ body }) => { |
| 88 | + if (body && body.ok) { |
| 89 | + splayer.log.info("已应用键盘背光颜色:", body.color); |
| 90 | + } else { |
| 91 | + splayer.log.warn("桥接服务返回失败:", body && body.error); |
| 92 | + } |
| 93 | + }) |
| 94 | + .catch((err) => { |
| 95 | + splayer.log.warn("应用键盘背光失败:", err); |
| 96 | + }); |
| 97 | +}; |
| 98 | + |
| 99 | +splayer.player.on("trackChange", ({ track }) => { |
| 100 | + if (!track || !track.cover) { |
| 101 | + currentTrack = null; |
| 102 | + return; |
| 103 | + } |
| 104 | + currentTrack = track; |
| 105 | + applyBacklight(currentTrack); |
| 106 | +}); |
| 107 | + |
| 108 | +splayer.onSettingChange("mode", () => { |
| 109 | + if (currentTrack) applyBacklight(currentTrack); |
| 110 | +}); |
| 111 | + |
| 112 | +splayer.onSettingChange("brightness", () => { |
| 113 | + if (currentTrack) applyBacklight(currentTrack); |
| 114 | +}); |
| 115 | + |
| 116 | +splayer.onSettingChange("enabled", (value) => { |
| 117 | + if (value && currentTrack) applyBacklight(currentTrack); |
| 118 | +}); |
0 commit comments