From 46d3a85cc026e9436e10adec31fe6822e40f00d1 Mon Sep 17 00:00:00 2001 From: JellyBrick Date: Sat, 30 Sep 2023 08:43:10 +0900 Subject: [PATCH] fix: remove unnecessary `JSON.stringify` & `JSON.parse` --- plugins/downloader/back.ts | 7 ++++--- plugins/lyrics-genius/back.ts | 4 ++-- plugins/lyrics-genius/front.ts | 4 +++- plugins/sponsorblock/back.ts | 6 +++--- providers/song-info-front.ts | 6 +++--- providers/song-info.ts | 12 ++++++------ 6 files changed, 21 insertions(+), 18 deletions(-) diff --git a/plugins/downloader/back.ts b/plugins/downloader/back.ts index 83f27b8a..958fd6aa 100644 --- a/plugins/downloader/back.ts +++ b/plugins/downloader/back.ts @@ -29,7 +29,8 @@ import { isEnabled } from '../../config/plugins'; import { cleanupName, getImage, SongInfo } from '../../providers/song-info'; import { injectCSS } from '../utils'; import { cache } from '../../providers/decorators'; -import { GetPlayerResponse } from '../../types/get-player-response'; + +import type { GetPlayerResponse } from '../../types/get-player-response'; type CustomSongInfo = SongInfo & { trackId?: string }; @@ -78,8 +79,8 @@ export default async (win_: BrowserWindow) => { generate_session_locally: true, }); ipcMain.on('download-song', (_, url: string) => downloadSong(url)); - ipcMain.on('video-src-changed', (_, data: string) => { - playingUrl = (JSON.parse(data) as GetPlayerResponse).microformat.microformatDataRenderer.urlCanonical; + ipcMain.on('video-src-changed', (_, data: GetPlayerResponse) => { + playingUrl = data.microformat.microformatDataRenderer.urlCanonical; }); ipcMain.on('download-playlist-request', async (_event, url: string) => downloadPlaylist(url), diff --git a/plugins/lyrics-genius/back.ts b/plugins/lyrics-genius/back.ts index ce46aa3a..a4da0d3d 100644 --- a/plugins/lyrics-genius/back.ts +++ b/plugins/lyrics-genius/back.ts @@ -24,8 +24,8 @@ export default (win: BrowserWindow, options: LyricGeniusType) => { injectCSS(win.webContents, join(__dirname, 'style.css')); - ipcMain.handle('search-genius-lyrics', async (_, extractedSongInfo: string) => { - const metadata = JSON.parse(extractedSongInfo) as SongInfo; + ipcMain.handle('search-genius-lyrics', async (_, extractedSongInfo: SongInfo) => { + const metadata = extractedSongInfo; return await fetchFromGenius(metadata); }); }; diff --git a/plugins/lyrics-genius/front.ts b/plugins/lyrics-genius/front.ts index c87daf23..8e54ac94 100644 --- a/plugins/lyrics-genius/front.ts +++ b/plugins/lyrics-genius/front.ts @@ -1,8 +1,10 @@ import { ipcRenderer } from 'electron'; import is from 'electron-is'; +import type { SongInfo } from '../../providers/song-info'; + export default () => { - ipcRenderer.on('update-song-info', (_, extractedSongInfo: string) => setTimeout(async () => { + ipcRenderer.on('update-song-info', (_, extractedSongInfo: SongInfo) => setTimeout(async () => { const tabList = document.querySelectorAll('tp-yt-paper-tab'); const tabs = { upNext: tabList[0], diff --git a/plugins/sponsorblock/back.ts b/plugins/sponsorblock/back.ts index 1a95c776..cd3e49d0 100644 --- a/plugins/sponsorblock/back.ts +++ b/plugins/sponsorblock/back.ts @@ -6,8 +6,8 @@ import { sortSegments } from './segments'; import { SkipSegment } from './types'; import defaultConfig from '../../config/defaults'; -import { GetPlayerResponse } from '../../types/get-player-response'; +import type { GetPlayerResponse } from '../../types/get-player-response'; import type { ConfigType } from '../../config/dynamic'; let videoID: string; @@ -18,8 +18,8 @@ export default (win: BrowserWindow, options: ConfigType<'sponsorblock'>) => { ...options, }; - ipcMain.on('video-src-changed', async (_, data: string) => { - videoID = (JSON.parse(data) as GetPlayerResponse)?.videoDetails?.videoId; + ipcMain.on('video-src-changed', async (_, data: GetPlayerResponse) => { + videoID = data?.videoDetails?.videoId; const segments = await fetchSegments(apiURL, categories); win.webContents.send('sponsorblock-skip', segments); }); diff --git a/providers/song-info-front.ts b/providers/song-info-front.ts index 0848a9b9..f5607869 100644 --- a/providers/song-info-front.ts +++ b/providers/song-info-front.ts @@ -12,8 +12,8 @@ export const getSongInfo = () => songInfo; const $ = (s: string): E | null => document.querySelector(s); const $$ = (s: string): NodeListOf => document.querySelectorAll(s); -ipcRenderer.on('update-song-info', async (_, extractedSongInfo: string) => { - songInfo = JSON.parse(extractedSongInfo) as SongInfo; +ipcRenderer.on('update-song-info', async (_, extractedSongInfo: SongInfo) => { + songInfo = extractedSongInfo; if (songInfo.imageSrc) songInfo.image = await getImage(songInfo.imageSrc); }); @@ -138,7 +138,7 @@ export default () => { data.videoDetails.elapsedSeconds = 0; data.videoDetails.isPaused = false; - ipcRenderer.send('video-src-changed', JSON.stringify(data)); + ipcRenderer.send('video-src-changed', data); } }, { once: true, passive: true }); }; diff --git a/providers/song-info.ts b/providers/song-info.ts index 4ffc1110..83cfa6ca 100644 --- a/providers/song-info.ts +++ b/providers/song-info.ts @@ -3,7 +3,8 @@ import { BrowserWindow, ipcMain, nativeImage, net } from 'electron'; import { cache } from './decorators'; import config from '../config'; -import { GetPlayerResponse } from '../types/get-player-response'; + +import type { GetPlayerResponse } from '../types/get-player-response'; export interface SongInfo { title: string; @@ -53,8 +54,7 @@ export const getImage = cache( }, ); -const handleData = async (responseText: string, win: Electron.BrowserWindow) => { - const data = JSON.parse(responseText) as GetPlayerResponse; +const handleData = async (data: GetPlayerResponse, win: Electron.BrowserWindow) => { if (!data) { return; } @@ -83,7 +83,7 @@ const handleData = async (responseText: string, win: Electron.BrowserWindow) => songInfo.imageSrc = thumbnails.at(-1)?.url.split('?')[0]; if (songInfo.imageSrc) songInfo.image = await getImage(songInfo.imageSrc); - win.webContents.send('update-song-info', JSON.stringify(songInfo)); + win.webContents.send('update-song-info', songInfo); } }; @@ -100,9 +100,9 @@ let handlingData = false; const registerProvider = (win: BrowserWindow) => { // This will be called when the song-info-front finds a new request with song data - ipcMain.on('video-src-changed', async (_, responseText: string) => { + ipcMain.on('video-src-changed', async (_, data: GetPlayerResponse) => { handlingData = true; - await handleData(responseText, win); + await handleData(data, win); handlingData = false; for (const c of callbacks) { c(songInfo, 'video-src-changed');