Last active
September 20, 2017 22:05
-
-
Save EvgeniGordeev/e864cdd99bde55ee7ad95b02001915c7 to your computer and use it in GitHub Desktop.
Sorting feature for VK playlist.
This file contains hidden or 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
(function (paramsMap) { | |
function loadScript(url, callback) { | |
var script = document.createElement("script") | |
script.type = "text/javascript"; | |
if (script.readyState) { //IE | |
script.onreadystatechange = function () { | |
if (script.readyState == "loaded" || script.readyState == "complete") { | |
script.onreadystatechange = null; | |
callback(); | |
} | |
}; | |
} else { //Others | |
script.onload = function () { | |
callback(); | |
}; | |
} | |
script.src = url; | |
document.getElementsByTagName("head")[0].appendChild(script); | |
} | |
function naturalSort(a, b) { | |
try { | |
return +/\d+/.exec(a)[0] - +/\d+/.exec(b)[0]; | |
} catch (e) { | |
return a.localeCompare(b); | |
} | |
} | |
function sortPlayList(jQuery) { | |
var edit_attrs = jQuery(".audio_pl_snippet__action_btn_edit").first().attr('onclick').split('(').slice(-1).pop().split(', '); | |
var owner_id = window.vk.id; | |
var playlist_id = edit_attrs[1]; | |
var getParams = { | |
owner_id: owner_id, playlist_id: playlist_id, | |
access_hash: null, act: 'load_section', al: 1, claim: 0, is_loading_all: 1, offset: 0, type: 'playlist' | |
}; | |
jQuery.post('https://vk.com/al_audio.php', getParams, | |
data => { | |
var jsonStr = data.split('<!json>').slice(1, 2).pop().split('<!>').shift(); | |
var playlist = JSON.parse(jsonStr); | |
var trackList = playlist.list; | |
trackList.sort((a, b) => { | |
var aText = a[3]; | |
var bText = b[3]; | |
return paramsMap.naturalSort ? naturalSort(aText, bText) : aText.localeCompare(bText); | |
}); | |
console.log("Tracks sorted like: "); | |
console.log(trackList.map(el => el[3])); | |
if (paramsMap.dryRun) { | |
return; | |
} | |
var audio_ids = trackList.map(el => el[1] + "_" + el[0]); | |
var updateParams = { | |
Audios: audio_ids.join(','), owner_id: owner_id, playlist_id: playlist_id, | |
act: 'save_playlist', al: 1, cover: 0, | |
description: playlist.description, hash: playlist.editHash, title: playlist.title | |
} | |
jQuery.post('https://vk.com/al_audio.php', updateParams, | |
() => console.log("Playlist was updated successfully")) | |
.fail(e => console.log("Playlist was not updated successfully, %s", e)); | |
}).fail(e => console.log("Error on loading the playlist, %s", e)); | |
} | |
loadScript("https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js", function () { | |
console.log('jQuery loaded'); | |
var jq = jQuery.noConflict(); | |
sortPlayList(jq); | |
}); | |
})({"naturalSort": true, "dryRun": true}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
By default the script is executed in dryRun mode. Set the 2nd argument to
false
to do an actual update to your VK play list.How to use it: