-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.user.js
130 lines (113 loc) · 4.39 KB
/
script.user.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
// ==UserScript==
// @name playlist_from_YT_links
// @namespace git_script
// @include http://www.wykop.pl/mikroblog/*
// @include http://www.wykop.pl/tag/*
// @version 1
// @grant none
// ==/UserScript==
function main()
{
var player;
var playlist = [];
var originalTitle = document.title;
function appendElements()
{
$('#content').prepend($('<div>').attr('id', 'playerWrapper').css({'text-align': 'center', 'overflow': 'hidden'}).append($('<div>').attr('id', 'player')))
$('#playerWrapper').prepend($('<div>').attr('id', 'controllPlayerWrapper').css('text-align', 'left'));
$('#controllPlayerWrapper').prepend($('<span>').attr({'id': 'handlePlayer', 'class': 'playerH'}).text('Odtwórz wszystkie (' + playlist.length + ').').css({'display': 'inline-block', 'font-weight': 'bold', 'cursor': 'pointer'}));
$('<span>').css({'display': 'inline-block', 'font-weight': 'bold', 'cursor': 'pointer'}).text('Odśwież.').attr({'id': 'refreshPlaylist'}).insertAfter('#handlePlayer');
$('<span>').css({'display': 'inline-block', 'font-weight': 'bold'}).attr('class', 'marginright5 marginleft5').text(' | ').insertAfter('#handlePlayer');
}
function parseLinks(i, e)
{
var href = e.getAttribute('href');
if (href.indexOf('//www.youtube.com/watch') > -1) {
var pattern = /\/\/www.youtube.com\/watch\?.*?v=(.{11})/i;
var result = pattern.exec(href);
playlist.push(result[1]);
} else if (href.indexOf('//youtu.be') > -1) {
var pattern = /\/\/youtu.be\/(.{11})/i;
var result = pattern.exec(href);
playlist.push(result[1]);
}
}
function handlePlayer()
{
if (typeof player === "undefined")
onYouTubeIframeAPIReady();
if ($(this).is('.playerD'))
{
$(this).removeClass('playerD').addClass('playerH');
$('#playerWrapper').animate({'height': 16}, 'fast');
} else
{
$(this).removeClass('playerH').addClass('playerD');
$('#playerWrapper').animate({'height': 416}, 'fast');
}
}
function appendYoutubeAPI()
{
if (typeof player === "undefined") {
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
}
}
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: null,
events: {
'onReady': onPlayerReady,
'onStateChange' : onStateChange
}
});
}
function onStateChange(event)
{
if(event.data == YT.PlayerState.PLAYING){
document.title = '\u25b6 '+originalTitle;
}else if(event.data == YT.PlayerState.ENDED || event.data == YT.PlayerState.CUED){
document.title = '\u25A0 '+originalTitle;
}else if(event.data == YT.PlayerState.PAUSED || event.data == YT.PlayerState.BUFFERING){
document.title = '\u258c\u258c '+originalTitle;
}else{
document.title = originalTitle;
}
}
function onPlayerReady()
{
player.cuePlaylist(playlist);
}
function buildPlaylist()
{
playlist = [];
$('a[href^="https://www.youtube.com"]:not(".openEmbed"), a[href^="http://www.youtube.com"]:not(".openEmbed"), a[href^="http://youtu.be"]:not(".openEmbed")').each(parseLinks);
$('#handlePlayer').text('Odtwórz wszystkie (' + playlist.length + ').');
if (typeof player !== "undefined")
player.cuePlaylist(playlist);
}
buildPlaylist();
appendElements();
appendYoutubeAPI();
$('body').on('click', '#handlePlayer', handlePlayer);
$('body').on('click', '#refreshPlaylist', buildPlaylist);
}
if (typeof $ == 'undefined') {
if (typeof unsafeWindow !== 'undefined' && unsafeWindow.jQuery) {
var $ = unsafeWindow.jQuery;
main();
} else {
addJQuery(main);
}
} else {
main();
}
function addJQuery(callback) {
var script = document.createElement("script");
script.textContent = "(" + callback.toString() + ")();";
document.body.appendChild(script);
}