From 271fca44fbb1df3e623046534de26c0c1ccff7e2 Mon Sep 17 00:00:00 2001 From: kodxana Date: Sat, 19 Oct 2019 23:51:45 +0200 Subject: [PATCH] Add files via upload --- background.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ manifest.json | 16 ++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 background.js create mode 100644 manifest.json diff --git a/background.js b/background.js new file mode 100644 index 0000000..0d46f89 --- /dev/null +++ b/background.js @@ -0,0 +1,50 @@ +chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { + if(tab.url.includes('watch?')) { + let videoId = getVideoId(tab.url) + validateVideo(videoId) + } +}); + +function getVideoId(videoUrl) { + var videoId = videoUrl.split('v=')[1] + var ampersandPosition = videoId.indexOf('&') + if(ampersandPosition != -1) { + videoId = videoId.substring(0, ampersandPosition) + } + return videoId +} + +chrome.runtime.onMessage.addListener(function(request, sender) { + chrome.tabs.update(sender.tab.id, {url: request.redirect}); +}); + +function validateVideo (id) { + 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) { + console.log('Success!') + let title = data.data.videos[id] + + + if (title != null) { + let url = "https://open.lbry.com/" + title + console.log(url) + chrome.tabs.update({url: url}); + } + + }); + +} \ No newline at end of file diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..714db0f --- /dev/null +++ b/manifest.json @@ -0,0 +1,16 @@ +{ + "name": "Watch on LBRY", + "version": "1.0", + "permissions": [ + "https://www.youtube.com/watch?v=*", + "tabs" + ], + "background": { + "scripts": ["background.js"], + "persistent": false + }, + "browser_action": { + "default_title": "Test" + }, + "manifest_version": 2 +} \ No newline at end of file