Merge pull request #25 from clay53/a-redirect

lbry.tv links now redirect to lbry app
This commit is contained in:
kodxana 2020-08-10 14:40:47 +02:00 committed by GitHub
commit 713105c7b2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 6 deletions

View file

@ -7,6 +7,7 @@
"https://invidio.us/channel/*", "https://invidio.us/channel/*",
"https://invidio.us/watch?v=*", "https://invidio.us/watch?v=*",
"https://api.lbry.com/*", "https://api.lbry.com/*",
"https://lbry.tv/*",
"tabs", "tabs",
"storage" "storage"
], ],
@ -21,12 +22,11 @@
"browser_action": { "browser_action": {
"default_title": "Watch on LBRY", "default_title": "Watch on LBRY",
"default_popup": "popup/popup.html" "default_popup": "popup/popup.html"
}, },
"web_accessible_resources": [ "web_accessible_resources": [
"popup.html", "popup.html",
"tools/YTtoLBRY.html" "tools/YTtoLBRY.html"
], ],
"icons": { "icons": {
"16": "icons/icon16.png", "16": "icons/icon16.png",
"48": "icons/icon48.png", "48": "icons/icon48.png",

View file

@ -2,6 +2,46 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
chrome.storage.local.get(async ({ enabled }) => { chrome.storage.local.get(async ({ enabled }) => {
if (!enabled) return; if (!enabled) return;
if (!changeInfo.url) return; if (!changeInfo.url) return;
if (tab.url.match(/\b(https:\/\/lbry.tv|lbry:\/\/)/g)) {
chrome.storage.local.get('redirect', ({redirect}) => {
var redirectTo;
// If handler can be added for new lbry:// tabs, below code can be uncommented and merged
// if (redirect === "lbry.tv") {
// console.error(tab.url);
// let isChannel = tab.url.match(/^lbry:\/\/@[^^$#@;/"<>%{}|^~[\]`]+?#([a-z0-9]{40}|[a-z0-9])$/g);
// let isClaim = tab.url.match(/^lbry:\/\/@[^^$#@;/"<>%{}|^~[\]`]+?#([a-z0-9]{40}|[a-z0-9])\/[^^$#@;/"<>%{}|^~[\]`]+?#([a-z0-9]{40}|[a-z0-9])$/g);
// if (isChannel) {
// let channelId = tab.url.match(/#[a-z0-9]+/g)[0].substr(1);
// redirectTo = `https://lbry.tv/${tab.url.match(/@[^^$#@;/"<>%{}|^~[\]`]+?(?=#)/g)[0]}${channelId.length > 1 ? '#' : ':'}${channelId}`;
// } else if (isClaim) {
// let channelId = tab.url.match(/#[a-z0-9]+/g)[0].substr(1);
// let claimId = tab.url.match(/[a-z0-9]+$/g)[0];
// redirectTo = `https://lbry.tv/${tab.url.match(/@[^^$#@;/"<>%{}|^~[\]`]+?(?=#)/g)[0]}${channelId.length > 1 ? '#' : ':'}${channelId}/${tab.url.match(/[^^$#@;/"<>%{}|^~[\]`]+(?=#[a-z0-9]+$)/g)}${claimId.length > 1 ? '#' : ':'}${claimId}`;
// }
// console.error(redirectTo);
// }
if (redirect === "app") {
let isChannel = tab.url.match(/^(https|http):\/\/lbry.tv\/@([^$#@;/"<>%{}|^~[\]`])+?(#([a-z0-9]{40})|:[a-z0-9])$/g);
let isClaim = tab.url.match(/^(https|http):\/\/lbry.tv\/@([^$#@;/"<>%{}|^~[\]`])+?(#([a-z0-9]{40})|:[a-z0-9])\/([^$#@;/"<>%{}|^~[\]`])+?(#([a-z0-9]{40})|:[a-z0-9])$/g);
if (isChannel) {
redirectTo = `lbry://${tab.url.match(/@([^$#@;/"<>%{}|^~[\]`])+?(?=[#:])/g)[0]}#${tab.url.match(/#([a-z0-9]{40})|:[a-z0-9]$/g)[0].substr(1)}`;
} else if (isClaim) {
redirectTo = `lbry://${tab.url.match(/@([^$#@;/"<>%{}|^~[\]`])+?(?=[#:])/g)}#${tab.url.match(/(#([a-z0-9]{40})|:[a-z0-9])(?=\/([^$#@;/"<>%{}|^~[\]`])+?(#([a-z0-9]{40})|:[a-z0-9])$)/g)[0].substr(1)}${tab.url.match(/\/([^$#@;/"<>%{}|^~[\]`])+?(?=[#:])/g)[0]}#${tab.url.match(/(#([a-z0-9]{40})|:[a-z0-9])$/g)[0].substr(1)}`;
}
}
if (redirectTo) {
chrome.tabs.update(tabId, { url: redirectTo + "?src=watch-on-lbry" });
if (redirect === "app") alert("Opened link in LBRY App!");
}
});
return;
}
const { id, type } = getId(tab.url); const { id, type } = getId(tab.url);
if (!id) return; if (!id) return;