// ==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
// @downloadURL https://update.greasyfork.icu/scripts/26523/plswatch.user.js
// @updateURL https://update.greasyfork.icu/scripts/26523/plswatch.meta.js
// ==/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 = $('');
$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 = '↻
yt.looper';
const $a = $(html).click(function () {
window.open(url);
});
$('