notes/blog/lastfm_bookmarklets.md
2024-04-13 20:44:27 +01:00

64 lines
2.3 KiB
Markdown
Executable File

---
author: Akbar Rahman
pub_date: Mon, 18 Sep 2023 16:25:48 +0100
title: last.fm bookmarklets
tags: [ last.fm, scripts ]
uuid: e54ebf58-4033-4dae-81db-91db344f1311
---
# last.fm bookmarklets
last.fm doesn't let you see how many scrobbled you've made in one day particularly easily.
Here is a bookmarklet to solve that.
<label for="days"> Number of days to view: </label>
<input type="number" value="1" id="days" placeholder="Days"/><br><br>
<label for="offset"> Offset (e.g. 0 to include today, 7 to look at last week): </label>
<input type="number" value="0" id="offset" placeholder="Offset (Days)" /><br><br>
<label for="username"> last.fm username </label>
<input type="text" value="" id="username" placeholder="Username" /><br><br>
<input type="button" id="button" value="Generate bookmarklet"><br>
<p><a style="display: none" href="" id="scriptLink">Bookmark this link</a></p>
When you press generate bookmarklet, the values `OFFSET`, `DAYS`, `USERNAME` will be
subsituted and put into the link above.
It's always best to inspect bookmarklets though.
Inspect the page to view the script used to generate the bookmarklet.
<textarea cols="109" rows="15" readonly id="scriptText">
javascript: (() => {
const MILLESECONDS_PER_DAY = 1000 * 24 * 60 * 60;
const OFFSET;
const DAYS;
const USERNAME;
const currentDate = new Date();
const to = new Date(currentDate - (OFFSET * MILLESECONDS_PER_DAY));
const from = new Date(to - ((DAYS-1) * MILLESECONDS_PER_DAY));
const toDate = to.getFullYear() + "-" + (to.getMonth()+1) + "-" + to.getDate();
const fromDate = from.getFullYear() + "-" + (from.getMonth()+1) + "-" + from.getDate();
document.location = "https://www.last.fm/user/" + USERNAME + "/library?from=" + fromDate + "&to=" + toDate;
})();
</textarea>
<script>
document.getElementById("button").addEventListener("click", () => {
scriptText = document.getElementById("scriptText").value;
offset = document.getElementById("offset").value;
days = document.getElementById("days").value;
username = document.getElementById("username").value;
newscript = scriptText.replace(
"OFFSET", "OFFSET = " + offset
).replace(
"DAYS", "DAYS = " + days
).replace(
"USERNAME", "USERNAME = '" + username + "'"
);
document.getElementById("scriptLink").href = newscript;
document.getElementById("scriptLink").style = "";
});
</script>