Forked from SeanMcP/increase-audio-playback.user.js
Last active
September 7, 2021 16:42
-
-
Save oasisfeng/f1cb1fd09d7767fb433c0fc01650e569 to your computer and use it in GitHub Desktop.
🎧 Increase the playback speed of audio elements around the web
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Increase audio playback | |
// @namespace https://seanmcp.com | |
// @version 0.1 | |
// @description Increase the playback speed of audio elements around the web | |
// @author Sean McPherson, forked by Oasis Feng | |
// @match *://*/* | |
// @require https://cdn.jsdelivr.net/gh/CoeJoder/[email protected]/waitForKeyElements.js | |
// @grant GM_addStyle | |
// ==/UserScript== | |
waitForKeyElements ("audio", applyAPR); | |
function applyAPR(audioEl) { | |
'use strict'; | |
function formateRate(rate) { | |
const [number, decimal = '0'] = String(rate).split('.') | |
return number + '.' + decimal.padEnd(2, '0') | |
} | |
if (audioEl) { | |
const defaultRate = '1.50' | |
audioEl.playbackRate = defaultRate | |
audioEl.defaultPlaybackRate = defaultRate | |
if (document.getElementById('__playback-rate')) return | |
const controls = document.createElement('aside') | |
controls.id = '__playback-rate' | |
const span = document.createElement('span') | |
span.textContent = defaultRate + 'x' | |
const input = document.createElement('input') | |
input.setAttribute('aria-label', 'audio playback rate') | |
input.type = "range" | |
input.min = 1.0 | |
input.max = 2.0 | |
input.step = 0.25 | |
input.defaultValue = defaultRate | |
input.addEventListener('change', ({ target: { valueAsNumber } }) => { | |
audioEl.playbackRate = valueAsNumber | |
const [number, decimal = '0'] = String(valueAsNumber).split('.') | |
span.textContent = number + '.' + decimal.padEnd(2, '0') + 'x' | |
}) | |
controls.appendChild(input) | |
controls.appendChild(span) | |
const style = document.createElement('style') | |
style.innerHTML = ` | |
#__playback-rate { | |
position: fixed; | |
bottom: 30; | |
left: 0.3rem; | |
background: white; | |
display: flex; | |
} | |
#__playback-rate input { | |
margin: 0.1rem; | |
width: 5rem; | |
} | |
#__playback-rate span { | |
color: black; | |
font-size: 0.3rem; | |
font-weight: bold; | |
padding: 0.5rem; | |
} | |
` | |
controls.appendChild(style) | |
const container = document.getElementsByClassName("music-control")[0] | |
if (container) container.appendChild(controls) | |
else document.body.appendChild(controls) | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment