You are using client-side JavaScript library.
If you will browse the NPM, there are plenty of Last.fm modules, the most popular is simple-lastfm
Here is the example taken from the docs:
var Lastfm = require('simple-lastfm');
var lastfm = new Lastfm({
api_key: 'xxx',
api_secret: 'xxx',
username: 'xxx',
password: 'xxx'
});
lastfm.getSessionKey(function(result) {
console.log("session key = " + result.session_key);
if(result.success) {
lastfm.scrobbleNowPlayingTrack({
artist: 'Ratatat',
track: 'Seventeen Years',
callback: function(result) {
console.log("in callback, finished: ", result);
}
});
} else {
console.log("Error: " + result.error);
}
});