From 9a4ebae5638c173a5c4704f870438cb3e8f284f4 Mon Sep 17 00:00:00 2001 From: Booteille Date: Sun, 17 Feb 2019 18:23:10 +0100 Subject: [PATCH 1/7] =?UTF-8?q?Ajoute=20la=20redirection=20automatique=20d?= =?UTF-8?q?es=20vid=C3=A9os=20embed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/background.ts | 19 ++++++++++--------- src/peertube.ts | 2 +- src/util.ts | 6 ++++-- src/youtube.ts | 2 +- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/background.ts b/src/background.ts index fd414b2..43ec586 100644 --- a/src/background.ts +++ b/src/background.ts @@ -18,7 +18,6 @@ import * as _ from 'lodash/fp'; import * as browser from 'webextension-polyfill'; import { MessageKind, RedirectType } from './types'; -import constants from './constants'; import Preferences from './preferences'; import * as InvidiousAPI from './invidious-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. */ const redirectYoutube = async (r) => { const prefs = await Preferences.getPreferences(); 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 url = getPeertubeVideoURL(video, prefs); + const url = await getPeertubeVideoURL(video, prefs, isEmbed); return { redirectUrl: url @@ -69,12 +69,13 @@ const redirectPeertube = async (r) => { if (prefs.redirectPeertube == RedirectType.Auto) { const id = _.last(_.split('/', r.url)); const video: any = await PeertubeAPI.getVideo(id); + const isEmbed = r.url.indexOf('embed') >= 0; if (prefs.openInOriginalInstance && video.account.host === hostname) { return {}; // Don't redirect if original instance } - const url = getPeertubeVideoURL(video, prefs); + const url = getPeertubeVideoURL(video, prefs, isEmbed); return { 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) { case MessageKind.SearchByName: return searchByName(message.query); @@ -108,12 +109,12 @@ browser.runtime.onMessage.addListener(function(message, sender) { browser.webRequest.onBeforeRequest.addListener( redirectYoutube, - { urls: ['*://*.youtube.com/watch?v=*'] }, + { urls: ['*://*.youtube.com/watch?v=*', '*://*.youtube.com/embed/*'] }, ['blocking'] ); browser.webRequest.onBeforeRequest.addListener( redirectPeertube, - { urls: ['*://*/videos/watch/*'] }, + { urls: ['*://*/videos/watch/*', '*://*/videos/embed/*'] }, ['blocking'] ); diff --git a/src/peertube.ts b/src/peertube.ts index 726bd2f..250fa04 100644 --- a/src/peertube.ts +++ b/src/peertube.ts @@ -48,7 +48,7 @@ async function peertubeify() { case RedirectType.Show: { searchVideo() .then(async video => { - const link = videoLink(getPeertubeVideoURL(video, prefs), video); + const link = videoLink(getPeertubeVideoURL(video, prefs, false), video); removeVideoLink(); document.querySelector('body').appendChild(link); }).catch(removeVideoLink); diff --git a/src/util.ts b/src/util.ts index 502d097..a669824 100644 --- a/src/util.ts +++ b/src/util.ts @@ -4,8 +4,10 @@ export function htmlToElement(html: string): Element { return template.content.firstElementChild; } -export function getPeertubeVideoURL(video, prefs) { - return `https://${getPeertubeHost(video.account.host, prefs)}/videos/watch/${video.uuid}` +export function getPeertubeVideoURL(video, prefs, isEmbed) { + const endpoint = isEmbed ? 'embed' : 'watch'; + + return `https://${getPeertubeHost(video.account.host, prefs)}/videos/${endpoint}/${video.uuid}`; } export function getPeertubeHost(host, prefs) { diff --git a/src/youtube.ts b/src/youtube.ts index 7fe2f57..9d42e1d 100644 --- a/src/youtube.ts +++ b/src/youtube.ts @@ -39,7 +39,7 @@ async function peertubeify(query: String) { case RedirectType.Show: { searchVideo(query) .then(async video => { - const url = getPeertubeVideoURL(video, prefs) + const url = getPeertubeVideoURL(video, prefs, false) const link = videoLink(url, video); removeVideoLink(); From 8eca34b755bf7b97a962b6a4c5b064276a342742 Mon Sep 17 00:00:00 2001 From: Booteille Date: Sun, 17 Feb 2019 20:07:50 +0100 Subject: [PATCH 2/7] Add support for Invidious --- extension/manifest.json | 2 +- src/background.ts | 7 ++---- src/youtube.ts | 49 +++++++++++++++++++++++++---------------- 3 files changed, 33 insertions(+), 25 deletions(-) diff --git a/extension/manifest.json b/extension/manifest.json index bf1df3c..d5d8cfe 100644 --- a/extension/manifest.json +++ b/extension/manifest.json @@ -20,7 +20,7 @@ "scripts": ["dist/background.js", "dist/vendors.js"] }, "content_scripts": [{ - "matches": ["*://*.youtube.com/*"], + "matches": ["*://*.youtube.com/*", "*://*.invidio.us/*"], "js": ["dist/youtube.js", "dist/vendors.js"] }, { "matches": ["https://*/videos/watch/*"], diff --git a/src/background.ts b/src/background.ts index 43ec586..e28b782 100644 --- a/src/background.ts +++ b/src/background.ts @@ -42,6 +42,7 @@ const redirectYoutube = async (r) => { const isEmbed = r.url.indexOf('embed') >= 0; 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 url = await getPeertubeVideoURL(video, prefs, isEmbed); @@ -50,8 +51,6 @@ const redirectYoutube = async (r) => { redirectUrl: url }; } - - throw new Error('No results.'); }; /** @@ -81,8 +80,6 @@ const redirectPeertube = async (r) => { redirectUrl: url }; } - - throw new Error('No results.'); }; const searchByName = query => new Promise(async (resolve, reject) => { @@ -109,7 +106,7 @@ browser.runtime.onMessage.addListener(function(message) { browser.webRequest.onBeforeRequest.addListener( redirectYoutube, - { urls: ['*://*.youtube.com/watch?v=*', '*://*.youtube.com/embed/*'] }, + { urls: ['*://*.youtube.com/watch?v=*', '*://*.youtube.com/embed/*', '*://*.invidio.us/watch?v=*', '*://*.invidio.us/embed/*'] }, ['blocking'] ); diff --git a/src/youtube.ts b/src/youtube.ts index 9d42e1d..1256881 100644 --- a/src/youtube.ts +++ b/src/youtube.ts @@ -22,7 +22,7 @@ import { MessageKind, RedirectType } from './types'; import Preferences from './preferences' const thumbnailURL = (host, path) => `https://${host}${path}`; - +const isYouTube = document.location.hostname === 'www.youtube.com'; const LINK_ID = 'peertube-link'; function searchVideo(query) { @@ -43,7 +43,9 @@ async function peertubeify(query: String) { const link = videoLink(url, video); removeVideoLink(); - document.querySelector('ytd-app').appendChild(link); + + const querySelector = isYouTube ? 'ytd-app' : '.pure-g'; + document.querySelector(querySelector).appendChild(link); }).catch(removeVideoLink); break; } @@ -53,23 +55,31 @@ async function peertubeify(query: String) { } } -const throttledPeertubeify = _.throttle(1000, peertubeify); -const observer = new MutationObserver(function(mutationsList) { - for (const mutation of mutationsList) { - if ((mutation.target as Element).classList.contains('ytp-title-link')) { - for (const node of mutation.addedNodes) { - if (node.nodeType == Node.TEXT_NODE) { - throttledPeertubeify(node.textContent); +if (isYouTube) { + const throttledPeertubeify = _.throttle(1000, peertubeify); + const observer = new MutationObserver(function(mutationsList) { + for (const mutation of mutationsList) { + if ((mutation.target as Element).classList.contains('ytp-title-link')) { + for (const node of mutation.addedNodes) { + if (node.nodeType == Node.TEXT_NODE) { + throttledPeertubeify(node.textContent); + } } } } - } -}); + }); -observer.observe(document.body, { - childList: true, - subtree: true, -}) + observer.observe(document.body, { + childList: true, + subtree: true, + }) +} else { + peertubeify(document.title.substring(0, document.title.indexOf(' - Invidious'))); +} + +const backgroundColor = isYouTube ? 'var(--yt-swatch-primary)' : '#fff'; +const buttonColor = isYouTube ? 'var(--yt-swatch-text)' : '#000'; +const textColor = isYouTube ? 'var(--yt-primary-text-color)' : '#000'; const videoLink = (url, video) => htmlToElement(`