diff --git a/manifest.json b/manifest.json index a176a27..f8b576e 100644 --- a/manifest.json +++ b/manifest.json @@ -7,6 +7,7 @@ "https://invidio.us/channel/*", "https://invidio.us/watch?v=*", "https://api.lbry.com/*", + "https://lbry.tv/*", "tabs", "storage" ], @@ -21,12 +22,11 @@ "browser_action": { "default_title": "Watch on LBRY", "default_popup": "popup/popup.html" -}, - "web_accessible_resources": [ - "popup.html", - "tools/YTtoLBRY.html" - ], - + }, + "web_accessible_resources": [ + "popup.html", + "tools/YTtoLBRY.html" + ], "icons": { "16": "icons/icon16.png", "48": "icons/icon48.png", diff --git a/scripts/tabOnUpdated.js b/scripts/tabOnUpdated.js index 50b3630..bbfddf4 100644 --- a/scripts/tabOnUpdated.js +++ b/scripts/tabOnUpdated.js @@ -2,6 +2,46 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => { chrome.storage.local.get(async ({ enabled }) => { if (!enabled) 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); if (!id) return;