Refactor Automatic Peertube redirection to redirect before loading the page
This commit is contained in:
parent
5c35ce031d
commit
2614a3fc11
|
@ -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']
|
||||
);
|
||||
|
|
|
@ -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('&');
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue