[YouTube] Auto add Text in ChatBox

Auto add Text in ChatBox.

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.greasyfork.org/scripts/455302/1121818/%5BYouTube%5D%20Auto%20add%20Text%20in%20ChatBox.js

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

Bạn sẽ cần cài đặt một tiện ích mở rộng như Tampermonkey hoặc Violentmonkey để cài đặt kịch bản này.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         [YouTube] Auto add Text in ChatBox
// @namespace    http://tampermonkey.net/
// @version      0.5
// @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")
if(localStorage.getItem('enable-words-typing') != 'false'){
	addText(localStorage.getItem('add-text') == null ? '#' : localStorage.getItem('add-text'))
}

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

	if(e.key == "Enter" && htmlSetUp && localStorage.getItem('enable-words-typing') != 'false'){
		//テキストボックスに指定した文字を追加。
		addText(htmlSetUp.addTextOption.value == null ? '#' : htmlSetUp.addTextOption.value)
	}

})

/**
 *@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)

}