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.');
|
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) => {
|
const searchByName = query => new Promise(async (resolve, reject) => {
|
||||||
PeertubeAPI.searchVideo({ search: query })
|
PeertubeAPI.searchVideo({ search: query })
|
||||||
.then(function(data) {
|
.then(function(data) {
|
||||||
|
@ -68,13 +101,9 @@ const searchByName = query => new Promise(async (resolve, reject) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
const searchByID = id => new Promise(async (resolve, reject) => {
|
const searchByID = id => new Promise(async (resolve, reject) => {
|
||||||
const prefs = await Preferences.getPreferences();
|
|
||||||
|
|
||||||
PeertubeAPI.getVideo(id)
|
PeertubeAPI.getVideo(id)
|
||||||
.then(function(video) {
|
.then(video => resolve(video));
|
||||||
resolve({ url: getPeertubeVideoURL(video, prefs), video });
|
});
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
browser.runtime.onMessage.addListener(function(message, sender) {
|
browser.runtime.onMessage.addListener(function(message, sender) {
|
||||||
switch (message.kind) {
|
switch (message.kind) {
|
||||||
|
@ -90,3 +119,9 @@ browser.webRequest.onBeforeRequest.addListener(
|
||||||
{ urls: ['*://*.youtube.com/watch?v=*'] },
|
{ urls: ['*://*.youtube.com/watch?v=*'] },
|
||||||
['blocking']
|
['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) {
|
private static async fetchAPI(path: string, query?: Object) {
|
||||||
const instance = _.getOr(constants.peertubeAPI.defaultInstance, 'searchInstance', await browser.storage.local.get()).toString();
|
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) {
|
if (query) {
|
||||||
url = url + '?' + Object.keys(query).map(key => encodeURIComponent(key) + '=' + encodeURIComponent(query[key])).join('&');
|
url = url + '?' + Object.keys(query).map(key => encodeURIComponent(key) + '=' + encodeURIComponent(query[key])).join('&');
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
import * as _ from 'lodash/fp';
|
import * as _ from 'lodash/fp';
|
||||||
import * as browser from 'webextension-polyfill';
|
import * as browser from 'webextension-polyfill';
|
||||||
|
|
||||||
import { htmlToElement } from './util';
|
import { htmlToElement, getPeertubeVideoURL } from './util';
|
||||||
import { MessageKind, RedirectType } from './types';
|
import { MessageKind, RedirectType } from './types';
|
||||||
import Preferences from './preferences';
|
import Preferences from './preferences';
|
||||||
|
|
||||||
|
@ -46,20 +46,13 @@ async function peertubeify() {
|
||||||
switch (prefs.redirectPeertube) {
|
switch (prefs.redirectPeertube) {
|
||||||
case RedirectType.Show: {
|
case RedirectType.Show: {
|
||||||
searchVideo()
|
searchVideo()
|
||||||
.then(async ({ video, url }) => {
|
.then(async video => {
|
||||||
const link = videoLink(url, video);
|
const link = videoLink(getPeertubeVideoURL(video, prefs), video);
|
||||||
removeVideoLink();
|
removeVideoLink();
|
||||||
document.querySelector('body').appendChild(link);
|
document.querySelector('body').appendChild(link);
|
||||||
}).catch(removeVideoLink);
|
}).catch(removeVideoLink);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case RedirectType.Auto: {
|
|
||||||
searchVideo()
|
|
||||||
.then(async ({ video, url }) => {
|
|
||||||
location.replace(url);
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case RedirectType.None: {
|
case RedirectType.None: {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue