Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Read Everything
- // @namespace Violentmonkey Scripts
- // @include *
- // @grant none
- // @version 1.0
- // @author -
- // ==/UserScript==
- // requires voicevox to be running, eg
- // C:\Users\<youruser>\AppData\Local\Programs\VOICEVOX\run.exe --host localhost --cors_policy_mod all
- const host = 'http://localhost:50021'
- const speaker = 0
- document.body.addEventListener("keydown", (e) => {
- if (!(e.shiftKey && e.altKey)) return
- const text = getSelectionText()
- if (!text) return
- play(text)
- })
- const play = async (text) => {
- const audioQuery = await fetch(`${host}/audio_query?text=${text}&speaker=${speaker}`, {
- "method": "POST"
- })
- const meta = await audioQuery.json()
- const maybeWav = await fetch(`${host}/synthesis?speaker=${speaker}`, {
- "headers": {
- "content-type": "application/json",
- },
- "body": JSON.stringify(meta),
- "method": "POST",
- })
- const buffer = await maybeWav.arrayBuffer()
- const audioContext = new(window.AudioContext || window.webkitAudioContext)()
- const decodedAudio = await audioContext.decodeAudioData(buffer)
- const gain = audioContext.createGain()
- gain.connect(audioContext.destination)
- const playSound = audioContext.createBufferSource()
- playSound.buffer = decodedAudio
- playSound.connect(gain)
- playSound.start(0)
- }
- const getSelectionText = () => {
- let text = ""
- if (window.getSelection) {
- text = window.getSelection().toString()
- } else if (document.selection && document.selection.type != "Control") {
- text = document.selection.createRange().text
- }
- return text;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement