-
Notifications
You must be signed in to change notification settings - Fork 2
/
historyShortcut.js
127 lines (103 loc) · 4.46 KB
/
historyShortcut.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// NAME: History Shortcut
// AUTHOR: einzigartigerName
// DESCRIPTION: Adds a Shortcut to your Listening History to the Sidebar
(function HistoryShortcut() {
const { CosmosAPI, Player, LocalStorage, PlaybackControl, ContextMenu, URI } = Spicetify
if (!(CosmosAPI && Player && LocalStorage && PlaybackControl && ContextMenu && URI)) {
setTimeout(HistoryShortcut, 300)
return
}
const ITEM_LABEL = "History"
const HISTORY_DIV_CLASS = "SidebarListItem"
const HISTORY_DIV_CLASS_ACTIVE = "SidebarListItem SidebarListItem--is-active"
const HISTORY_ANKER_CLASS = "SidebarListItemLink SidebarListItemLink--tall spoticon-time-24"
const HISTORY_ANKER_CLASS_ACTIVE = "SidebarListItemLink SidebarListItemLink--is-highlighted SidebarListItemLink--tall spoticon-time-24"
let historyItem = createHistoyItem()
// Get Sidebar Lists
var topicList = document.querySelector("#view-navigation-bar > div > div.LeftSidebar__section > div > ul")
if (topicList) {
// Add to first in list
// On default layout this would be the Home/Browse/Radio List
topicList.appendChild(historyItem.div)
} else {
return
}
const toCheckMutate = document.getElementById('view-content');
const config = { attributes: true, childList: true, subtree: true };
let observerCallback = function(_, _) {
appQueue = document.getElementById("app-queue")
if (!appQueue){ return }
if (appQueue.getAttribute("class") === "active"
&& appQueue.getAttribute("data-app-uri") === "spotify:app:queue:history"
) {
onClickHistory()
} else {
onLeaveHistory()
}
};
let observer = new MutationObserver(observerCallback)
observer.observe(toCheckMutate, config)
// Deactivate Active Status for History Item
function onLeaveHistory() {
historyItem.div.setAttribute("class",HISTORY_DIV_CLASS)
historyItem.anker.setAttribute("class", HISTORY_ANKER_CLASS)
}
// Activate Active Status for History Item
function onClickHistory() {
historyItem.div.setAttribute("class", HISTORY_DIV_CLASS_ACTIVE)
historyItem.anker.setAttribute("class", HISTORY_ANKER_CLASS_ACTIVE)
}
// Construct the List Item
function createHistoyItem() {
/* List Item
* <li class="SidebarListItem">
*/
let listItem = document.createElement("li")
listItem.setAttribute("class", HISTORY_DIV_CLASS)
/* Outer Div Element
* <div class="DropTarget SidebarListItem__drop-target DropTarget--tracks DropTarget--albums DropTarget--artists DropTarget--playlists">
*/
let outer = document.createElement("div")
outer.setAttribute("class", "DropTarget SidebarListItem__drop-target")
/* Middle Div Element
* <div class="SidebarListItem__inner">
*/
let inner = document.createElement("div")
inner.setAttribute("class", "SidebarListItem__inner")
/* Link Div Element
* <div class="SidebarListItem__link">
*/
let link = document.createElement("div")
link.setAttribute("class", "SidebarListItem__link")
/* Anker
* <a class="SidebarListItemLink SidebarListItemLink--tall spoticon-time-24"
* draggable="false"
* href="spotify:app:queue:history"
* data-sidebar-list-item-uri="spotify:app:queue:history"
* data-ta-id="sidebar-list-item-link">
*/
anker = document.createElement("a")
anker.setAttribute("class", HISTORY_ANKER_CLASS)
anker.setAttribute("draggable", "false")
anker.setAttribute("href", "spotify:app:queue:history")
anker.setAttribute("data-sidebar-list-item-uri", "spotify:app:queue:history")
anker.setAttribute("data-ta-id", "sidebar-list-item-link")
/* Item Text
* <span class="SidebarListItem__label"
* dir="auto">
* History
* </span>
*/
span = document.createElement("span")
span.setAttribute("class", "SidebarListItem__label")
span.setAttribute("dir", "auto")
span.textContent = ITEM_LABEL
anker.appendChild(span)
link.appendChild(anker)
inner.appendChild(link)
outer.appendChild(inner)
listItem.appendChild(outer)
listItem.addEventListener("click", onClickHistory)
return {div: listItem, anker: anker}
}
})();