forked from meikuam/lazy_yandex_music_developers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
injected.js
282 lines (230 loc) · 10.3 KB
/
injected.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
--------------------------------------------
Preambula:
I'm not a js programmer, so I'm not worried
about the correct use of code styles.
It is important for me that it just works.
--------------------------------------------
Copyright (C) 2019 Beresnev Alexey
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
async function get_playlist(owner, playlist_id) {
url = "https://music.yandex.ru/handlers/playlist.jsx?owner=" + owner + "&kinds=" + playlist_id + "&light=false&lang=ru&external-domain=music.yandex.ru";
var playlist = null;
await $.get(url).done(function(data) {
playlist = data.playlist;
});
return playlist;
}
// data = [{"op":"move","from":1, "to":0,"tracks":[{"id":"2201582","albumId":220390}]}]
async function playlist_patch(owner_id, playlist_id, data, revision_id) {
// [{"op":"move","from":9,"to":8,"tracks":[{"id":"2191979","albumId":216924} ]}]
url = "https://music.yandex.ru/handlers/playlist-patch.jsx";
await $.post(url, {
owner: owner_id,
kind: playlist_id,
revision: revision_id,
diff: JSON.stringify(data),
lang: "ru",
sign: Mu['authData']['user']['sign'],
experiments: JSON.stringify(Mu['experiments']),
"external-domain": "music.yandex.ru"
}).done(function(data) {
console.log('done');
}).error(function(data){
console.log(data);
});
}
function get_name(item, sort_by_name=false) {
/*
Get string for item in tracks
for compare purpuses.
*/
if(sort_by_name) {
let a_name = item['title'];
let a_artists = " - ";
item['artists'].forEach(function(art) {
a_artists += art['name'] + ", ";
});
a_name += a_artists;
return a_name;
} else {
let a_name = "";
item['artists'].forEach(function(art) {
a_name += art['name'] + ", ";
});
a_name = a_name + " - " + item['title'];
return a_name;
}
}
function get_track_ids(tracks) {
/*
Get track ids for playlist_patch function.
*/
var track_ids = [];
for(i=0; i < tracks.length; i++) {
if( tracks[i]["albums"] > 0) {
track_ids.push({
"id": tracks[i]["id"],
"albumId": tracks[i]["albums"][0]["id"]
});
} else {
track_ids.push({
"id": tracks[i]["id"]
});
}
}
return track_ids;
}
async function sort_items() {
var username = Mu['authData']['user']['login'];
var playlist_id = Mu['settings']['pathname'].split("/");
playlist_id = playlist_id[playlist_id.length - 1];
playlist = await get_playlist(username, playlist_id);
revision_id = playlist['revision']
tracks = playlist["tracks"];
// sort has been borrowed from here: https://stackoverflow.com/questions/282670/easiest-way-to-sort-dom-nodes
tracks.sort(function(a, b) {
let a_name = get_name(a);
let b_name = get_name(b);
return a_name == b_name
? 0
: (a_name > b_name ? 1 : -1);
});
console.log('sorted, send to server');
var track_ids = get_track_ids(tracks);
// insert tracks first
patch_data = [{"op":"insert","at":0,"tracks":track_ids}];
await playlist_patch(Mu['authData']['user']['uid'], playlist_id, patch_data, revision_id);
// delete tracks
delete_data = [{"op":"delete","from":tracks.length,"to": tracks.length * 2}];
await playlist_patch(Mu['authData']['user']['uid'], playlist_id, delete_data, revision_id + 1);
// reload page (stupid, but I don't find proper way)
location.reload(true);
console.log('sort items clicked');
}
async function del_duplicates() {
/*
Compare each item with others and
call "click()" method for
"d-track__delete" DOM element.
*/
let username = Mu['authData']['user']['login'];
let playlist_id = Mu['settings']['pathname'].split("/");
playlist_id = playlist_id[playlist_id.length - 1];
playlist = await get_playlist(username, playlist_id);
revision_id = playlist['revision']
tracks = playlist["tracks"];
// delete duplicates
let track_count = tracks.length;
let list = tracks;
list = list.reduce((accumulator, thing) => {
if (!accumulator.filter((duplicate) => get_name(thing) === get_name(duplicate))[0]) {
accumulator.push(thing);
}
return accumulator;
}, []);
tracks = list;
var track_ids = get_track_ids(tracks);
// insert tracks first
patch_data = [{"op":"insert","at":0,"tracks":track_ids}];
await playlist_patch(Mu['authData']['user']['uid'], playlist_id, patch_data, revision_id );
// delete tracks
delete_data = [{"op":"delete","from":tracks.length,"to": tracks.length + track_count}];
await playlist_patch(Mu['authData']['user']['uid'], playlist_id, delete_data, revision_id + 1);
// reload page (stupid, but I don't find proper way)
location.reload(true);
console.log('delete duplicates clicked');
}
function injected_main() {
// work only at pages with playlists
var playlist_div = document.getElementsByClassName("page-playlist")[0];
if (playlist_div != undefined) {
// var context_menu_div = playlist_div.getElementsByClassName("d-context-menu__content_playlist");
var context_menu_list = playlist_div.getElementsByClassName("d-context-menu__list");
if(context_menu_list.length >= 1) {
// console.log('context_menu_div not null', context_menu_div);
for (i = 0; i < context_menu_list.length; i++) {
var context_menu_list_item = context_menu_list[i];
var sort_menu_injected = false;
var del_duplicates_menu_injected = false;
var child_nodes = context_menu_list_item.children;
for (j = 0; j < child_nodes.length; j++) {
if (child_nodes[j].classList.contains("d-context-menu__item_sort")) {
sort_menu_injected = true;
}
if (child_nodes[j].classList.contains("d-context-menu__item_del_duplicates")) {
del_duplicates_menu_injected = true;
}
}
// sort button
if(sort_menu_injected == false) {
// console.log('sort_menu_div is null');
var sort_button = document.createElement("li");
sort_button.classList.add("d-context-menu__item");
sort_button.classList.add("deco-popup-menu__item");
sort_button.classList.add("d-context-menu__item_sort");
sort_button.onclick = sort_items;
var span = document.createElement("span");
span.classList.add("d-context-menu__item-icon");
var span_icon = document.createElement("span");
span_icon.classList.add("d-icon");
span_icon.classList.add("deco-icon");
span_icon.classList.add("d-icon_sun");
var span_image = document.createElement("img");
span_image.width = 24;
span_image.height = 24;
span_image.src = "https://image.flaticon.com/icons/png/128/54/54937.png";
span_image.classList.add("d-icon_sort");
span_icon.appendChild(span_image);
span.appendChild(span_icon);
sort_button.appendChild(span);
var span_title = document.createElement("span");
span_title.classList.add("d-context-menu__item-title");
span_title.innerHTML = "Сортировать плейлист";
sort_button.appendChild(span_title);
context_menu_list_item.appendChild(sort_button);
}
// del duplicates button
if(del_duplicates_menu_injected == false) {
// console.log('sort_menu_div is null');
var del_duplicates_button = document.createElement("li");
del_duplicates_button.classList.add("d-context-menu__item");
del_duplicates_button.classList.add("deco-popup-menu__item");
del_duplicates_button.classList.add("d-context-menu__item_del_duplicates");
del_duplicates_button.onclick = del_duplicates;
var span = document.createElement("span");
span.classList.add("d-context-menu__item-icon");
var span_icon = document.createElement("span");
span_icon.classList.add("d-icon");
span_icon.classList.add("deco-icon");
span_icon.classList.add("d-icon_sun");
var span_image = document.createElement("img");
span_image.width = 24;
span_image.height = 24;
span_image.src = "https://static.thenounproject.com/png/217757-200.png";
span_image.classList.add("d-icon_sort");
span_icon.appendChild(span_image);
span.appendChild(span_icon);
del_duplicates_button.appendChild(span);
var span_title = document.createElement("span");
span_title.classList.add("d-context-menu__item-title");
span_title.innerHTML = "Удалить дупликаты";
del_duplicates_button.appendChild(span_title);
context_menu_list_item.appendChild(del_duplicates_button);
}
}
}
}
// set timeout
var tid = setTimeout(injected_main, 300);
}