Skip to content

Commit

Permalink
Closed #143
Browse files Browse the repository at this point in the history
在 console 中使用 `executeScript` 能打开 pip,但是写在 background.ts 中无效
  • Loading branch information
mantou132 committed Jul 30, 2024
1 parent a4b43ef commit b45223f
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 20 deletions.
3 changes: 2 additions & 1 deletion public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@
"default": "Ctrl+Shift+L",
"mac": "Command+Shift+L"
},
"description": "Toggle lyrics"
"description": "Toggle lyrics",
"global": true
}
},
"icons": {
Expand Down
10 changes: 8 additions & 2 deletions src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
isFirefox,
isRateTest,
} from './common/constants';
import { sendMessage } from './common/bg';
import { getTabs, sendMessage } from './common/bg';
import { getOptions } from './options/store';
import { i18n, i18nMap } from './i18n';
import type { Req, Res } from './page/request';
Expand Down Expand Up @@ -118,7 +118,13 @@ browser.runtime.onMessage.addListener(async (msg: Message, sender) => {
browser.commands.onCommand.addListener((command) => {
switch (command) {
case 'toggle': {
return sendMessage({ type: Event.TOGGLE });
return getTabs().then(async (tabs) => {
const tab = tabs.find((e) => !!e.id);
if (!tab) return;
await browser.windows.update(tab.windowId!, { focused: true });
await browser.tabs.update(tab.id!, { active: true });
sendMessage({ type: Event.TOGGLE });
});
}
}
});
Expand Down
25 changes: 14 additions & 11 deletions src/common/bg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@ import browser from 'webextension-polyfill';

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

export function sendMessage<T>(tabId: number, msg: Message<T>): void;
export function sendMessage<T>(msg: Message<T>): void;
export function sendMessage<T>(tabIdOrMsg: number | Message<T>, msg?: Message<T>) {
export async function getTabs() {
return browser.tabs.query({
url: browser.runtime.getManifest().content_scripts![0].matches,
});
}

export async function sendMessage<T>(tabId: number, msg: Message<T>): Promise<void>;
export async function sendMessage<T>(msg: Message<T>): Promise<void>;
export async function sendMessage<T>(tabIdOrMsg: number | Message<T>, msg?: Message<T>) {
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 getTabs();
tabs.forEach((tab) => {
// Only the tab that open the lyrics will response
if (tab?.id) browser.tabs.sendMessage(tab.id, tabIdOrMsg);
});
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/options/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class OptionsApp extends GemElement<State> {

copyIdHandler = (e: KeyboardEvent) => {
const { options } = this.state;
if (e.key === 'c' && options) {
if (e.key.toLowerCase() === 'c' && options) {
navigator.clipboard.writeText(options.cid);
}
};
Expand Down
3 changes: 2 additions & 1 deletion src/page/btn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/page/netease.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/popup/elements/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit b45223f

Please sign in to comment.