Save Every Noise

Save and automatically load all the artists you have listend to on http://everynoise.com!

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         Save Every Noise
// @namespace    https://github.com/JZ6
// @version      1.6
// @description  Save and automatically load all the artists you have listend to on http://everynoise.com!
// @author       JZ6
// @match        *://everynoise.com/*
// @grant        none
// ==/UserScript==


const config = {
	entry: loadSavedArtists,
	// entry: saveAllPlayedArtists,
	divUniqueKey: 'textContent',
	lsSavedArtistsKey: `playedArtists`,
	importantKeys: [
		'baseURI',
		'id',
		'innerHTML',
		'innerText',
		'outerText',
		'textContent',
		'title'
	]
}

config.entry()

function loadSavedArtists() {
	const playedArtists = loadPlayedArtists()

	const playedDivs = getAllDivs()
		.map(hookArtistClick)
		.filter(div => playedArtists.hasOwnProperty(div[config.divUniqueKey]))

	playedDivs.forEach(div => div.setAttribute("played", true))

	console.info(`Loaded ${playedDivs.length} artists!`);
}

function hookArtistClick(div) {
	if (div.scan || div.title) {
		div.onclick = () => clickedArtist(div)
		// div.addEventListener("click", () => clickedArtist(div), false)		//Disable multiple click handlers until a work around is found for logic regarding scan current class tag.
	}
	return div
}

function clickedArtist(div) {

	const uniqueID = div[config.divUniqueKey]
	const playedArtists = loadPlayedArtists()

	if (div.hasAttribute('played')) {
		div.removeAttribute('played')
		delete playedArtists[uniqueID]
		console.info(`Removed ${uniqueID}!`);
	} else {
		div.setAttribute('played', true)
		playedArtists[uniqueID] = copyObjectSubSet(div, config.importantKeys)
		console.info(`Added ${uniqueID}!`);
	}

	savePlayedArtists(playedArtists)
}

function loadPlayedArtists() {
	const savedArtistString = localStorage.getItem(config.lsSavedArtistsKey) || '{}'
	// const savedArtistString = GM.getValue(config.lsSavedArtistsKey)
	return JSON.parse(savedArtistString)
}

function savePlayedArtists(playedArtists) {
	const savedArtistString = JSON.stringify(playedArtists)
	// GM.setValue(config.lsSavedArtistsKey, savedArtistString)
	localStorage.setItem(config.lsSavedArtistsKey, savedArtistString)
}

// Manual save
function saveAllPlayedArtists() {

	const playedArtists = {}

	getAllDivs()
		.filter(div => div.hasAttribute("played") && div.title)
		.forEach(div => playedArtists[div[config.divUniqueKey]] = copyObjectSubSet(div, config.importantKeys))

	savePlayedArtists(playedArtists)

}

function getAllDivs() {
	return [...document.getElementsByTagName("div")]
}

function copyObjectSubSet(obj, keys) {
	const result = {}
	keys.forEach(key => result[key] = obj[key])
	return result
}