See commit desc.

Big changes here.
- I've moved all of the scripts to individual
files in the `scripts` folder.
- I've updated all the code to ES7.
- The extension stores the state in browser
storage now, and it persists.
- The extension has a default state of on.
This commit is contained in:
Yamboy1 2020-01-11 22:49:50 +13:00
parent 7cfb3c875a
commit 464150afac
6 changed files with 69 additions and 106 deletions

View file

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

View file

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

View file

@ -0,0 +1,5 @@
chrome.browserAction.onClicked.addListener(() => {
chrome.storage.local.get('enabled', ({ enabled }) => {
chrome.storage.local.set({ enabled: !enabled });
});
});

View file

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

View file

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

36
scripts/tabOnUpdated.js Normal file
View file

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