Skip to content

Instantly share code, notes, and snippets.

@tokland
Created April 2, 2022 16:01
Show Gist options
  • Save tokland/cb0ce96b16ac81f58fb5eae22fb9902c to your computer and use it in GitHub Desktop.
Save tokland/cb0ce96b16ac81f58fb5eae22fb9902c to your computer and use it in GitHub Desktop.
Add forward/backward buttons to RAC1 A la Carta web player [tampermonkey] [greasemonkey] [userscripts]
// ==UserScript==
// @name rac1-alacarta
// @description Add back/forward buttons to audio player
// @namespace https://github.com/tokland
// @homepage https://github.com/cvzi/rollup-userscript-template
// @author tokland
// @match https://www.rac1.cat/a-la-carta*
// @icon https://www.google.com/s2/favicons?sz=64&domain=rac1.cat
// @run-at document-start
// @version 0.0.1
// @license MIT
// @grant GM.getValue
// ==/UserScript==
/*
MIT License
Copyright (c) 2020 cvzi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
(function () {
'use strict';
const pageWindow = unsafeWindow;
function interceptPlayer() {
const AudioOriginal = pageWindow.Audio;
pageWindow.Audio = function () {
const Klass = Function.prototype.bind.apply(AudioOriginal, arguments);
const player = new Klass();
if (!pageWindow.players)
pageWindow.players = [];
pageWindow.players.push(player);
return player;
};
pageWindow.Audio.prototype = Object.create(AudioOriginal.prototype);
}
function addButtons() {
const button = document.querySelector('.player-rac-icons.fa-play');
button.style.clear = 'right';
createButton({ button, className: 'fa-backward', seekTime: -10, insert: 'before' });
createButton({ button, className: 'fa-forward', seekTime: +10, insert: 'after' });
}
function createButton(options) {
const { button } = options;
const newButton = button.cloneNode();
if (options.insert === 'after') {
button.parentNode.insertBefore(newButton, button.nextSibling);
newButton.style.clear = 'right';
}
else {
button.parentNode.insertBefore(newButton, button);
newButton.style.clear = 'both';
}
newButton.removeAttribute('data-player-play-button');
newButton.classList.remove('fa-play');
newButton.classList.add(options.className);
newButton.addEventListener('click', () => {
if (!pageWindow.players)
alert('no player');
pageWindow.players.forEach(player => {
player.currentTime = player.currentTime + options.seekTime;
});
});
}
function runRac1AlaCarta() {
window.addEventListener('load', addButtons);
interceptPlayer();
}
runRac1AlaCarta();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment