-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpls.watch.user.js
126 lines (120 loc) · 5.09 KB
/
pls.watch.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
// ==UserScript==
// @name pls.watch
// @description Adds a button on YouTube, Imgur and SoundCloud to open current resource in pls.watch looper
// @version 1.9.2
// @namespace https://pls.watch
// @icon https://ipfs.io/ipfs/QmZFXPq9xMJY3Z8q2fq4wfsU93uTpVfjbiaYzwFmfnkCfM
// @match https://www.youtube.com/*
// @match https://youtube.com/*
// @match http://www.youtube.com/*
// @match http://youtube.com/*
// @match https://imgur.com/*
// @match http://imgur.com/*
// @match http://soundcloud.com/*
// @match https://soundcloud.com/*
// @license CC0; https://creativecommons.org/publicdomain/zero/1.0/
// @homepageURL https://github.com/lidel/pls.watch/#companion-userscript
// @supportURL https://github.com/lidel/pls.watch/issues
// @require https://cdn.jsdelivr.net/jquery/3.2.1/jquery.slim.min.js
// @grant none
// @run-at document-end
// @noframes
// ==/UserScript==
'use strict';
(function ($, undefined) { // eslint-disable-line no-unused-vars
const youtubeHandler = function () {
const atWatchPage = window.location.pathname.startsWith('/watch');
if (atWatchPage && $('#pls-watch').length === 0) {
const getYtPlayer = function () {
return window.document.getElementById('movie_player');
};
const renderLooperActions = function() {
console.log('renderLooperActions()');
$('#pls-watch').remove();
// new layout (https://www.youtube.com/new)
const $secondaryActions = $('#info #top-level-buttons');
const $button = $('<button id="pls-watch" style="cursor:pointer;background:none;border:none;vertical-align:middle;" title="Open in pls.watch"><span style="margin:0 0.1rem;vertical-align: middle;font-size:2em;" class="yt-view-count-renderer"> ↻ </span></button>');
$secondaryActions.append($button);
$button.show();
$button.click(function () {
let url = window.location.href;
url = url.replace(/.*youtube.com\/watch\?/, 'https://pls.watch/#');
url = url.replace(/[&#]t=[^&#]*/g, '');
window.open(url);
});
};
const initButton = function() {
console.log('initButton()');
renderLooperActions();
window.ytLooperStateChanged = function(state) {
console.log('ytLooperStateChanged().state', state);
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.
renderLooperActions();
}
};
getYtPlayer().addEventListener('onStateChange', window.ytLooperStateChanged);
};
initButton();
}
};
const imgurHandlerRenderButton = function(ids) {
const id = ids[0];
if (/[a-zA-Z0-9]+/.test(id) && ($('div.post-image img').length > 0 || $('div.post-image video').length > 0)) {
console.log('pls.watch → renderButton for ' + ids.join(', '));
const url = 'https://pls.watch/#i=' + ids.join('&i=');
const html = '<a style="pointer:cursor;text-decoration:none;color:#ccc" title="Open in pls.watch"><span style="font-size:1.5em">↻</span><br>yt.looper</a>';
const $a = $(html).click(function () {
window.open(url);
});
$('<div class="post-account" id="pls-watch">')
.data('imgurId', id)
.css('margin-left','1em')
.css('float', 'right')
.css('text-align', 'center')
.append($a)
.hide()
.prependTo($('div.post-header'))
.show();
}
};
const imgurHandler = function () {
const $oldButton = $('#pls-watch');
const imgurIds = $('div.post-image-container').map(function() { return $(this).attr('id'); }).get();
if (imgurIds.length > 0 && $oldButton.data('imgurId') != imgurIds[0]) {
$oldButton.remove();
imgurHandlerRenderButton(imgurIds);
}
};
const soundCloudHandler = function () {
if ($('#pls-watch').length === 0) {
const $soundActions = $('div.l-about-top div.soundActions');
if ($soundActions.length === 1) {
console.log('soundCloudHandler(): adding pls.watch button..');
const scId = window.location.pathname.replace(/^\//, '');
const url = 'https://pls.watch/#s=' + scId;
const $button = $('<button id="pls-watch" class="sc-button sc-button-medium" title="Open in pls.watch"><span style="font-weight: 900;">↻</span> pls.watch</button>');
$button.click(function () {
window.open(url);
});
$('div.sc-button-group', $soundActions).first().append($button);
}
}
};
// since everyone does reactive gui, we just poll to see if elements are present/gone
console.log('pls.watch userscript loaded');
switch (window.location.hostname.replace('www.', '')) {
case 'youtube.com':
window.setInterval(youtubeHandler, 1000);
break;
case 'imgur.com':
window.setInterval(imgurHandler, 1000);
break;
case 'soundcloud.com':
window.setInterval(soundCloudHandler, 1000);
break;
}
}(window.jQuery.noConflict(true)));
// vim:ts=2:sw=2:et: