diff --git a/src/background.ts b/src/background.ts index d939e4a..2fc52e0 100644 --- a/src/background.ts +++ b/src/background.ts @@ -17,7 +17,7 @@ import * as _ from 'lodash/fp'; import * as browser from 'webextension-polyfill'; -import { MessageKind } from './types'; +import { MessageKind, RedirectType } from './types'; import constants from './constants'; import Preferences from './preferences'; import InvidiousAPI from './invidious-api'; @@ -27,7 +27,7 @@ import { getPeertubeVideoURL } from './util'; * Retrieve the title of the video using Invidious API. */ const getTitle = async (id: string) => { - let data = await InvidiousAPI.getVideo(id); + const data = await InvidiousAPI.getVideo(id); return data.title; } @@ -38,19 +38,19 @@ const getTitle = async (id: string) => { const preventYoutube = async (r) => { const prefs = await Preferences.getPreferences(); - if (prefs.redirectYoutube === 'Auto') { + if (prefs.redirectYoutube == RedirectType.Auto) { const query = new URLSearchParams(r.url.substring(r.url.indexOf('?') + 1)); - let title = await getTitle(query.get('v')); - let video = await searchByName(title); - let url = getPeertubeVideoURL(video, prefs); + const title = await getTitle(query.get('v')); + const video = await searchByName(title); + const url = getPeertubeVideoURL(video, prefs); return { redirectUrl: url }; } - return {}; + throw new Error('No results.'); }; const buildSearchByNameURL = (instance: string, query: string): string => `https://${instance}/api/v1/search/videos?search=${encodeURIComponent(query)}`; diff --git a/src/invidious-api.ts b/src/invidious-api.ts index 9049d07..c9a482b 100644 --- a/src/invidious-api.ts +++ b/src/invidious-api.ts @@ -3,7 +3,7 @@ import constants from './constants'; export default class InvidiousAPI { static async _fetchAPI(action: string, params: any) { - let paramString = typeof params == 'string' + const paramString = typeof params == 'string' ? params : Object.keys(params).map(function(key) { return encodeURIComponent(key) + '=' + encodeURIComponent(params[key]); @@ -16,6 +16,7 @@ export default class InvidiousAPI { + e.message )); } + static async getVideo(id: string) { return this._fetchAPI(constants.invidiousAPI.videos, id); }