diff --git a/background.js b/background.js deleted file mode 100644 index c343b50..0000000 --- a/background.js +++ /dev/null @@ -1,100 +0,0 @@ -var loaded = false; -var enable = false; - -chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { - if (changeInfo.status == 'complete' && tab.status == 'complete' && tab.url != undefined && enable) { - if(tab.url.includes('watch?') && !loaded) { - loaded = true - let videoId = getVideoId(tab.url) - validateVideo(videoId) - } - - if (tab.url.includes('/channel/') && !loaded) { - loaded = true - //przypisujemy do zmiennej id kanaƂu - let channelId = getChannelId(tab.url) - getInformationFromApi(channelId) - } - - } -}); - -function getChannelId(url) { - //pop obcina ostatni element z tablicy (zwraca ostatni element) - // to zwracam - // https://www.youtube.com/channel / UC-vYDJQiabg9BK8XTDPFspg - return url.split("/").pop() -} - -function getInformationFromApi(id) { - loaded = false - let url = 'https://cors-anywhere.herokuapp.com/https://api.lbry.com/yt/resolve?channel_ids=' + id - console.log('Calling url: ' + url) - - var request = new Request( - url, - { - method: 'GET', - headers: new Headers({ 'Content-Type': 'application/json' }) - } - ) - - fetch(request) - .then(function(resp) { - return resp.json(); - }) - .then(function(data) { - let channel = data.data.channels[id] - if (channel != null) { - let url = "https://lbry.tv/" + channel - chrome.tabs.update({url: url}); - } - }); -} - -function getVideoId(videoUrl) { - var videoId = videoUrl.split('v=')[1] - var ampersandPosition = videoId.indexOf('&') - if(ampersandPosition != -1) { - videoId = videoId.substring(0, ampersandPosition) - } - return videoId -} - - -function validateVideo (id) { - loaded = false - let url = 'https://cors-anywhere.herokuapp.com/https://api.lbry.com/yt/resolve?video_ids=' + id - console.log('Calling url: ' + url) - - var request = new Request( - url, - { - method: 'GET', - headers: new Headers({ 'Content-Type': 'application/json' }) - } - ) - - fetch(request) - .then(function(resp) { - return resp.json(); - }) - .then(function(data) { - let title = data.data.videos[id] - - if (title != null) { - let url = "https://lbry.tv/" + title.replace(/^lbry:\/\//, "").replace(/#/g, ":"); - chrome.tabs.update({url: url}); - } - }); - -} - -chrome.browserAction.onClicked.addListener(function (tab) { - enable = enable ? false : true; - if(enable){ - chrome.browserAction.setBadgeText({ text: 'ON' }); - }else{ - chrome.browserAction.setBadgeText({ text: 'OFF' }); - } -}); diff --git a/manifest.json b/manifest.json index de13e88..c88d5aa 100644 --- a/manifest.json +++ b/manifest.json @@ -4,18 +4,25 @@ "permissions": [ "https://www.youtube.com/watch?v=*", "https://www.youtube.com/channel/*", - "tabs" + "tabs", + "storage" ], "background": { - "scripts": ["background.js"], + "scripts": [ + "scripts/browserActionOnClicked.js", + "scripts/runtimeOnInstalled.js", + "scripts/storageOnChanged.js", + "scripts/tabOnUpdated.js" + ], "persistent": false }, "browser_action": { "default_title": "Watch on LBRY" }, - "icons": { "16": "icons/icon16.png", - "48": "icons/icon48.png", - "128": "icons/icon128.png" - }, + "icons": { + "16": "icons/icon16.png", + "48": "icons/icon48.png", + "128": "icons/icon128.png" + }, "manifest_version": 2 } diff --git a/scripts/browserActionOnClicked.js b/scripts/browserActionOnClicked.js new file mode 100644 index 0000000..aa5e2b5 --- /dev/null +++ b/scripts/browserActionOnClicked.js @@ -0,0 +1,5 @@ +chrome.browserAction.onClicked.addListener(() => { + chrome.storage.local.get('enabled', ({ enabled }) => { + chrome.storage.local.set({ enabled: !enabled }); + }); +}); diff --git a/scripts/runtimeOnInstalled.js b/scripts/runtimeOnInstalled.js new file mode 100644 index 0000000..c83bc2d --- /dev/null +++ b/scripts/runtimeOnInstalled.js @@ -0,0 +1,8 @@ +chrome.runtime.onInstalled.addListener(() => { + chrome.storage.local.get('enabled', ({ enabled }) => { + if (enabled === null || enabled === undefined) enabled = true; + chrome.storage.local.set({ enabled }); + // have to set this manually as the trigger doesn't work for `onInstalled` + chrome.browserAction.setBadgeText({ text: enabled ? "ON" : "OFF" }); + }); +}); diff --git a/scripts/storageOnChanged.js b/scripts/storageOnChanged.js new file mode 100644 index 0000000..2a1c909 --- /dev/null +++ b/scripts/storageOnChanged.js @@ -0,0 +1,7 @@ +chrome.storage.onChanged.addListener((changes, areaName) => { + if (areaName !== "local") return; + if (!changes.enabled) return; + const { newValue } = changes.enabled; + console.log(newValue); + chrome.browserAction.setBadgeText({ text: newValue ? "ON" : "OFF" }); +}); diff --git a/scripts/tabOnUpdated.js b/scripts/tabOnUpdated.js new file mode 100644 index 0000000..a98bc4b --- /dev/null +++ b/scripts/tabOnUpdated.js @@ -0,0 +1,36 @@ +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 +}