Get Twitch emojis

under construction ...

当前为 2021-11-25 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Get Twitch emojis
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.3
  5. // @description under construction ...
  6. // @author Lee-7723
  7. // @match https://www.twitch.tv/*
  8. // @exclude https://www.twitch.tv/
  9. // @icon https://static.twitchcdn.net/assets/favicon-16-52e571ffea063af7a7f4.png
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. 'use strict';
  14.  
  15. function sleep (time) {
  16. return new Promise((resolve) => setTimeout(resolve, time));
  17. }
  18.  
  19. var main;
  20. var emoji_btn;
  21. var all_emojis = new Array();
  22. var emojis;
  23. var load_status;
  24. var img_srcset = new Array();
  25. var img_alt = new Array();
  26.  
  27.  
  28.  
  29. get_emoji_after_button ()
  30. async function get_emoji_after_button () {//加载出表情按钮之后
  31. await get_button()
  32. //await get_emoji()
  33. //console.log('loaded' + emoji_btn.getElementsByTagName('div')[0])
  34. main = document.getElementsByTagName('body')[0];
  35. emoji_btn.onclick = function(){output()}//给聊天区的表情按钮添加功能,指向output()
  36. }
  37.  
  38. //set_btn_click ()
  39. //async function set_btn_click () {
  40. // emoji_btn.getElementBysTagName('div')[0].onclick = "function(){alert('clicked')}"
  41. // console.log('2nd' + emoji_btn)}
  42. //}
  43.  
  44. async function get_button() {//监视获取聊天区的表情按钮
  45. 'use strict';
  46. for (var i = 0; !emoji_btn; i) {
  47. await sleep(1000);
  48. emoji_btn = document.querySelectorAll('button[data-a-target="emote-picker-button"]')[0];
  49. //console.log(emoji_btn)
  50. }
  51. }
  52.  
  53. async function get_emoji() {//获取表情名img_alt,及表情链接img_srcset
  54. for (var i = 0; !emojis; i) {
  55. console.log('starting to fetch emojis')
  56. await sleep(1000);
  57. all_emojis = document.getElementsByClassName('Layout-sc-nxg1ff-0 lmlxil');
  58. if(all_emojis.length == 1) emojis = all_emojis[0];
  59. else emojis = all_emojis[1]//.getElementsByTagName("img");
  60. //for (var j = 0; j < emojis.length; j++){
  61. // console.log(emojis[j].srcset)
  62. //}
  63. if (emojis) {
  64. emojis = emojis.getElementsByTagName("img")
  65. for (var j = 0; j < emojis.length; j++) {
  66. if (j == 1) {
  67. img_srcset[j-1] = emojis[j].srcset.replace(' 3.0x','').split(" 2.0x,")[1];
  68. } else {
  69. img_srcset[j-1] = emojis[j].srcset.replace(' 3.0x','').split(" 2.0x, ")[1];
  70. }
  71. img_alt[j-1] = emojis[j].alt;
  72. //if (j != 0) console.log(img_alt[j-1])
  73. }
  74. //console.log(emojis)
  75. }
  76. }
  77. }
  78.  
  79.  
  80. async function output() {
  81. await get_emoji();//等待获取表情完成
  82. var head;
  83. if(all_emojis.length == 1) head = all_emojis[0].children[0];//未登录状态表情区只有一个分类
  84. else head = all_emojis[1].children[0];//登录状态选择表情区第二个分类
  85. console.log(head)
  86. var log_button = document.createElement('div');
  87. log_button.className = "Layout-sc-nxg1ff-0 jreOmo";
  88. log_button.id = 'emoji_url_pop_box';
  89. log_button.innerHTML = `<div class="Layout-sc-nxg1ff-0 emWtQg">
  90. <div style="display: inherit;">
  91. <button id='emoji_url_pop' class="ScCoreButton-sc-1qn4ixc-0 jGqsfG ScButtonIcon-sc-o7ndmn-0 fNzXyu">
  92. <svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="currentcolor">
  93. <path d="M0 0h24v24H0V0z" fill="none"/><path d="M19 9h-4V3H9v6H5l7 7 7-7zm-8 2V5h2v6h1.17L12 13.17 9.83 11H11zm-6 7h14v2H5z"/></svg>
  94. </svg>
  95. </button>
  96. </div>
  97. </div>`;
  98. if(!document.getElementById('emoji_url_pop'))head.insertBefore(log_button , head.children[1])
  99. document.getElementById('emoji_url_pop').onclick=function(){pop_up()}
  100. function pop_up () {
  101. main.append(output_box)
  102. }
  103.  
  104.  
  105. var output_box = document.createElement("div")
  106. output_box.style = `position: absolute;
  107. top: 5rem; left: 25%;
  108. padding: 10px;
  109. z-index: 9999;
  110. height: 70%;
  111. width: 50%;
  112. background-color: var(--color-background-alt);
  113. border-radius: 10px;
  114. border: 1.6px solid var(--color-background-pill);
  115. box-shadow: 4px 4px 10px #000a;`;
  116. output_box.id = "emoji_url_box";
  117.  
  118. var output_text = document.createElement("textarea")
  119. output_text.style = `height: 85%; width: 100%;
  120. resize: none;
  121. padding: 12px;
  122. background-color: var(--color-background-input);
  123. color: var(--color-text-input);
  124. margin: 2px 2px 5px;
  125. display: flex;`;
  126. output_text.id = "emoji_url_text";
  127.  
  128. var output_author = document.createElement("p")
  129. output_author.style = `color: #888; margin-left: 10px; position: absolute`;
  130. output_author.innerHTML = `script by Lee-7723`;
  131.  
  132. var output_close_btn = document.createElement("button")
  133. output_close_btn.style = `position: absolute; right: 8px; bottom: 8px; width: 50px; border-radius: 6px;`;
  134. output_close_btn.className = "ScCoreButton-sc-1qn4ixc-0 ScCoreButtonPrimary-sc-1qn4ixc-1 jGqsfG ksFrFH";
  135. output_close_btn.id = "emoji_url_close_btn";
  136. output_close_btn.onclick = function () {close_emoji_box()};
  137. output_close_btn.appendChild(document.createTextNode('关闭'))
  138.  
  139. var preview_btn = document.createElement("button")
  140. preview_btn.style = `position: absolute; right: 66px; bottom: 8px; width: 50px; border-radius: 6px; `;
  141. preview_btn.className = "ScCoreButton-sc-1qn4ixc-0 ScCoreButtonPrimary-sc-1qn4ixc-1 jGqsfG ksFrFH";
  142. preview_btn.id = "emoji_url_preview_btn";
  143. preview_btn.onclick = function () {preview_emoji_box()};
  144. preview_btn.appendChild(document.createTextNode('预览'))
  145.  
  146. var usage = document.createElement("p")
  147. usage.style = `text-color: #FFF; margin-bottom: 10px; margin-left: 10px; font-weight: bold;`;
  148. usage.appendChild(document.createTextNode('将下面文本框中内容 全选->复制,粘贴到emoji.txt中'))
  149. usage.id = "function_readme";
  150.  
  151. for (var i = 0; i < img_srcset.length; i++) {
  152. output_text.appendChild(document.createTextNode(img_alt[i] + ' ' + img_srcset[i] + '\n'))
  153. }
  154. output_box.append(usage,output_text,output_author,preview_btn,output_close_btn)
  155. }
  156.  
  157. function close_emoji_box() {
  158. document.getElementById('emoji_url_box').remove()//document.getElementById('emoji_url_box')
  159. document.getElementById('emoji_url_pop_box').remove()
  160. }
  161.  
  162. function preview_emoji_box() {
  163. document.getElementById('emoji_url_text').style.display = 'none';
  164. var preview_box = document.createElement("div")
  165. preview_box.style = `height: 85%; width: 100%;
  166. resize: none; margin: 2px 2px 5px;
  167. padding: 12px;
  168. background-color: var(--color-background-input);
  169. color: var(--color-text-input);
  170. border: 1px solid rgb(118, 118, 118);
  171. border-radius: 2px;
  172. overflow-y: scroll;
  173. display: flex;
  174. flex-wrap: wrap;
  175. justify-content: center;`;
  176. preview_box.id = "emoji_url_preview";
  177.  
  178. for (var j = 0; j < img_srcset.length; j++) {
  179. var pics = document.createElement("a")
  180. var pic_img =`<img src=${img_srcset[j]} alt=${img_alt[j]} style="margin: 5px 5px 1px;
  181. border: 2px solid #FFF;
  182. border-radius: 4px;
  183. width: 112px; height: 112px;">
  184. <p style="text-align: center; margin-bottom: 3px">${img_alt[j]}</p>`;
  185. pics.innerHTML = pic_img;
  186. pics.href = img_srcset[j];
  187. pics.target = 'view emoji';
  188. pics.download = img_alt[j];
  189. pics.style.cssText = 'width: 122px; overflow: hidden;'
  190. preview_box.appendChild(pics)
  191. }
  192.  
  193. document.getElementById('emoji_url_box').insertBefore(preview_box , document.getElementById('emoji_url_box').getElementsByTagName('p')[1])
  194. document.getElementById("emoji_url_preview_btn").innerHTML = '返回';
  195. document.getElementById("function_readme").innerHTML = '下列表情点击可跳转,可以手动保存';
  196. document.getElementById("emoji_url_preview_btn").onclick = function () {return_emoji_url_box()};
  197. }
  198.  
  199. function return_emoji_url_box() {
  200. document.getElementById('emoji_url_text').style.display = 'flex';
  201. document.getElementById('emoji_url_preview').style.display = 'none';
  202. document.getElementById("emoji_url_preview_btn").innerHTML = '预览';
  203. document.getElementById("function_readme").innerHTML = '将下面文本框中内容 全选->复制,粘贴到emoji.txt中';
  204. document.getElementById("emoji_url_preview_btn").onclick = function () {return_emoji_preview_box()};
  205. }
  206.  
  207. function return_emoji_preview_box() {
  208. document.getElementById('emoji_url_text').style.display = 'none';
  209. document.getElementById('emoji_url_preview').style.display = 'flex';
  210. document.getElementById("emoji_url_preview_btn").innerHTML = '返回';
  211. document.getElementById("function_readme").innerHTML = '下列表情点击可跳转,可以手动保存';
  212. document.getElementById("emoji_url_preview_btn").onclick = function () {return_emoji_url_box()};
  213. }