forked from lidel/pls.watch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
yt-looper.user.js
142 lines (140 loc) · 6.17 KB
/
yt-looper.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
131
132
133
134
135
136
137
138
139
140
141
142
// ==UserScript==
// @name yt-looper
// @description Adds a button on YouTube, Imgur and SoundCloud to open current resource in yt.aergia.eu looper
// @namespace https://yt.aergia.eu
// @icon https://i.imgur.com/EGgL1nx.png
// @include http://www.youtube.com/watch*
// @include http://youtube.com/watch*
// @include https://www.youtube.com/watch*
// @include https://youtube.com/watch*
// @include https://imgur.com/*
// @include http://imgur.com/*
// @include http://soundcloud.com/*
// @include https://soundcloud.com/*
// @version 1.6.3
// @updateURL https://yt.aergia.eu/yt-looper.user.js
// @downloadURL https://yt.aergia.eu/yt-looper.user.js
// @require https://cdn.jsdelivr.net/jquery/3.0.0-alpha1/jquery.min.js
// @grant none
// ==/UserScript==
(function ($, undefined) {
'use strict';
var youtubeHandler = function () {
var humanize = function (sec) {
var sec_num = parseInt(sec, 10);
var hours = Math.floor(sec_num / 3600);
var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
var seconds = sec_num - (hours * 3600) - (minutes * 60);
hours = hours > 0 ? (hours + 'h') : '';
minutes = minutes > 0 ? (minutes + 'm') : '';
seconds = seconds > 0 ? (seconds + 's') : '';
return sec === 0 ? '0s' : hours + minutes + seconds;
};
var getYtPlayer = function () {
var doc = window.document;
var ytplayer = doc.getElementById('movie_player') || doc.getElementById('movie_player-flash');
return ytplayer;
};
var renderLooperActions = function() {
$('#yt-looper-start').remove();
$('#yt-looper-end').remove();
$('#yt-looper').remove();
$('#yt-looper-interval').remove();
var $button = $('<button id="yt-looper" class="yt-uix-button yt-uix-button-opacity yt-uix-button-has-icon yt-uix-tooltip" title="Open in yt-looper"><span style="margin:0 0.1em;font-size:2.25em;vertical-align: middle;">↻</span> yt.aergia.eu </button>').hide();
var $intervals = $('<span id="yt-looper-interval"></span>').hide();
var input = '<input class="yt-uix-form-input-text title-input" style="width:50px!important;margin-right:0.25em;" />';
var $secondaryActions = $('div.watch-action-buttons > div.watch-secondary-actions');
var start = humanize(0);
var end = humanize(getYtPlayer().getDuration());
var $start = $(input).attr('id', 'yt-looper-start').val(start).attr('title', 'Start of loop');
var $end = $(input).attr('id', 'yt-looper-end').val(end).attr('title', 'End of loop');
$secondaryActions.prepend($button);
$intervals.append($start).append($end).insertBefore($button);
$button.show();
$button.click(function () {
var url = window.location.href;
url = url.replace(/.*youtube.com\/watch/, 'https://yt.aergia.eu/');
if (getYtPlayer().getPlayerState() === 2) {
url = url.replace(/[&#]t=[^&#]*/g, '');
url = url + '#t=' + $('#yt-looper-start').val() + ';' + $('#yt-looper-end').val();
}
window.open(url);
});
};
var initButton = function() {
window.onYouTubePlayerReady = function () {
console.log('yt-looper @ onYouTubePlayerReady');
var newVideo = true;
window.ytPlayerStateChanged = function (state) {
if (state === 2) {
$('#yt-looper-start').val(humanize(getYtPlayer().getCurrentTime()));
$('#yt-looper-interval').fadeIn('slow');
} else if (state === 1) {
// youtube redraws div.watch-action-buttons AFTER 5 and -1 events
// and 1 is the only one we have after GUI stabilizes
// ANGST.
newVideo = true;
} else {
$('#yt-looper-interval').hide();
$('#yt-looper-start').val(humanize(0));
}
if (newVideo) {
renderLooperActions();
newVideo = false;
}
};
renderLooperActions();
getYtPlayer().addEventListener('onStateChange', 'ytPlayerStateChanged');
};
};
initButton();
};
var imgurHandler = function () {
var $oldButton = $('li#yt-looper');
var imgurId = window.location.pathname.replace(/\/(?:[^\/]+\/)*/, '');
var renderButton = function() {
// display button only on single-image pages
if (/[a-zA-Z0-9]+/.test(imgurId) && ($('div.post-image img').length === 1 || $('div.post-image video').length === 1)) {
console.log('yt-looper @ imgurHandler('+imgurId+')');
var url = 'https://yt.aergia.eu/#i=' + imgurId;
var html = '<a style="min-width:28px;min-height:26px;padding:12px 6px;pointer:cursor"><span style="font-size:2em;vertical-align:middle;line-height:26px">↻</span> yt.aergia.eu</a>';
var $a = $(html).click(function () {
window.open(url);
});
var $newButton = $('<li id="yt-looper">').append($a).data('imgurId', imgurId);
$('#main-nav ul').append($newButton);
}
};
if ($oldButton.length === 0 || $oldButton.data('imgurId') != imgurId) {
$oldButton.remove();
renderButton();
}
};
var soundCloudHandler = function () {
if ($('#yt-looper').length === 0) {
var $soundActions = $('div.l-about-top div.soundActions');
if ($soundActions.length === 1) {
console.log('soundCloudHandler(): adding yt-looper button..');
var scId = window.location.pathname.replace(/^\//, '');
var url = 'https://yt.aergia.eu/#s=' + scId;
var $button = $('<button id="yt-looper" class="sc-button sc-button-medium" title="Open in yt-looper"><span style="font-weight: 900;">↻</span> yt.aergia.eu</button>');
$button.click(function () {
window.open(url);
});
$('div.sc-button-group', $soundActions).first().append($button);
}
}
};
switch (window.location.hostname.replace('www.', '')) {
case 'youtube.com':
youtubeHandler();
break;
case 'imgur.com':
window.setInterval(imgurHandler, 1000); // quick hack for reactive gui
break;
case 'soundcloud.com':
window.setInterval(soundCloudHandler, 1000); // same
break;
}
}(window.jQuery.noConflict(true)));
// vim:ts=2:sw=2:et: