From 93a4e4df487084cfbb5d445f70bcc1c949da2b49 Mon Sep 17 00:00:00 2001 From: Yamboy1 Date: Sun, 12 Jan 2020 08:59:09 +1300 Subject: [PATCH] new version with popup and lbry app support --- manifest.json | 5 +++-- popup/popup.css | 5 +++++ popup/popup.html | 20 ++++++++++++++++++++ popup/popup.js | 19 +++++++++++++++++++ scripts/runtimeOnInstalled.js | 8 -------- scripts/runtimeOnStartup.js | 12 ++++++++++++ scripts/tabOnUpdated.js | 19 +++++++++++++++---- 7 files changed, 74 insertions(+), 14 deletions(-) create mode 100644 popup/popup.css create mode 100644 popup/popup.html create mode 100644 popup/popup.js delete mode 100644 scripts/runtimeOnInstalled.js create mode 100644 scripts/runtimeOnStartup.js diff --git a/manifest.json b/manifest.json index 872052c..e3e8ff0 100644 --- a/manifest.json +++ b/manifest.json @@ -10,14 +10,15 @@ "background": { "scripts": [ "scripts/browserActionOnClicked.js", - "scripts/runtimeOnInstalled.js", + "scripts/runtimeOnStartup.js", "scripts/storageOnChanged.js", "scripts/tabOnUpdated.js" ], "persistent": false }, "browser_action": { - "default_title": "Watch on LBRY" + "default_title": "Watch on LBRY", + "default_popup": "popup/popup.html" }, "icons": { "16": "icons/icon16.png", diff --git a/popup/popup.css b/popup/popup.css new file mode 100644 index 0000000..64c1964 --- /dev/null +++ b/popup/popup.css @@ -0,0 +1,5 @@ +body { + width: 200px; + text-align: center; +} + diff --git a/popup/popup.html b/popup/popup.html new file mode 100644 index 0000000..9f7b9da --- /dev/null +++ b/popup/popup.html @@ -0,0 +1,20 @@ + + + + + + + +
+
+ + + + diff --git a/popup/popup.js b/popup/popup.js new file mode 100644 index 0000000..6b2b9c1 --- /dev/null +++ b/popup/popup.js @@ -0,0 +1,19 @@ +const checkbox = document.querySelector('input.enable'); +const radios = [...document.querySelectorAll('input[name="redirect"]').values()]; +console.log(radios); + +chrome.storage.local.get(['enabled', 'redirect'], ({ enabled, redirect }) => { + checkbox.checked = enabled; + const currentRadio = radios.find(x => x.getAttribute("value") === redirect) || radios[0]; + currentRadio.checked = true; +}); + +checkbox.addEventListener("input", () => { + chrome.storage.local.set({ enabled: checkbox.checked }); +}); + +radios.forEach(radio => { + radio.addEventListener("input", () => { + chrome.storage.local.set({ redirect: radio.getAttribute("value") }); + }); +}); diff --git a/scripts/runtimeOnInstalled.js b/scripts/runtimeOnInstalled.js deleted file mode 100644 index c83bc2d..0000000 --- a/scripts/runtimeOnInstalled.js +++ /dev/null @@ -1,8 +0,0 @@ -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/runtimeOnStartup.js b/scripts/runtimeOnStartup.js new file mode 100644 index 0000000..9655e3b --- /dev/null +++ b/scripts/runtimeOnStartup.js @@ -0,0 +1,12 @@ +const func = () => { + chrome.storage.local.get(['enabled', 'redirect'], ({ enabled, redirect }) => { + if (enabled === null || enabled === undefined) enabled = true; + if (!redirect) redirect = 'lbry.tv'; + chrome.storage.local.set({ enabled, redirect }); + // have to set this manually as the trigger doesn't work for `onInstalled` + chrome.browserAction.setBadgeText({ text: enabled ? 'ON' : 'OFF' }); + }); +}; + +chrome.runtime.onStartup.addListener(func); +chrome.runtime.onInstalled.addListener(func); diff --git a/scripts/tabOnUpdated.js b/scripts/tabOnUpdated.js index a98bc4b..a6286fc 100644 --- a/scripts/tabOnUpdated.js +++ b/scripts/tabOnUpdated.js @@ -1,6 +1,5 @@ chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => { - if (!tab.url) return; - + if (!changeInfo.url) return; const { id, type } = getId(tab.url); if (!id) return; @@ -10,9 +9,18 @@ chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => { console.log(json); const title = json.data[`${type}s`][id]; if (!title) return; + console.log(title); - let newUrl = `https://lbry.tv/${title.replace(/^lbry:\/\//, "").replace(/#/g, ":")}`; - chrome.tabs.update(tab.tabId, { url: newUrl }); + chrome.storage.local.get('redirect', ({ redirect }) => { + console.log(redirect); + let newUrl; + if (redirect === "lbry.tv") { + newUrl = `https://lbry.tv/${title.replace(/^lbry:\/\//, "").replace(/#/g, ":")}`; + } else if (redirect === "app") { + newUrl = `lbry://${title.replace(/^lbry:\/\//, "")}`; + } + chrome.tabs.update(tabId, { url: newUrl }); + }); }); function getId(url) { @@ -34,3 +42,6 @@ function getChannelId(url) { const match = url.match(regex); return match ? match[1] : null; // match[1] is the channelId } + +function getNewUrl(title) { +}