From 5edb1726174e455ddb7be72781db56a683587fa5 Mon Sep 17 00:00:00 2001 From: theo1 Date: Tue, 29 Sep 2020 22:31:28 +0200 Subject: [PATCH] working browser extension to catch current track info and sent it to Flask bot --- deezer-tooter/background-script.js | 34 +++++++++++++++++++++++++ deezer-tooter/browserAction/index.html | 5 ++-- deezer-tooter/browserAction/script.js | 30 +++++++++++++++++----- deezer-tooter/content_script.js | 8 ++++++ deezer-tooter/icons/icon.png | Bin 3066 -> 1414 bytes deezer-tooter/manifest.json | 14 ++++------ 6 files changed, 73 insertions(+), 18 deletions(-) create mode 100644 deezer-tooter/background-script.js diff --git a/deezer-tooter/background-script.js b/deezer-tooter/background-script.js new file mode 100644 index 0000000..cf281b6 --- /dev/null +++ b/deezer-tooter/background-script.js @@ -0,0 +1,34 @@ +// function onError(error) { +// console.error(`Error in background script: ${error}`); +// } + +// function handleMessage(request, sender, sendResponse){ +// console.log("In background : " + request.command) + +// browser.tabs.query({ +// currentWindow: true, +// active: true +// }).then(sendMessageToTabs) +// .then(() => sendResponse({title: "No title yet"}) +// ) +// .catch(onError); + +// } + +// function sendMessageToTabs (tabs){ +// for(let tab of tabs){ +// browser.tabs.sendMessage( +// tab.id, +// {content: "get-title"} +// ).then(response => { +// console.log("Message from content script : " + response) +// }).catch(onError) +// } +// } + +// browser.runtime.onMessage.addListener(handleMessage); + + +// // "background": { +// // "scripts": ["background-script.js"] +// // }, \ No newline at end of file diff --git a/deezer-tooter/browserAction/index.html b/deezer-tooter/browserAction/index.html index 125d948..10c0eda 100644 --- a/deezer-tooter/browserAction/index.html +++ b/deezer-tooter/browserAction/index.html @@ -5,10 +5,11 @@ - + - +
+

- Gemini diff --git a/deezer-tooter/browserAction/script.js b/deezer-tooter/browserAction/script.js index 770fa5b..b96fd14 100644 --- a/deezer-tooter/browserAction/script.js +++ b/deezer-tooter/browserAction/script.js @@ -1,9 +1,6 @@ - -function tootButtonHandler() { - // TODO : add content script to get the pages' HTML elements - tootContent = "Now playing awesome stuff !" - url = "http://localhost:5000/toot?content=" - console.log(tootContent) +function tootInfo(message) { + tootContent = message.artist + " - " + message.title + url = "http://localhost:5000/toot?content=" + tootContent fetch(url) .then(function (response) { return response; @@ -16,5 +13,24 @@ function tootButtonHandler() { }) } +function handleResponse() { + document.getElementById("artist").innerText = message.artist + document.getElementById("title").innerText = message.title +} + +function handleError(error) { + console.log(`Error: ${error}`); +} + +function getCurrentInfo () { + var gettingActiveTab = browser.tabs.query({active: true, currentWindow: true}) + gettingActiveTab.then((tabs) => { + browser.tabs.sendMessage(tabs[0].id, {command: "get-info"}) + .then(handleResponse, handleError); + }) +} + button = document.getElementById('toot-button') -button.onclick = tootButtonHandler \ No newline at end of file +browser.browserAction.onClicked.addListener(getCurrentInfo) +button.onclick = tootInfo +browser.tabs.executeScript(null, { file: "/content_script.js" }); \ No newline at end of file diff --git a/deezer-tooter/content_script.js b/deezer-tooter/content_script.js index e69de29..8098157 100644 --- a/deezer-tooter/content_script.js +++ b/deezer-tooter/content_script.js @@ -0,0 +1,8 @@ +browser.runtime.onMessage.addListener((data, sender, sendResponse) => { + console.log("Content script : " + data.command) + deezerTitle = document.getElementsByClassName('track-link')[0].innerHTML + deezerArtist = document.getElementsByClassName('track-link')[1].innerHTML + sendResponse({title: deezerTitle, artist: deezerArtist}) +}); + +console.log("Content script loaded") \ No newline at end of file diff --git a/deezer-tooter/icons/icon.png b/deezer-tooter/icons/icon.png index 063cfa8f40d2773ce841dea48d343e3940b2b7ee..fb2675f0b3b199fd4f680f989590ff1761732a81 100644 GIT binary patch literal 1414 zcmV;11$p|3P)C0001HP)t-s|Ns9m zh}12H)A;-TW2N7Px#s5a`l`I** z-;BNI^Z5PR>i5Lm^HQAK&EoaA*6^ds?7!Obcd_GYso=5F@0!Kxhq~r_v*Sy>CE@@8 z1lvhOK~#90?ONHkt0)kSK%5Y8icu5w`2RoIYGM>2*D&PVUfu48JgA{IMNJf(P6rMg zIB?*=fddB)=zuYpN0B=qj^p7sigL)ky-6{~z8_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8733124..3ce9752 100644 --- a/deezer-tooter/manifest.json +++ b/deezer-tooter/manifest.json @@ -8,12 +8,8 @@ }, "content_scripts": [ { - "matches": [ - "*://*.mozilla.org/*" - ], - "js": [ - "content_script.js" - ] + "matches": [""], + "js": ["content_script.js"] } ], "browser_action": { @@ -23,7 +19,7 @@ "default_popup": "browserAction/index.html", "default_title": "Deezer Tooter" }, - "options_ui": { - "page": "options/index.html" - } + "permissions" : [ + "activeTab" + ] } \ No newline at end of file