From e5cde79ab332e39b5a106a321bb18af8f5584e0b Mon Sep 17 00:00:00 2001 From: mantou132 <709922234@qq.com> Date: Mon, 29 Jul 2024 23:38:19 +0800 Subject: [PATCH] Closed #143 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在 console 中使用 `executeScript` 能打开 pip,但是写在 background.ts 中无效 --- src/common/bg.ts | 25 ++++++++++++++----------- src/options/app.ts | 2 +- src/page/btn.ts | 3 ++- src/page/netease.ts | 6 +++--- src/popup/elements/root.ts | 2 +- 5 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/common/bg.ts b/src/common/bg.ts index f20b16c..8f40d89 100644 --- a/src/common/bg.ts +++ b/src/common/bg.ts @@ -2,20 +2,23 @@ import browser from 'webextension-polyfill'; import { Event, isProd, type Message } from './constants'; -export function sendMessage(tabId: number, msg: Message): void; -export function sendMessage(msg: Message): void; -export function sendMessage(tabIdOrMsg: number | Message, msg?: Message) { +export async function getTags() { + return browser.tabs.query({ + url: browser.runtime.getManifest().content_scripts![0].matches, + }); +} + +export async function sendMessage(tabId: number, msg: Message): Promise; +export async function sendMessage(msg: Message): Promise; +export async function sendMessage(tabIdOrMsg: number | Message, msg?: Message) { if (typeof tabIdOrMsg === 'number') { browser.tabs.sendMessage(tabIdOrMsg, msg); } else { - browser.tabs - .query({ url: browser.runtime.getManifest().content_scripts![0].matches }) - .then((tabs) => { - tabs.forEach((tab) => { - // Only the tab that open the lyrics will response - if (tab?.id) browser.tabs.sendMessage(tab.id, tabIdOrMsg); - }); - }); + const tabs = await getTags(); + tabs.forEach((tab) => { + // Only the tab that open the lyrics will response + if (tab?.id) browser.tabs.sendMessage(tab.id, tabIdOrMsg); + }); } } diff --git a/src/options/app.ts b/src/options/app.ts index 0dbc6bb..9565c1a 100644 --- a/src/options/app.ts +++ b/src/options/app.ts @@ -65,7 +65,7 @@ export class OptionsApp extends GemElement { copyIdHandler = (e: KeyboardEvent) => { const { options } = this.state; - if (e.key === 'c' && options) { + if (e.key.toLowerCase() === 'c' && options) { navigator.clipboard.writeText(options.cid); } }; diff --git a/src/page/btn.ts b/src/page/btn.ts index a65963c..15a7caa 100644 --- a/src/page/btn.ts +++ b/src/page/btn.ts @@ -50,7 +50,7 @@ window.addEventListener( ) { return; } - if (e.key === options['toggle-shortcut'] && !e.repeat) { + if (e.key.toLowerCase() === options['toggle-shortcut'] && !e.repeat) { // Execute in current microtask e.stopImmediatePropagation(); e.stopPropagation(); @@ -102,6 +102,7 @@ export const insetLyricsBtn = async () => { btnWrapper.style.display = 'flex'; const lyricsBtn = likeBtn.cloneNode(true) as HTMLButtonElement; + (window as any).lyricsBtn = lyricsBtn; lyricsBtn.classList.add(localConfig.LYRICS_CLASSNAME); lyricsBtn.disabled = false; diff --git a/src/page/netease.ts b/src/page/netease.ts index 795cbeb..d90312f 100644 --- a/src/page/netease.ts +++ b/src/page/netease.ts @@ -56,9 +56,9 @@ export async function fetchNetEaseChineseName( } // auto swtch to lrclib -let down = false; +let down = 0; export async function fetchNetEaseSongList(s: string, fetchOptions?: RequestInit) { - if (down) return fetchLRCLIBSongList(s, fetchOptions); + if (down > 3) return fetchLRCLIBSongList(s, fetchOptions); const { API_HOST } = await configPromise; const searchQuery = new URLSearchParams({ @@ -71,7 +71,7 @@ export async function fetchNetEaseSongList(s: string, fetchOptions?: RequestInit const res: SearchSongsResult = await request(`${API_HOST}/search?${searchQuery}`, fetchOptions); return res.result?.songs || []; } catch (err) { - down = true; + down++; return fetchLRCLIBSongList(s, fetchOptions); } } diff --git a/src/popup/elements/root.ts b/src/popup/elements/root.ts index 6449d91..859b7ab 100644 --- a/src/popup/elements/root.ts +++ b/src/popup/elements/root.ts @@ -9,7 +9,7 @@ import { i18n } from '../../i18n'; import './list'; window.addEventListener('keydown', (e) => { - if (e.key === 'i') { + if (e.key.toLowerCase() === 'i') { const id = Number(prompt('Enter NetEase Cloud Music ID:')); if (id) changeSong(id); }