working browser extension to catch current track info and sent it to Flask bot

This commit is contained in:
theo1 2020-09-29 22:31:28 +02:00
parent da88659dd5
commit 5edb172617
6 changed files with 73 additions and 18 deletions

View File

@ -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"]
// // },

View File

@ -5,10 +5,11 @@
<link href="style.css" rel="stylesheet" />
</head>
<body>
<input type="text" name="Content" id="toot-content">
<span id="artist"></span> - <span id="title"></span>
<br>
<input type="text" name="Comment" id="toot-content" placeholder="Your comment here">
<button id="toot-button">Toot !</button><br>
<br>
<span id='track-link'>Gemini</span>
<script src="script.js"></script>
</body>
</html>

View File

@ -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
browser.browserAction.onClicked.addListener(getCurrentInfo)
button.onclick = tootInfo
browser.tabs.executeScript(null, { file: "/content_script.js" });

View File

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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

After

Width:  |  Height:  |  Size: 1.4 KiB

View File

@ -8,12 +8,8 @@
},
"content_scripts": [
{
"matches": [
"*://*.mozilla.org/*"
],
"js": [
"content_script.js"
]
"matches": ["<all_urls>"],
"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"
]
}