/
ReverseYouTubePlaylist.js
90 lines (87 loc) · 4.87 KB
/
ReverseYouTubePlaylist.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
// ==UserScript==
// @version 1.0.3a
// @name Reverse Playlist Order for YouTube
// @namespace https://github.com/ParticleCore
// @description Reverse the order of YouTube playlists
// @match *://www.youtube.com/*
// @run-at document-start
// @downloadURL https://github.com/ParticleCore/Particle/raw/master/MDL/rpo.user.js
// @grant none
// @noframes
// ==/UserScript==
(function () {
'use strict';
function add_Node(text, s_URL, funcToRun, type) {
var D = document;
var scriptNode = D.createElement (type);
if (text) scriptNode.textContent = text;
if (s_URL) scriptNode.src = s_URL;
if (funcToRun) scriptNode.textContent = '(' + funcToRun.toString() + ')()';
var targ = D.getElementsByTagName('head')[0]
|| D.body || D.documentElement;
targ.appendChild (scriptNode);
}
var styleSheet =
'#watch-appbar-playlist .yt-uix-button-icon-watch-appbar-reverse-video-list\n' +
'{background: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAMAAADXqc3KAAAANlBMVEX///////////////////////////////////////////////////////////////////////8BOg0gAAAAEXRSTlMA8KS9FQYBt8gPhw6JigvJyoYcNuUAAABRSURBVHhevdE5DsAgDERRs8ZAtrn/ZaMIiSKD5AbllX8qg/wubnHam0JPYSkACIl69nj5LB8VXRXm4EbggdvKYbCHHd1hHc3PxMqFuxhfsdwDNLwDxD27Q0MAAAAASUVORK5CYII=") no-repeat; width:24px; height:24px}',
core = function () {
function reverseControl() {
var temp,
prev = document.querySelector('[class*="prev-playlist-list-item"]'),
next = document.querySelector('[class*="next-playlist-list-item"]'),
list = document.getElementById('playlist-autoscroll-list'),
videos = list.getElementsByTagName('li'),
length = videos.length;
while (length--) {
list.appendChild(videos[length]);
}
temp = prev.href;
prev.href = next.href;
next.href = temp;
window.yt.player.getPlayerByElement('player-api').updatePlaylist();
list.scrollTop = document.getElementsByClassName('currently-playing')[0].offsetTop;
}
function buttonToggled(a) {
a = (window.chrome && a.target.parentNode) || a.target;
a.classList.toggle('yt-uix-button-toggled');
if (window.localStorage.playlistReverse) {
delete window.localStorage.playlistReverse;
} else {
window.localStorage.playlistReverse = window.yt.config_.LIST_ID;
}
reverseControl();
}
function insertButton() {
var playlistBar,
navControls,
spanWrapper,
spanIcon,
button = document.querySelector('[title="Reverse playlist"]');
if (location.href.indexOf('list=') !== -1 && !button) {
playlistBar = document.getElementById('watch-appbar-playlist');
navControls = document.querySelector('.playlist-nav-controls');
button = document.createElement('button');
button.title = 'Reverse playlist';
button.type = 'button';
button.className = 'yt-uix-button yt-uix-button-size-default yt-uix-button-player-controls yt-uix-button-empty yt-uix-button-has-icon reverse-playlist yt-uix-button-opacity yt-uix-tooltip' + ((location.href.indexOf(window.localStorage.playlistReverse) !== -1 && ' yt-uix-button-toggled') || '');
button.setAttribute('data-tooltip-text', 'Reverse playlist');
spanWrapper = document.createElement('span');
spanWrapper.className = 'yt-uix-button-icon-wrapper';
spanIcon = document.createElement('span');
spanIcon.className = 'yt-uix-button-icon yt-uix-button-icon-watch-appbar-reverse-video-list';
button.appendChild(spanWrapper);
button.appendChild(spanIcon);
navControls.appendChild(button);
button.addEventListener('click', buttonToggled);
playlistBar.className = playlistBar.className.replace('radio-playlist', '');
}
if (document.readyState === 'complete' && location.href.indexOf(window.localStorage.playlistReverse) !== -1) {
reverseControl();
}
}
window.addEventListener('readystatechange', insertButton, true);
window.addEventListener('spfdone', insertButton);
};
add_Node(null,null,core, 'script');
add_Node(styleSheet, null, null, 'style');
}());