[YouTube] Auto add Text in ChatBox

Auto add Text in ChatBox.

Tính đến 23-11-2022. Xem phiên bản mới nhất.

Script này sẽ không được không được cài đặt trực tiếp. Nó là một thư viện cho các script khác để bao gồm các chỉ thị meta // @require https://update.greatest.deepsurf.us/scripts/455302/1120373/%5BYouTube%5D%20Auto%20add%20Text%20in%20ChatBox.js

// ==UserScript==
// @name         [YouTube] Auto add Text in ChatBox
// @namespace    http://tampermonkey.net/
// @version      0.1
// @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 player = document.getElementById("movie_player")

/**
*@Description チャットエリアが表示された後にチャットエリアのiframe内にkeydownイベントを追加する。
*/
let getChatInterval = setTimeout( ()=> {
	let chat = document.getElementById("chatframe")

	if(chat != null && chat.contentWindow.document.querySelector('#input') != null || document.querySelector('#input') != null){
		if(document.querySelector('#input') != null){
			chat = document.querySelector('#input')
		}else{
			chat = chat.contentWindow
		}
		addText("#")

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

			if(e.key == "Enter"){

				//フォーカスが当てられている要素のIDを取得
				let activeElementId

				//Elementsタブからiframeタグ内を参照している場合はwindowの参照先もiframe内になるので判定する。
				if(document.getElementById("chatframe") != null){
					activeElementId = document.getElementById("chatframe").contentWindow.document.activeElement.id
				}else{
					activeElementId = document.activeElement.id
				}

				//テキストボックスに指定した文字を追加。
				if(activeElementId == "input"){
					setTimeout( () => addText("#"))
				}
			}
		})

	}
},5000)

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

	//Elementsタブからiframeタグ内を参照している場合はwindowの参照先もiframe内になるので判定する。
	if(document.getElementById("chatframe") != null){
		const chat = document.getElementById("chatframe").contentWindow
		chat.document.querySelector('#input').setAttribute("has-text" , "")
		chat.document.querySelector('#input').querySelector('#input').setAttribute("aria-invalid" , "")
		chat.document.querySelector('#input').querySelector('#input').textContent = text
		moveEndCaret(chat.document.querySelector('#input').querySelector('#input') , chat)

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

	}
}

/**
 *@Description キャレットの位置を文末に変更
*/
function moveEndCaret(textBox , iframewindow = window){
	const selection = iframewindow.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)
}