[YouTube] Auto add Text in ChatBox

Auto add Text in ChatBox.

当前为 2022-11-23 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/455302/1120373/%5BYouTube%5D%20Auto%20add%20Text%20in%20ChatBox.js

  1. // ==UserScript==
  2. // @name [YouTube] Auto add Text in ChatBox
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Auto add Text in ChatBox.
  6. // @author You
  7. // @include https://www.youtube.com/watch*
  8. // @include https://www.youtube.com/live_chat*
  9. // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. const player = document.getElementById("movie_player")
  14.  
  15. /**
  16. *@Description チャットエリアが表示された後にチャットエリアのiframe内にkeydownイベントを追加する。
  17. */
  18. let getChatInterval = setTimeout( ()=> {
  19. let chat = document.getElementById("chatframe")
  20.  
  21. if(chat != null && chat.contentWindow.document.querySelector('#input') != null || document.querySelector('#input') != null){
  22. if(document.querySelector('#input') != null){
  23. chat = document.querySelector('#input')
  24. }else{
  25. chat = chat.contentWindow
  26. }
  27. addText("#")
  28.  
  29. chat.addEventListener("keydown", e => {
  30.  
  31. if(e.key == "Enter"){
  32.  
  33. //フォーカスが当てられている要素のIDを取得
  34. let activeElementId
  35.  
  36. //Elementsタブからiframeタグ内を参照している場合はwindowの参照先もiframe内になるので判定する。
  37. if(document.getElementById("chatframe") != null){
  38. activeElementId = document.getElementById("chatframe").contentWindow.document.activeElement.id
  39. }else{
  40. activeElementId = document.activeElement.id
  41. }
  42.  
  43. //テキストボックスに指定した文字を追加。
  44. if(activeElementId == "input"){
  45. setTimeout( () => addText("#"))
  46. }
  47. }
  48. })
  49.  
  50. }
  51. },5000)
  52.  
  53. /**
  54. *@Description キャレットの位置を文末に変更
  55. */
  56. function addText(text){
  57.  
  58. //Elementsタブからiframeタグ内を参照している場合はwindowの参照先もiframe内になるので判定する。
  59. if(document.getElementById("chatframe") != null){
  60. const chat = document.getElementById("chatframe").contentWindow
  61. chat.document.querySelector('#input').setAttribute("has-text" , "")
  62. chat.document.querySelector('#input').querySelector('#input').setAttribute("aria-invalid" , "")
  63. chat.document.querySelector('#input').querySelector('#input').textContent = text
  64. moveEndCaret(chat.document.querySelector('#input').querySelector('#input') , chat)
  65.  
  66. }else{
  67. document.querySelector('#input').setAttribute("has-text" , "")
  68. document.querySelector('#input').querySelector('#input').setAttribute("aria-invalid" , "")
  69. document.querySelector('#input').querySelector('#input').textContent = text
  70. moveEndCaret(document.querySelector('#input').querySelector('#input'))
  71.  
  72. }
  73. }
  74.  
  75. /**
  76. *@Description キャレットの位置を文末に変更
  77. */
  78. function moveEndCaret(textBox , iframewindow = window){
  79. const selection = iframewindow.getSelection()
  80. const range = document.createRange()
  81. const offset = textBox.innerText.length
  82. range.setStart(textBox.firstChild, offset)
  83. range.setEnd(textBox.firstChild, offset)
  84. selection.removeAllRanges()
  85. selection.addRange(range)
  86. }