Watch-on-LBRY/scripts/tabOnUpdated.js
Yamboy1 464150afac See commit desc.
Big changes here.
- I've moved all of the scripts to individual
files in the `scripts` folder.
- I've updated all the code to ES7.
- The extension stores the state in browser
storage now, and it persists.
- The extension has a default state of on.
2020-01-11 23:07:55 +13:00

36 lines
1.2 KiB
JavaScript

chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
if (!tab.url) return;
const { id, type } = getId(tab.url);
if (!id) return;
const url = `https://cors-anywhere.herokuapp.com/https://api.lbry.com/yt/resolve?${type}_ids=${id}`;
const response = await fetch(url, { headers: { 'Content-Type': 'application/json' } });
const json = await response.json();
console.log(json);
const title = json.data[`${type}s`][id];
if (!title) return;
let newUrl = `https://lbry.tv/${title.replace(/^lbry:\/\//, "").replace(/#/g, ":")}`;
chrome.tabs.update(tab.tabId, { url: newUrl });
});
function getId(url) {
const videoId = getVideoId(url);
if (videoId) return { id: videoId, type: "video" };
const channelId = getChannelId(url);
if (channelId) return { id: channelId, type: "channel" };
return {}; // Equivalent of returning null
}
function getVideoId(url) {
const regex = /watch\/?\?.*v=([^\s&]*)/;
const match = url.match(regex);
return match ? match[1] : null; // match[1] is the videoId
}
function getChannelId(url) {
const regex = /channel\/([^\s?]*)/;
const match = url.match(regex);
return match ? match[1] : null; // match[1] is the channelId
}