Skip to content

Commit b45223f

Browse files
committed
Closed #143
在 console 中使用 `executeScript` 能打开 pip,但是写在 background.ts 中无效
1 parent a4b43ef commit b45223f

File tree

7 files changed

+31
-20
lines changed

7 files changed

+31
-20
lines changed

Diff for: public/manifest.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@
6666
"default": "Ctrl+Shift+L",
6767
"mac": "Command+Shift+L"
6868
},
69-
"description": "Toggle lyrics"
69+
"description": "Toggle lyrics",
70+
"global": true
7071
}
7172
},
7273
"icons": {

Diff for: src/background.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
isFirefox,
1111
isRateTest,
1212
} from './common/constants';
13-
import { sendMessage } from './common/bg';
13+
import { getTabs, sendMessage } from './common/bg';
1414
import { getOptions } from './options/store';
1515
import { i18n, i18nMap } from './i18n';
1616
import type { Req, Res } from './page/request';
@@ -118,7 +118,13 @@ browser.runtime.onMessage.addListener(async (msg: Message, sender) => {
118118
browser.commands.onCommand.addListener((command) => {
119119
switch (command) {
120120
case 'toggle': {
121-
return sendMessage({ type: Event.TOGGLE });
121+
return getTabs().then(async (tabs) => {
122+
const tab = tabs.find((e) => !!e.id);
123+
if (!tab) return;
124+
await browser.windows.update(tab.windowId!, { focused: true });
125+
await browser.tabs.update(tab.id!, { active: true });
126+
sendMessage({ type: Event.TOGGLE });
127+
});
122128
}
123129
}
124130
});

Diff for: src/common/bg.ts

+14-11
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@ import browser from 'webextension-polyfill';
22

33
import { Event, isProd, type Message } from './constants';
44

5-
export function sendMessage<T>(tabId: number, msg: Message<T>): void;
6-
export function sendMessage<T>(msg: Message<T>): void;
7-
export function sendMessage<T>(tabIdOrMsg: number | Message<T>, msg?: Message<T>) {
5+
export async function getTabs() {
6+
return browser.tabs.query({
7+
url: browser.runtime.getManifest().content_scripts![0].matches,
8+
});
9+
}
10+
11+
export async function sendMessage<T>(tabId: number, msg: Message<T>): Promise<void>;
12+
export async function sendMessage<T>(msg: Message<T>): Promise<void>;
13+
export async function sendMessage<T>(tabIdOrMsg: number | Message<T>, msg?: Message<T>) {
814
if (typeof tabIdOrMsg === 'number') {
915
browser.tabs.sendMessage(tabIdOrMsg, msg);
1016
} else {
11-
browser.tabs
12-
.query({ url: browser.runtime.getManifest().content_scripts![0].matches })
13-
.then((tabs) => {
14-
tabs.forEach((tab) => {
15-
// Only the tab that open the lyrics will response
16-
if (tab?.id) browser.tabs.sendMessage(tab.id, tabIdOrMsg);
17-
});
18-
});
17+
const tabs = await getTabs();
18+
tabs.forEach((tab) => {
19+
// Only the tab that open the lyrics will response
20+
if (tab?.id) browser.tabs.sendMessage(tab.id, tabIdOrMsg);
21+
});
1922
}
2023
}
2124

Diff for: src/options/app.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export class OptionsApp extends GemElement<State> {
6565

6666
copyIdHandler = (e: KeyboardEvent) => {
6767
const { options } = this.state;
68-
if (e.key === 'c' && options) {
68+
if (e.key.toLowerCase() === 'c' && options) {
6969
navigator.clipboard.writeText(options.cid);
7070
}
7171
};

Diff for: src/page/btn.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ window.addEventListener(
5050
) {
5151
return;
5252
}
53-
if (e.key === options['toggle-shortcut'] && !e.repeat) {
53+
if (e.key.toLowerCase() === options['toggle-shortcut'] && !e.repeat) {
5454
// Execute in current microtask
5555
e.stopImmediatePropagation();
5656
e.stopPropagation();
@@ -102,6 +102,7 @@ export const insetLyricsBtn = async () => {
102102

103103
btnWrapper.style.display = 'flex';
104104
const lyricsBtn = likeBtn.cloneNode(true) as HTMLButtonElement;
105+
(window as any).lyricsBtn = lyricsBtn;
105106
lyricsBtn.classList.add(localConfig.LYRICS_CLASSNAME);
106107

107108
lyricsBtn.disabled = false;

Diff for: src/page/netease.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ export async function fetchNetEaseChineseName(
5656
}
5757

5858
// auto swtch to lrclib
59-
let down = false;
59+
let down = 0;
6060
export async function fetchNetEaseSongList(s: string, fetchOptions?: RequestInit) {
61-
if (down) return fetchLRCLIBSongList(s, fetchOptions);
61+
if (down > 3) return fetchLRCLIBSongList(s, fetchOptions);
6262

6363
const { API_HOST } = await configPromise;
6464
const searchQuery = new URLSearchParams({
@@ -71,7 +71,7 @@ export async function fetchNetEaseSongList(s: string, fetchOptions?: RequestInit
7171
const res: SearchSongsResult = await request(`${API_HOST}/search?${searchQuery}`, fetchOptions);
7272
return res.result?.songs || [];
7373
} catch (err) {
74-
down = true;
74+
down++;
7575
return fetchLRCLIBSongList(s, fetchOptions);
7676
}
7777
}

Diff for: src/popup/elements/root.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { i18n } from '../../i18n';
99
import './list';
1010

1111
window.addEventListener('keydown', (e) => {
12-
if (e.key === 'i') {
12+
if (e.key.toLowerCase() === 'i') {
1313
const id = Number(prompt('Enter NetEase Cloud Music ID:'));
1414
if (id) changeSong(id);
1515
}

0 commit comments

Comments
 (0)