From b8c92bdbd0121492e385d7fac21c941cedcc193a Mon Sep 17 00:00:00 2001 From: kodxana Date: Mon, 21 Oct 2019 23:20:11 +0200 Subject: [PATCH] Added new things - Turn ON/OFF Switch - Detection of channels pages --- background.js | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/background.js b/background.js index 9f56e79..c54e860 100644 --- a/background.js +++ b/background.js @@ -1,14 +1,57 @@ var loaded = false; +var enable = false; + chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { - if (changeInfo.status == 'complete' && tab.status == 'complete' && tab.url != undefined) { + 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://open.lbry.com/" + channel + chrome.tabs.update({url: url}); + } + }); +} + function getVideoId(videoUrl) { var videoId = videoUrl.split('v=')[1] var ampersandPosition = videoId.indexOf('&') @@ -46,3 +89,12 @@ function validateVideo (id) { }); } + +chrome.browserAction.onClicked.addListener(function (tab) { + enable = enable ? false : true; + if(enable){ + chrome.browserAction.setBadgeText({ text: 'ON' }); + }else{ + chrome.browserAction.setBadgeText({ text: 'OFF' }); + } +});