IdlePixel Chat Markdown

Adds support for some markdown into chat

  1. // ==UserScript==
  2. // @name IdlePixel Chat Markdown
  3. // @namespace lbtechnology.info
  4. // @version 1.1.1
  5. // @description Adds support for some markdown into chat
  6. // @author Lux-Ferre
  7. // @license MIT
  8. // @match *://idle-pixel.com/login/play*
  9. // @grant none
  10. // @require https://greatest.deepsurf.us/scripts/441206-idlepixel/code/IdlePixel+.js?anticache=20220905
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. class MarkdownPlugin extends IdlePixelPlusPlugin {
  17. constructor() {
  18. super("markdown", {
  19. about: {
  20. name: GM_info.script.name,
  21. version: GM_info.script.version,
  22. author: GM_info.script.author,
  23. description: GM_info.script.description
  24. }
  25. });
  26. this.previous = "";
  27. }
  28.  
  29. onChat(data){
  30. const newMessage = this.parseMarkdown(data)
  31. if (data.modified){
  32. const element = $("#chat-area > *").last();
  33. while (element[0].lastChild.nodeName !== "SPAN"){
  34. element[0].removeChild(element[0].lastChild)
  35. }
  36. element.append(newMessage.message)
  37. }
  38. }
  39. parseMarkdown(data){
  40. data.modified = false
  41. let message = data.message
  42. message = message.replace(/⁓/g, '~')
  43. const markdownPairs = {
  44. "``": ["``", "<code>", "</code>"],
  45. "**": ["\\*\\*", "<strong>", "</strong>"]
  46. }
  47.  
  48. for (const [markdown, html] of Object.entries(markdownPairs)){
  49. const re = new RegExp(html[0],"g");
  50. const tickCount = (message.match(re) || []).length;
  51. if (tickCount>1){
  52. data.modified = true
  53. const backtickPairs = Math.floor(tickCount / 2)
  54. for (let i=0; i<=backtickPairs; i++){
  55. message = message.replace(markdown, html[1])
  56. message = message.replace(markdown, html[2])
  57. }
  58. }
  59. }
  60.  
  61. data.message = message
  62. return data
  63. }
  64. }
  65. const plugin = new MarkdownPlugin();
  66. IdlePixelPlus.registerPlugin(plugin);
  67. })();