From 2614a3fc11a30ccaa0bf433e38234f6668544632 Mon Sep 17 00:00:00 2001 From: Booteille Date: Mon, 12 Nov 2018 23:05:06 +0100 Subject: [PATCH] Refactor Automatic Peertube redirection to redirect before loading the page --- src/background.ts | 47 +++++++++++++++++++++++++++++++++++++++------ src/peertube-api.ts | 2 +- src/peertube.ts | 13 +++---------- 3 files changed, 45 insertions(+), 17 deletions(-) diff --git a/src/background.ts b/src/background.ts index 04e07c8..c8f5bbd 100644 --- a/src/background.ts +++ b/src/background.ts @@ -54,6 +54,39 @@ const redirectYoutube = async (r) => { throw new Error('No results.'); }; +/** + * Redirect to preferred PeerTube Instance when trying to watch a PeerTube + * video. + */ +const redirectPeertube = async (r) => { + const prefs = await Preferences.getPreferences(); + const hostname = new URL(r.url).hostname; + + if (prefs.openInOriginalInstance === false && prefs.searchInstance === hostname) { + return {}; // Don't redirect if good instance + } + + if (prefs.redirectPeertube == RedirectType.Auto) { + const id = _.last(_.split('/', r.url)); + const video = await searchByID(id); + const getHost = video => new Promise(async (resolve, reject) => { + resolve(video.account.host); + }); + + if (prefs.openInOriginalInstance && await getHost(video) === hostname) { + return {}; // Don't redirect if original instance + } + + const url = getPeertubeVideoURL(video, prefs); + + return { + redirectUrl: url + }; + } + + throw new Error('No results.'); +}; + const searchByName = query => new Promise(async (resolve, reject) => { PeertubeAPI.searchVideo({ search: query }) .then(function(data) { @@ -68,13 +101,9 @@ const searchByName = query => new Promise(async (resolve, reject) => { }); const searchByID = id => new Promise(async (resolve, reject) => { - const prefs = await Preferences.getPreferences(); - PeertubeAPI.getVideo(id) - .then(function(video) { - resolve({ url: getPeertubeVideoURL(video, prefs), video }); - }) -}) + .then(video => resolve(video)); +}); browser.runtime.onMessage.addListener(function(message, sender) { switch (message.kind) { @@ -90,3 +119,9 @@ browser.webRequest.onBeforeRequest.addListener( { urls: ['*://*.youtube.com/watch?v=*'] }, ['blocking'] ); + +browser.webRequest.onBeforeRequest.addListener( + redirectPeertube, + { urls: ['*://*/videos/watch/*'] }, + ['blocking'] +); diff --git a/src/peertube-api.ts b/src/peertube-api.ts index 412abcd..2666e60 100644 --- a/src/peertube-api.ts +++ b/src/peertube-api.ts @@ -5,7 +5,7 @@ export default class PeertubeAPI { private static async fetchAPI(path: string, query?: Object) { const instance = _.getOr(constants.peertubeAPI.defaultInstance, 'searchInstance', await browser.storage.local.get()).toString(); - let url = `https://${constants.peertubeAPI.defaultInstance}/${constants.peertubeAPI.endpoint}/${path}`; + let url = `https://${instance}/${constants.peertubeAPI.endpoint}/${path}`; if (query) { url = url + '?' + Object.keys(query).map(key => encodeURIComponent(key) + '=' + encodeURIComponent(query[key])).join('&'); diff --git a/src/peertube.ts b/src/peertube.ts index f275fcc..76db0d9 100644 --- a/src/peertube.ts +++ b/src/peertube.ts @@ -17,7 +17,7 @@ import * as _ from 'lodash/fp'; import * as browser from 'webextension-polyfill'; -import { htmlToElement } from './util'; +import { htmlToElement, getPeertubeVideoURL } from './util'; import { MessageKind, RedirectType } from './types'; import Preferences from './preferences'; @@ -46,20 +46,13 @@ async function peertubeify() { switch (prefs.redirectPeertube) { case RedirectType.Show: { searchVideo() - .then(async ({ video, url }) => { - const link = videoLink(url, video); + .then(async video => { + const link = videoLink(getPeertubeVideoURL(video, prefs), video); removeVideoLink(); document.querySelector('body').appendChild(link); }).catch(removeVideoLink); break; } - case RedirectType.Auto: { - searchVideo() - .then(async ({ video, url }) => { - location.replace(url); - }); - break; - } case RedirectType.None: { break; }