Compare commits
	
		
			No commits in common. "9eb0976b355a2b7a1558da57dd086dfc801cbf0d" and "87f2833a3a8fac2a84d4c5512953e6325e135a9d" have entirely different histories.
		
	
	
		
			9eb0976b35
			...
			87f2833a3a
		
	
		
					 1 changed files with 9 additions and 30 deletions
				
			
		
							
								
								
									
										39
									
								
								src/main.ts
									
									
									
									
									
								
							
							
						
						
									
										39
									
								
								src/main.ts
									
									
									
									
									
								
							| 
						 | 
					@ -2,33 +2,13 @@ const emote_regex = /bttv:([^\s]+)/g;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Emote = {
 | 
					type Emote = {
 | 
				
			||||||
  code: string;
 | 
					  code: string;
 | 
				
			||||||
  url?: string;
 | 
					  url: string;
 | 
				
			||||||
};
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type BTTV_Emote = {
 | 
					 | 
				
			||||||
  id: string;
 | 
					 | 
				
			||||||
  code: string;
 | 
					 | 
				
			||||||
  imageType: string;
 | 
					 | 
				
			||||||
  animated: boolean;
 | 
					 | 
				
			||||||
  user: any;
 | 
					 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function bttv_get_url(code: string): Promise<Emote> {
 | 
					async function bttv_get_url(code: string): Promise<Emote> {
 | 
				
			||||||
  const res = await fetch(
 | 
					 | 
				
			||||||
    `https://api.betterttv.net/3/emotes/shared/search?query=${code}&offset=0&limit=50`,
 | 
					 | 
				
			||||||
  );
 | 
					 | 
				
			||||||
  const content = await res.json();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  if (!Array.isArray(content)) return { code: code };
 | 
					 | 
				
			||||||
  const matches = (content as BTTV_Emote[]).filter(
 | 
					 | 
				
			||||||
    (be) => be.code.toLowerCase() == code.toLowerCase(),
 | 
					 | 
				
			||||||
  );
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  if (matches.length <= 0) return { code: code };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  return {
 | 
					  return {
 | 
				
			||||||
    code: code,
 | 
					    code: code,
 | 
				
			||||||
    url: `https://cdn.betterttv.net/emote/${matches[0].id}/1x.${matches[0].imageType}`,
 | 
					    url: `result_${code}`,
 | 
				
			||||||
  };
 | 
					  };
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -39,12 +19,11 @@ async function get_emotes(codes: Iterable<string>): Promise<EmoteMap> {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // queue emote jobs
 | 
					  // queue emote jobs
 | 
				
			||||||
  const queue = [...codes_set].map((code) => bttv_get_url(code));
 | 
					  const queue = [...codes_set].map((code) => bttv_get_url(code));
 | 
				
			||||||
  console.log("[lmlfc-bttv] queueing", queue.length, "jobs");
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // run
 | 
					  // run
 | 
				
			||||||
  const result: EmoteMap = {};
 | 
					  const result: EmoteMap = {};
 | 
				
			||||||
  for (const job of await Promise.all(queue)) {
 | 
					  for (const job of await Promise.all(queue)) {
 | 
				
			||||||
    if (job.url) result[job.code] = job.url;
 | 
					    result[job.code] = job.url;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return result;
 | 
					  return result;
 | 
				
			||||||
| 
						 | 
					@ -57,16 +36,14 @@ async function process_text(text: string): Promise<string> {
 | 
				
			||||||
  for (const emote of matches.reverse()) {
 | 
					  for (const emote of matches.reverse()) {
 | 
				
			||||||
    const [match, code] = emote;
 | 
					    const [match, code] = emote;
 | 
				
			||||||
    const index = emote.index ?? 0;
 | 
					    const index = emote.index ?? 0;
 | 
				
			||||||
    if (!(code in emotes)) continue;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    text =
 | 
					    text =
 | 
				
			||||||
      text.substring(0, index) +
 | 
					      text.substring(0, index) +
 | 
				
			||||||
      `[not]${emotes[code]}[/not]` +
 | 
					      emotes[code] +
 | 
				
			||||||
      text.substring(index + match.length);
 | 
					      text.substring(index + match.length);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  text = text.replace(/\[\/not\]\s+\[not\]/g, "[/not][not]");
 | 
					  console.log(text);
 | 
				
			||||||
 | 
					 | 
				
			||||||
  return text;
 | 
					  return text;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -87,8 +64,10 @@ async function process_text(text: string): Promise<string> {
 | 
				
			||||||
      const cb_input = document.querySelector("#mgc_cb_evo_input");
 | 
					      const cb_input = document.querySelector("#mgc_cb_evo_input");
 | 
				
			||||||
      if (!(cb_input instanceof HTMLInputElement)) return;
 | 
					      if (!(cb_input instanceof HTMLInputElement)) return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      cb_input.value = await process_text(cb_input.value);
 | 
					      await process_text("bttv:foo bttv:bar bttv:foo");
 | 
				
			||||||
      cb_input.focus();
 | 
					
 | 
				
			||||||
 | 
					      // await process_text(cb_input.value);
 | 
				
			||||||
 | 
					      // cb_input.focus();
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    btn.appendChild(img);
 | 
					    btn.appendChild(img);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue