Ajoute la redirection automatique des vidéos embed

This commit is contained in:
Booteille 2019-02-17 18:23:10 +01:00
parent 8c24a4383f
commit 9a4ebae563
No known key found for this signature in database
GPG Key ID: 7FC1ED300B74CD91
4 changed files with 16 additions and 13 deletions

View File

@ -18,7 +18,6 @@ import * as _ from 'lodash/fp';
import * as browser from 'webextension-polyfill'; import * as browser from 'webextension-polyfill';
import { MessageKind, RedirectType } from './types'; import { MessageKind, RedirectType } from './types';
import constants from './constants';
import Preferences from './preferences'; import Preferences from './preferences';
import * as InvidiousAPI from './invidious-api'; import * as InvidiousAPI from './invidious-api';
import * as PeertubeAPI from './peertube-api'; import * as PeertubeAPI from './peertube-api';
@ -33,18 +32,19 @@ const getTitle = async (id: string) => {
} }
/** /**
* Redirect Youtube without loading it if the video is found and preferences lAHhDKzReKBQKj3R * Redirect Youtube without loading it if the video is found and preferences
* set on automatic redirection. * set on automatic redirection.
*/ */
const redirectYoutube = async (r) => { const redirectYoutube = async (r) => {
const prefs = await Preferences.getPreferences(); const prefs = await Preferences.getPreferences();
if (prefs.redirectYoutube == RedirectType.Auto) { if (prefs.redirectYoutube == RedirectType.Auto) {
const query = new URLSearchParams(r.url.substring(r.url.indexOf('?') + 1)); const isEmbed = r.url.indexOf('embed') >= 0;
const title = await getTitle(query.get('v')); const query = isEmbed ? r.url.substr(r.url.lastIndexOf('/') + 1, 11) : r.url.substr(r.url.indexOf('?v=') + 3, 11);
const title = await getTitle(query);
const video = await searchByName(title); const video = await searchByName(title);
const url = getPeertubeVideoURL(video, prefs); const url = await getPeertubeVideoURL(video, prefs, isEmbed);
return { return {
redirectUrl: url redirectUrl: url
@ -69,12 +69,13 @@ const redirectPeertube = async (r) => {
if (prefs.redirectPeertube == RedirectType.Auto) { if (prefs.redirectPeertube == RedirectType.Auto) {
const id = _.last(_.split('/', r.url)); const id = _.last(_.split('/', r.url));
const video: any = await PeertubeAPI.getVideo(id); const video: any = await PeertubeAPI.getVideo(id);
const isEmbed = r.url.indexOf('embed') >= 0;
if (prefs.openInOriginalInstance && video.account.host === hostname) { if (prefs.openInOriginalInstance && video.account.host === hostname) {
return {}; // Don't redirect if original instance return {}; // Don't redirect if original instance
} }
const url = getPeertubeVideoURL(video, prefs); const url = getPeertubeVideoURL(video, prefs, isEmbed);
return { return {
redirectUrl: url redirectUrl: url
@ -97,7 +98,7 @@ const searchByName = query => new Promise(async (resolve, reject) => {
}); });
}); });
browser.runtime.onMessage.addListener(function(message, sender) { browser.runtime.onMessage.addListener(function(message) {
switch (message.kind) { switch (message.kind) {
case MessageKind.SearchByName: case MessageKind.SearchByName:
return searchByName(message.query); return searchByName(message.query);
@ -108,12 +109,12 @@ browser.runtime.onMessage.addListener(function(message, sender) {
browser.webRequest.onBeforeRequest.addListener( browser.webRequest.onBeforeRequest.addListener(
redirectYoutube, redirectYoutube,
{ urls: ['*://*.youtube.com/watch?v=*'] }, { urls: ['*://*.youtube.com/watch?v=*', '*://*.youtube.com/embed/*'] },
['blocking'] ['blocking']
); );
browser.webRequest.onBeforeRequest.addListener( browser.webRequest.onBeforeRequest.addListener(
redirectPeertube, redirectPeertube,
{ urls: ['*://*/videos/watch/*'] }, { urls: ['*://*/videos/watch/*', '*://*/videos/embed/*'] },
['blocking'] ['blocking']
); );

View File

@ -48,7 +48,7 @@ async function peertubeify() {
case RedirectType.Show: { case RedirectType.Show: {
searchVideo() searchVideo()
.then(async video => { .then(async video => {
const link = videoLink(getPeertubeVideoURL(video, prefs), video); const link = videoLink(getPeertubeVideoURL(video, prefs, false), video);
removeVideoLink(); removeVideoLink();
document.querySelector('body').appendChild(link); document.querySelector('body').appendChild(link);
}).catch(removeVideoLink); }).catch(removeVideoLink);

View File

@ -4,8 +4,10 @@ export function htmlToElement(html: string): Element {
return template.content.firstElementChild; return template.content.firstElementChild;
} }
export function getPeertubeVideoURL(video, prefs) { export function getPeertubeVideoURL(video, prefs, isEmbed) {
return `https://${getPeertubeHost(video.account.host, prefs)}/videos/watch/${video.uuid}` const endpoint = isEmbed ? 'embed' : 'watch';
return `https://${getPeertubeHost(video.account.host, prefs)}/videos/${endpoint}/${video.uuid}`;
} }
export function getPeertubeHost(host, prefs) { export function getPeertubeHost(host, prefs) {

View File

@ -39,7 +39,7 @@ async function peertubeify(query: String) {
case RedirectType.Show: { case RedirectType.Show: {
searchVideo(query) searchVideo(query)
.then(async video => { .then(async video => {
const url = getPeertubeVideoURL(video, prefs) const url = getPeertubeVideoURL(video, prefs, false)
const link = videoLink(url, video); const link = videoLink(url, video);
removeVideoLink(); removeVideoLink();