[YouTube] Auto add Text in ChatBox

Auto add Text in ChatBox.

Fra og med 25.11.2022. Se den nyeste version.

Dette script bør ikke installeres direkte. Det er et bibliotek, som andre scripts kan inkludere med metadirektivet // @require https://update.greatest.deepsurf.us/scripts/455302/1121194/%5BYouTube%5D%20Auto%20add%20Text%20in%20ChatBox.js

// ==UserScript==
// @name         [YouTube] Auto add Text in ChatBox
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  Auto add Text in ChatBox.
// @author       You
// @include        https://www.youtube.com/watch*
// @include        https://www.youtube.com/live_chat*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// ==/UserScript==
const chat = document.querySelector("#input > #input")
addText("#")

chat.addEventListener("keyup", e => {

	if(e.key == "Enter"){
		//テキストボックスに指定した文字を追加。
		addText("#")
	}

})

/**
 *@Description 自動でチャットボックスにテキストを追加
*/
function addText(text){

		document.querySelector('#input').setAttribute("has-text" , "")
		chat.setAttribute("aria-invalid" , "")
		chat.textContent = text
		moveEndCaret(chat)

}

/**
 *@Description キャレットの位置を文末に変更
*/
function moveEndCaret(textBox){

	const selection = window.getSelection()
	const range = document.createRange()
	const offset = textBox.innerText.length
	range.setStart(textBox.firstChild, offset)
	range.setEnd(textBox.firstChild, offset)
	selection.removeAllRanges()
	selection.addRange(range)

}