Last active
February 14, 2016 22:09
-
-
Save shuuki/e19fa1881a5b0ecd322d to your computer and use it in GitHub Desktop.
LET ME SORT MY PLAYLISTS, PLEASE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/********************************************* | |
PLAYLISTS, PLEASE | |
Adding categories to Google Play Music. | |
Looks for any four character string followed by | |
an underscore in playlist titles. | |
For example: "YYYY_" | |
Make into bookmarklet using | |
http://chriszarate.github.io/bookmarkleter | |
*********************************************/ | |
javascript:(function(){ | |
// simple sorting utility | |
// from http://khan4019.github.io/front-end-Interview-Questions/sort.html | |
function sort(arr) { | |
var len = arr.length; | |
for(var i = len-1; i>=0; i--) { | |
for(var j = 1; j<=i; j++) { | |
if(arr[j-1]<arr[j]) { | |
// change ^ this to > to get least to greatest sort isntead of greatest to least | |
var temp = arr[j-1]; | |
arr[j-1] = arr[j]; | |
arr[j] = temp; | |
} | |
} | |
} | |
return arr; | |
} | |
// find where the playlists live | |
var library = document.getElementById("playlists"); | |
// list all the playlists in the library | |
var playlists = library.childNodes; | |
// some empty arrays for later | |
var sorted = []; | |
var categories = []; | |
// go through all playlists and categorize them | |
for(var i = 0; i < playlists.length; i++) { | |
// start with blank category | |
var category = null; | |
// the actual dom node for playlist links is vitally important | |
var link = playlists[i]; | |
// playlist titles are lurking in first child of playlist links | |
var name = playlists[i].childNodes[0].title; | |
// fish out category from title string | |
if(name.includes("_") == true) { | |
var marker = name.indexOf("_"); | |
category = name.slice(marker-4, marker); | |
} else { | |
category = "Recent"; | |
} | |
// pair up links with categories in an array of objects | |
sorted.push({ | |
"category": category, | |
"link": link | |
}); | |
// also drop unique categories into their own list | |
if(categories.includes(category) == false) { | |
categories.push(category); | |
} | |
} | |
// now sort categories so they look nice displayed together | |
sort(categories); | |
/* DISABLED | |
// remove all content from the sidebar so it can be replaced | |
library.innerHTML = ""; | |
*/ | |
// populate the sidebar with sorted categories and playlists | |
for(var k = 0; k < categories.length; k++) { | |
// construct container element for the category | |
var container = document.createElement("div"); | |
container.classList.add("playlist-category-container"); | |
// add category container to sidebar | |
library.appendChild(container); | |
// construct element for the category title | |
var label = document.createElement("div"); | |
label.classList.add("playlist-category-title"); | |
label.appendChild(document.createTextNode(categories[k])); | |
/* DISABLED*/ | |
// style the category title | |
label.style.fontWeight = "bold"; | |
label.style.padding = "10px 0 10px 18px"; | |
label.style.borderBottom = "1px solid #eee"; | |
label.style.backgroundColor = "#888"; | |
label.style.color = "#fff"; | |
// construct element to hold all links | |
var links = document.createElement("div"); | |
links.classList.add("playlist-category-links"); | |
// style links container | |
//links.style.display = "none"; | |
// add titles and space for links to the container | |
container.appendChild(label) | |
container.appendChild(links) | |
// add all links for each category | |
for(var j = 0; j < sorted.length; j++) { | |
if(sorted[j].category.includes(categories[k]) == true) { | |
links.appendChild(sorted[j].link) | |
} | |
} | |
} | |
})(); | |
/* | |
STYLES | |
.playlist-category-container {} | |
.playlist-category-title { | |
font-weight: bold; | |
padding: 10px 0 10px 18px; | |
border-bottom: 1px solid #ddd; | |
background: #eee; | |
} | |
.playlist-category-title:hover { | |
background: #ddd; | |
} | |
.playlist-category-title:active { | |
color: #ef6c00; | |
} | |
.playlist-category-links {} | |
.playlist-category-links.closed {} | |
.playlist-category-links.open {} | |
*/ | |
/* | |
BOOKMARKLET | |
javascript:void%20function(){(function(){function%20e(e){for(var%20t=e.length,l=t-1;l%3E=0;l--)for(var%20d=1;l%3E=d;d++)if(e[d-1]%3Ce[d]){var%20a=e[d-1];e[d-1]=e[d],e[d]=a}return%20e}for(var%20t=document.getElementById(%22playlists%22),l=t.childNodes,d=[],a=[],n=0;n%3Cl.length;n++){var%20i=null,o=l[n],r=l[n].childNodes[0].title;if(1==r.includes(%22_%22)){var%20s=r.indexOf(%22_%22);i=r.slice(s-4,s)}else%20i=%22Recent%22;d.push({category:i,link:o}),0==a.includes(i)%26%26a.push(i)}e(a);for(var%20c=0;c%3Ca.length;c++){var%20p=document.createElement(%22div%22);p.classList.add(%22playlist-category-container%22),t.appendChild(p);var%20u=document.createElement(%22div%22);u.classList.add(%22playlist-category-title%22),u.appendChild(document.createTextNode(a[c])),u.style.fontWeight=%22bold%22,u.style.padding=%2210px%200%2010px%2018px%22,u.style.borderBottom=%221px%20solid%20%23eee%22,u.style.backgroundColor=%22%23888%22,u.style.color=%22%23fff%22;var%20f=document.createElement(%22div%22);f.classList.add(%22playlist-category-links%22),p.appendChild(u),p.appendChild(f);for(var%20v=0;v%3Cd.length;v++)1==d[v].category.includes(a[c])%26%26f.appendChild(d[v].link)}})()}(); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment