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 29, 2024
1 parent a4b43ef commit e5cde79
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 17 deletions.
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 getTags() {
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 getTags();
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 e5cde79

Please sign in to comment.