Watch-on-LBRY/src/scripts/tabOnUpdated.ts
Kevin Raoofi 7954c29482 Watch-on-Lbry button
* Created a content script for YouTube that injects a styled button
* Automatically pause the video when redirecting to the app

The button location is rather finicky as certain polymer components
seem to move around, causing random DOM elements to appear all over the
place if it's not a "singleton" component.

squash
2020-10-28 05:21:49 -04:00

29 lines
1.2 KiB
TypeScript

import { appRedirectUrl } from '../common/lbry-url';
import { getSettingsAsync } from '../common/settings';
// handles lbry.tv -> lbry app redirect
chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, { url: tabUrl }) => {
const { enabled, redirect } = await getSettingsAsync('enabled', 'redirect');
if (!enabled || redirect !== 'app' || !changeInfo.url || !tabUrl?.startsWith('https://lbry.tv/')) return;
const url = appRedirectUrl(tabUrl, { encode: true });
if (!url) return;
chrome.tabs.update(tabId, { url });
alert('Opened link in LBRY App!'); // Better for UX since sometimes LBRY App doesn't take focus, if that is fixed, this can be removed
chrome.tabs.executeScript(tabId, {
code: `if (window.history.length === 1) {
window.close();
} else {
window.history.back();
}
document.querySelectorAll('video').forEach(v => v.pause());
`
});
});
// relay youtube link changes to the content script
chrome.tabs.onUpdated.addListener((tabId, changeInfo, { url }) => {
if (!changeInfo.url || !url ||
!(url.startsWith('https://www.youtube.com/watch?v=') || url.startsWith('https://www.youtube.com/channel/'))) return;
chrome.tabs.sendMessage(tabId, { url });
});