Skip to content

Commit

Permalink
Close #83
Browse files Browse the repository at this point in the history
  • Loading branch information
mantou132 committed May 19, 2024
1 parent 92c7e55 commit 7f035fe
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 70 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "spotify-lyrics",
"version": "1.6.2",
"version": "1.6.3",
"description": "Desktop Spotify Web Player Instant Synchronized Lyrics",
"scripts": {
"lint": "tsc --noEmit && eslint --ext .ts --fix src/",
Expand Down
2 changes: 1 addition & 1 deletion public/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://raw.githubusercontent.com/extend-chrome/manifest-json-schema/main/schema/manifest.schema.json",
"name": "__MSG_extensionName__",
"version": "1.6.2",
"version": "1.6.3",
"manifest_version": 3,
"description": "__MSG_extensionDescription__",
"default_locale": "en",
Expand Down
38 changes: 21 additions & 17 deletions src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,18 @@ function enableBrowserAction() {
disableBrowserAction();

browser.runtime.onMessage.addListener(async (msg: Message, sender) => {
const tabId = sender.tab?.id;
const { type, data } = msg || {};
switch (type) {
case Event.GET_OPTIONS: {
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/onMessage#Parameters
return getOptions().then((options) => ({
...options,
i18nMap,
}));
const options = await getOptions();
if (!tabId) return;

sendMessage(tabId, {
type: Event.SEND_OPTIONS,
data: { ...options, i18nMap },
});
}
case Event.POPUP_ACTIVE: {
if (data === true) {
Expand All @@ -85,7 +89,6 @@ browser.runtime.onMessage.addListener(async (msg: Message, sender) => {
}
case Event.SEND_REQUEST: {
const { reqId, uri, options } = data as Req;
const tabId = sender.tab?.id;
if (!tabId) return;

const sendRes = (data: Omit<Res, 'reqId'>) => {
Expand Down Expand Up @@ -119,8 +122,6 @@ browser.commands.onCommand.addListener((command) => {
}
});

browser.runtime.setUninstallURL('https://forms.gle/bUWyEqfSTCU9NEwEA');

browser.contextMenus.create({
id: ContextItems.WELCOME,
title: i18n.menusWelcome(),
Expand All @@ -133,22 +134,21 @@ browser.contextMenus.create({
contexts: ['action'],
});

const storeLinkMap: Record<string, string> = {
const storeLinkMap = {
'{d5bcc68d-856a-41e2-8021-d4c51f3b8e4a}':
'https://addons.mozilla.org/en-US/firefox/addon/spotify-lyrics/',
'{9bbdd06f-4fe2-4d05-8c2d-1ef6bf71f84d}': 'https://github.com/mantou132/Spotify-Lyrics',
mkjfooclbdgjdclepjeepbmmjaclipod:
'https://chrome.google.com/webstore/detail/spotify-lyrics/mkjfooclbdgjdclepjeepbmmjaclipod/reviews',
aiehldpoaeaidnljjimhbojpblkbembm:
'https://microsoftedge.microsoft.com/addons/detail/spotify-lyrics/aiehldpoaeaidnljjimhbojpblkbembm',
github: 'https://github.com/mantou132/Spotify-Lyrics',
};
if (storeLinkMap[browser.runtime.id]) {
browser.contextMenus.create({
id: ContextItems.RATE_ME,
title: i18n.menusRateMe(),
contexts: ['action'],
});
}

browser.contextMenus.create({
id: ContextItems.RATE_ME,
title: i18n.menusRateMe(),
contexts: ['action'],
});

const openPage = async (url: string) => {
const { windowId } = await browser.tabs.create({ url });
Expand All @@ -164,11 +164,15 @@ browser.contextMenus.onClicked.addListener(async function (info) {
openPage('https://github.com/mantou132/Spotify-Lyrics/issues');
break;
case ContextItems.RATE_ME:
openPage(storeLinkMap[browser.runtime.id]);
openPage(
storeLinkMap[browser.runtime.id as keyof typeof storeLinkMap] || storeLinkMap.github,
);
break;
}
});

browser.runtime.setUninstallURL('https://forms.gle/bUWyEqfSTCU9NEwEA');

browser.runtime.onInstalled.addListener(({ reason }) => {
if (reason === 'install') {
openPage(browser.runtime.getURL('welcome.html'));
Expand Down
50 changes: 25 additions & 25 deletions src/common/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Value } from '../options/elements/switch';
import type { SwitchValue } from '../options/elements/switch';

// Read package.json `version` field
export const VERSION = process.env.VERSION || 'UNKNOWN';
Expand All @@ -14,25 +14,25 @@ export interface Message<T = any> {
}

export enum Event {
GET_SONGS = 'get-songs',
SEND_SONGS = 'send-songs',
SELECT_SONG = 'select-song',
CONFIRMED_SONG = 'confirmed-song',
GET_OPTIONS = 'get-options',
SEND_OPTIONS = 'send-options',
OPEN_OPTIONS = 'open-options',
POPUP_ACTIVE = 'popup-active',
CAPTURE_EXCEPTION = 'capture-exception',
SEND_REQUEST = 'send-request',
SEND_RESPONSE = 'send-response',
TOGGLE = 'toggle',
GET_SONGS = 100000,
SEND_SONGS,
SELECT_SONG,
CONFIRMED_SONG,
GET_OPTIONS,
SEND_OPTIONS,
OPEN_OPTIONS,
POPUP_ACTIVE,
CAPTURE_EXCEPTION,
SEND_REQUEST,
SEND_RESPONSE,
TOGGLE,
}

export const ContextItems = {
FEEDBACK: 'feedback',
RATE_ME: 'rate-me',
WELCOME: 'welcome',
};
export enum ContextItems {
FEEDBACK = 'feedback',
RATE_ME = 'rate-me',
WELCOME = 'welcome',
}

export const LyricsPositions = ['page', 'pip'] as const;
export const LyricsAlign = ['left', 'center'] as const;
Expand All @@ -42,16 +42,16 @@ export interface Options {
'font-size': string;
'font-family': (typeof LyricsFontFamily)[number] | string;
'toggle-shortcut': string;
'only-cover': Value;
'clean-lyrics': Value;
'hd-cover': Value;
'use-unreviewed-lyrics': Value;
'only-cover': SwitchValue;
'clean-lyrics': SwitchValue;
'hd-cover': SwitchValue;
'use-unreviewed-lyrics': SwitchValue;
'show-on': (typeof LyricsPositions)[number];
'lyrics-align': (typeof LyricsAlign)[number];
'traditional-chinese-lyrics': Value;
'traditional-chinese-lyrics': SwitchValue;
// Deprecated
'lyrics-smooth-scroll'?: Value;
'strict-mode'?: Value;
'lyrics-smooth-scroll'?: SwitchValue;
'strict-mode'?: SwitchValue;
}

export type Platform = 'SPOTIFY' | 'YOUTUBE' | 'DEEZER' | 'TIDAL' | 'APPLE';
23 changes: 4 additions & 19 deletions src/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,10 @@ runtime.onMessage.addListener((msg: Message) => {

window.addEventListener('message', ({ data }) => {
const { type } = data || {};
switch (type) {
case Event.GET_OPTIONS: {
return runtime
.sendMessage(data)
.then((options) => {
window.postMessage({ type: Event.SEND_OPTIONS, data: options }, '*');
})
.catch(() => {
//
});
}
case Event.SEND_REQUEST:
case Event.POPUP_ACTIVE:
case Event.CAPTURE_EXCEPTION:
case Event.SEND_SONGS: {
return runtime.sendMessage(data).catch(() => {
//
});
}
if (type in Event) {
runtime.sendMessage(data).catch(() => {
//
});
}
});

Expand Down
6 changes: 3 additions & 3 deletions src/options/elements/switch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import { customElement, attribute, refobject, RefObject } from '@mantou/gem/lib/

import { theme } from '../../common/theme';

export type Value = 'off' | 'on';
export type SwitchValue = 'off' | 'on';

@customElement('ele-switch')
export class Switch extends GemElement {
@attribute name: string;
@attribute defaultValue: Value;
@attribute defaultValue: SwitchValue;

@refobject checkboxRef: RefObject<HTMLInputElement>;

get control() {
return this.checkboxRef.element!;
}
get value(): Value {
get value(): SwitchValue {
return this.control.checked ? 'on' : 'off';
}

Expand Down
6 changes: 4 additions & 2 deletions src/page/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ const setPopupState = (active: boolean) => {
};

lyricVideo.addEventListener('enterpictureinpicture', () => {
setPopupState(true);
lyricVideoIsOpen = true;
});
lyricVideo.addEventListener('leavepictureinpicture', () => {
setPopupState(false);
lyricVideoIsOpen = false;
});

// service worker active will disabled browser action
setInterval(() => setPopupState(lyricVideoIsOpen), 1000);

// When the lyrics are displayed on the page
window.addEventListener('beforeunload', () => {
if (lyricVideoIsOpen) setPopupState(false);
Expand Down
6 changes: 4 additions & 2 deletions src/page/lyrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export interface Song {
name: string;
artists: Artist[];
album: Album;
duration?: number; // ms
/**ms */
duration?: number;
}

interface SearchSongsResult {
Expand Down Expand Up @@ -91,6 +92,7 @@ const simplifiedText = (s: string) => {
const removeSongFeat = (s: string) => {
return (
s
.replace(/-\s+\d*\s*remaster(ed)?\s*\d*/, '')
.replace(/-\s+(feat|with).*/i, '')
.replace(/(\(|\[)(feat|with)\.?\s+.*(\)|\])$/i, '')
.trim() || s
Expand Down Expand Up @@ -410,7 +412,7 @@ export function parseLyrics(lyricStr: string, options: ParseLyricsOptions = {})
const otherInfoKeys = [
'作?\\s*词|作?\\s*曲|编\\s*曲?|监\\s*制?',
'.*编写|.*和音|.*和声|.*合声|.*提琴|.*录|.*工程|.*工作室|.*设计|.*剪辑|.*制作|.*发行|.*出品|.*后期|.*混音|.*缩混',
'原唱|翻唱|题字|文案|海报|古筝|二胡|钢琴|吉他|贝斯|笛子|鼓|弦乐',
'母带|原唱|翻唱|题字|文案|海报|古筝|二胡|钢琴|吉他|贝斯|笛子|鼓|弦乐',
'lrc|publish|vocal|guitar|program|produce|write',
];
const otherInfoRegexp = new RegExp(`^(${otherInfoKeys.join('|')}).*(:|:)`, 'i');
Expand Down

0 comments on commit 7f035fe

Please sign in to comment.