new version with popup and lbry app support

This commit is contained in:
Yamboy1 2020-01-12 08:59:09 +13:00
parent b21257eeb4
commit 93a4e4df48
7 changed files with 74 additions and 14 deletions

View file

@ -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",

5
popup/popup.css Normal file
View file

@ -0,0 +1,5 @@
body {
width: 200px;
text-align: center;
}

20
popup/popup.html Normal file
View file

@ -0,0 +1,20 @@
<!doctype html>
<html lang="en">
<head>
<link rel="stylesheet" href="popup.css" />
</head>
<body>
<label>
Enable:
<input class="enable" type="checkbox" />
</label>
<br>
<br>
<label>
Redirect to: <br>
<input type="radio" name="redirect" value="lbry.tv" /> https:///lbry.tv <br>
<input type="radio" name="redirect" value="app"/> LBRY App
</label>
<script src="popup.js"></script>
</body>
</html>

19
popup/popup.js Normal file
View file

@ -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") });
});
});

View file

@ -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" });
});
});

View file

@ -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);

View file

@ -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) {
}