Picarto Markup

Enable markups in Picarto chats

当前为 2018-05-06 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Picarto Markup
  3. // @description Enable markups in Picarto chats
  4. // @include *//picarto.tv/*
  5. // @version 1
  6. // @grant none
  7. // @license MIT
  8. // @namespace https://greasyfork.org/users/184200
  9. // ==/UserScript==
  10.  
  11. function markup(str) {
  12.  
  13. var matches = ("i>" + str).match(/[a|i|"|n]>(.*?)(?=<)/gm);
  14. var nonmatches = str.match(/<(.*?)(?=[a|i|"|n]>)/gm);
  15.  
  16. var star = /\*(\S(.*?\S)?)\*/gm;
  17. var double_star = /\*\*(\S(.*?\S)?)\*\*/gm;
  18. var undersc = /\_(\S(.*?\S)?)\_/gm;
  19. var double_undersc = /\_\_(\S(.*?\S)?)\_\_/gm;
  20. var tilde = /\~(\S(.*?\S)?)\~/gm;
  21. var double_tilde = /\~\~(\S(.*?\S)?)\~\~/gm;
  22.  
  23. matches[0] = matches[0].substring(2);
  24.  
  25. var newstring = "";
  26.  
  27. for (i = 0; i < matches.length; i++) {
  28. if( !(i+1<matches.length && matches[i+1].startsWith("a")) ) {
  29. matches[i] = matches[i].replace(double_star, '<b>$1</b>');
  30. matches[i] = matches[i].replace(double_undersc, '<u>$1</u>');
  31. matches[i] = matches[i].replace(star, '<i>$1</i>');
  32. matches[i] = matches[i].replace(undersc, '<i>$1</i>');
  33. matches[i] = matches[i].replace(double_tilde, '<s>$1</s>');
  34. }
  35. newstring = newstring + matches[i] + nonmatches[i];
  36. }
  37.  
  38. newstring += "n>";
  39. return newstring;
  40. }
  41.  
  42. $(document).ready(() => {
  43. let targetNode = document.getElementById("chatContainer");
  44. let options = {childList:true,subtree:true};
  45. let observer = new MutationObserver((mutationList)=>{
  46. let msgs = document.getElementsByClassName("theMsg");
  47. for(let a = msgs.length-1; a >= 0; a--){
  48. let m = msgs[a];
  49. if(!m.classList.contains("MarkUp")){
  50. m.classList.add("MarkUp");
  51. m.innerHTML = markup(m.innerHTML);
  52. }
  53. }
  54. });
  55. observer.observe(targetNode, options);
  56. });