Changed how the artwork gets sent to the API, this is done by sending it separately, so now we have 2 endpoints, its not the best but, it works for now :)
This commit is contained in:
82
content.js
82
content.js
@ -1,7 +1,9 @@
|
|||||||
let pauseStartTime = null;
|
let pauseStartTime = null;
|
||||||
let endpoint = null;
|
let endpoint = null;
|
||||||
|
let artworkEndpoint = null;
|
||||||
let token = null;
|
let token = null;
|
||||||
let intervalId = null;
|
let intervalId = null;
|
||||||
|
let lastSentArtworkKey = null;
|
||||||
|
|
||||||
async function imageUrlToBase64(url) {
|
async function imageUrlToBase64(url) {
|
||||||
try {
|
try {
|
||||||
@ -72,6 +74,7 @@ async function getNowPlayingData() {
|
|||||||
|
|
||||||
const currentTime = timestampInput ? Number(timestampInput.getAttribute('aria-valuenow')) : media.currentTime;
|
const currentTime = timestampInput ? Number(timestampInput.getAttribute('aria-valuenow')) : media.currentTime;
|
||||||
const duration = timestampInput ? Number(timestampInput.getAttribute('aria-valuemax')) : media.duration;
|
const duration = timestampInput ? Number(timestampInput.getAttribute('aria-valuemax')) : media.duration;
|
||||||
|
const commonData={details:title,state:artist,album,smallImageKey:'play',smallImageText:'Playing',startTimestamp:Math.floor(Date.now()/1000)-Math.floor(currentTime),endTimestamp:Math.floor(Date.now()/1000)+Math.floor(duration-currentTime),paused,currentTime,duration};
|
||||||
|
|
||||||
if (paused) {
|
if (paused) {
|
||||||
if (!pauseStartTime) pauseStartTime = Date.now();
|
if (!pauseStartTime) pauseStartTime = Date.now();
|
||||||
@ -81,37 +84,12 @@ async function getNowPlayingData() {
|
|||||||
state: "",
|
state: "",
|
||||||
paused: true
|
paused: true
|
||||||
};
|
};
|
||||||
} else {
|
|
||||||
return {
|
|
||||||
details: title,
|
|
||||||
state: artist,
|
|
||||||
album,
|
|
||||||
smallImageKey: 'play',
|
|
||||||
smallImageText: 'Playing',
|
|
||||||
artworkBase64,
|
|
||||||
startTimestamp: Math.floor(Date.now() / 1000) - Math.floor(currentTime),
|
|
||||||
endTimestamp: Math.floor(Date.now() / 1000) + Math.floor(duration - currentTime),
|
|
||||||
paused: true,
|
|
||||||
currentTime,
|
|
||||||
duration
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
pauseStartTime = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
details: title,
|
...commonData,
|
||||||
state: artist,
|
artworkBase64
|
||||||
album,
|
|
||||||
smallImageKey: 'play',
|
|
||||||
smallImageText: 'Playing',
|
|
||||||
artworkBase64,
|
|
||||||
startTimestamp: Math.floor(Date.now() / 1000) - Math.floor(currentTime),
|
|
||||||
endTimestamp: Math.floor(Date.now() / 1000) + Math.floor(duration - currentTime),
|
|
||||||
paused: false,
|
|
||||||
currentTime,
|
|
||||||
duration
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,23 +100,54 @@ async function sendNowPlaying() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const data = await getNowPlayingData();
|
const data = await getNowPlayingData();
|
||||||
|
if (!data) return;
|
||||||
|
|
||||||
|
const { artworkBase64, details, state, ...noArtData } = data;
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(endpoint, {
|
await fetch(endpoint, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Authorization': `Bearer ${token}`
|
'Authorization': `Bearer ${token}`
|
||||||
},
|
},
|
||||||
body: JSON.stringify(data),
|
body: JSON.stringify({
|
||||||
|
...noArtData,
|
||||||
|
details,
|
||||||
|
state
|
||||||
|
})
|
||||||
});
|
});
|
||||||
if (response.ok) {
|
console.log('Now playing data sent:', details, '-', state);
|
||||||
console.log('Now playing data sent:', data.details, '-', data.state);
|
|
||||||
} else {
|
|
||||||
console.error('Failed to send now playing data:', response.status);
|
|
||||||
}
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Error sending now playing data:', e);
|
console.error('Failed to send now playing:', e);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only send artwork when song changes
|
||||||
|
const currentTrackKey = `${details} - ${state}`;
|
||||||
|
if (
|
||||||
|
artworkEndpoint &&
|
||||||
|
artworkBase64 &&
|
||||||
|
currentTrackKey !== lastSentArtworkKey
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
await fetch(artworkEndpoint, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Authorization': `Bearer ${token}`
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
image: artworkBase64,
|
||||||
|
details,
|
||||||
|
state
|
||||||
|
})
|
||||||
|
});
|
||||||
|
lastSentArtworkKey = currentTrackKey;
|
||||||
|
console.log('Artwork sent for:', currentTrackKey);
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Failed to send artwork:', e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -169,13 +178,14 @@ if (!window._nowPlayingExtensionLogged) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (typeof chrome !== "undefined" && chrome.storage && chrome.storage.sync) {
|
if (typeof chrome !== "undefined" && chrome.storage && chrome.storage.sync) {
|
||||||
chrome.storage.sync.get(['endpoint', 'token'], (result) => {
|
chrome.storage.sync.get(['endpoint', 'artworkEndpoint', 'token'], (result) => {
|
||||||
if (chrome.runtime.lastError) {
|
if (chrome.runtime.lastError) {
|
||||||
console.error("Failed to load settings:", chrome.runtime.lastError.message);
|
console.error("Failed to load settings:", chrome.runtime.lastError.message);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
endpoint = result.endpoint;
|
endpoint = result.endpoint;
|
||||||
|
artworkEndpoint = result.artworkEndpoint;
|
||||||
token = result.token;
|
token = result.token;
|
||||||
|
|
||||||
if (!endpoint || !token) {
|
if (!endpoint || !token) {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"manifest_version": 3,
|
"manifest_version": 3,
|
||||||
"name": "Apple Music Now Playing",
|
"name": "Apple Music Now Playing",
|
||||||
"version": "1.3.1",
|
"version": "1.4.0",
|
||||||
"description": "Easily display your currently playing Apple Music track on your website with this extension.",
|
"description": "Easily display your currently playing Apple Music track on your website with this extension.",
|
||||||
"permissions": [
|
"permissions": [
|
||||||
"storage",
|
"storage",
|
||||||
|
@ -88,8 +88,10 @@
|
|||||||
<body>
|
<body>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<h1><span title="Apple Music Now Playing">AmNP</span> Settings (Beta)</h1>
|
<h1><span title="Apple Music Now Playing">AmNP</span> Settings (Beta)</h1>
|
||||||
<label for="endpoint">POST Endpoint:</label>
|
<label for="endpoint">Now playing Endpoint:</label>
|
||||||
<input type="text" id="endpoint" placeholder="https://zorpzeep.blorp/wp-json/nowplaying/v1/update" autocomplete="off" spellcheck="false"/>
|
<input type="text" id="endpoint" placeholder="https://zorpzeep.blorp/wp-json/nowplaying/v1/update" autocomplete="off" spellcheck="false"/>
|
||||||
|
<label for="artworkEndpoint">Artwork Endpoint:</label>
|
||||||
|
<input type="text" id="artworkEndpoint" placeholder="https://zorpzeep.blorp/wp-json/nowplaying/v1/update/art" autocomplete="off" spellcheck="false"/>
|
||||||
<label for="token">Bearer Token:</label>
|
<label for="token">Bearer Token:</label>
|
||||||
<input type="text" id="token" placeholder="73757065725f7365637572655f746f6b656e" autocomplete="off" spellcheck="false"/>
|
<input type="text" id="token" placeholder="73757065725f7365637572655f746f6b656e" autocomplete="off" spellcheck="false"/>
|
||||||
<button id="save">Save</button>
|
<button id="save">Save</button>
|
||||||
|
@ -1,14 +1,16 @@
|
|||||||
/* global chrome */
|
/* global chrome */
|
||||||
document.addEventListener('DOMContentLoaded', () => {
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
chrome.storage.sync.get(['endpoint', 'token'], (data) => {
|
chrome.storage.sync.get(['endpoint', 'token', 'artworkEndpoint'], (data) => {
|
||||||
document.getElementById('endpoint').value = data.endpoint || '';
|
document.getElementById('endpoint').value = data.endpoint || '';
|
||||||
document.getElementById('token').value = data.token || '';
|
document.getElementById('token').value = data.token || '';
|
||||||
|
document.getElementById('artworkEndpoint').value = data.artworkEndpoint || '';
|
||||||
});
|
});
|
||||||
|
|
||||||
document.getElementById('save').addEventListener('click', () => {
|
document.getElementById('save').addEventListener('click', () => {
|
||||||
const endpoint = document.getElementById('endpoint').value;
|
const endpoint = document.getElementById('endpoint').value;
|
||||||
const token = document.getElementById('token').value;
|
const token = document.getElementById('token').value;
|
||||||
chrome.storage.sync.set({ endpoint, token }, () => {
|
const artworkEndpoint = document.getElementById('artworkEndpoint').value;
|
||||||
|
chrome.storage.sync.set({ endpoint, token, artworkEndpoint }, () => {
|
||||||
alert('Settings saved.');
|
alert('Settings saved.');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user