forked from LBRYFoundation/Watch-on-LBRY
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from Yamboy1/master
Rewrite with a few extra features
- Loading branch information
Showing
8 changed files
with
147 additions
and
121 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,28 @@ | ||
{ | ||
"name": "Watch on LBRY", | ||
"version": "1.4", | ||
"permissions": [ | ||
"https://www.youtube.com/watch?v=*", | ||
"https://www.youtube.com/channel/*", | ||
"tabs" | ||
], | ||
"background": { | ||
"scripts": ["background.js"], | ||
"persistent": false | ||
}, | ||
"browser_action": { | ||
"default_title": "Watch on LBRY" | ||
}, | ||
"icons": { "16": "icons/icon16.png", | ||
"48": "icons/icon48.png", | ||
"128": "icons/icon128.png" | ||
}, | ||
"manifest_version": 2 | ||
} | ||
{ | ||
"name": "Watch on LBRY", | ||
"version": "1.4", | ||
"permissions": [ | ||
"https://www.youtube.com/watch?v=*", | ||
"https://www.youtube.com/channel/*", | ||
"tabs", | ||
"storage" | ||
], | ||
"background": { | ||
"scripts": [ | ||
"scripts/runtimeOnStartup.js", | ||
"scripts/storageOnChanged.js", | ||
"scripts/tabOnUpdated.js" | ||
], | ||
"persistent": false | ||
}, | ||
"browser_action": { | ||
"default_title": "Watch on LBRY", | ||
"default_popup": "popup/popup.html" | ||
}, | ||
"icons": { | ||
"16": "icons/icon16.png", | ||
"48": "icons/icon48.png", | ||
"128": "icons/icon128.png" | ||
}, | ||
"manifest_version": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
body { | ||
width: 200px; | ||
text-align: center; | ||
} | ||
|
||
.container { | ||
display: inline-block; | ||
text-align: left; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<link rel="stylesheet" href="popup.css" /> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
<label> | ||
Enable: | ||
<input class="enable" type="checkbox" /> | ||
</label> | ||
<br> | ||
<br> | ||
<label> | ||
Redirect to: <br> | ||
<input type="radio" name="redirect" value="lbry.tv" /> LBRY.tv <br> | ||
<input type="radio" name="redirect" value="app"/> LBRY App | ||
</label> | ||
</div> | ||
<script src="popup.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => { | ||
chrome.storage.local.get(async ({ enabled }) => { | ||
if (!enabled) return; | ||
if (!changeInfo.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; | ||
console.log(title); | ||
|
||
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) { | ||
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 | ||
} | ||
|
||
function getNewUrl(title) { | ||
} |