IdlePixel Chat Markdown

Adds support for some markdown into chat

目前为 2023-11-03 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name IdlePixel Chat Markdown
  3. // @namespace lbtechnology.info
  4. // @version 1.1.0
  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://greasyfork.org/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. onLogin(){
  40. $("#game-chat .m-2 .m-2 button:not(.btn-chat-configure)").attr("onClick", "IdlePixelPlus.plugins.markdown.correctTildeSend()")
  41. }
  42. parseMarkdown(data){
  43. data.modified = false
  44. let message = data.message
  45. message = message.replace(/⁓/g, '~')
  46. const markdownPairs = {
  47. "``": ["``", "<code>", "</code>"],
  48. "**": ["\\*\\*", "<strong>", "</strong>"]
  49. }
  50.  
  51. for (const [markdown, html] of Object.entries(markdownPairs)){
  52. const re = new RegExp(html[0],"g");
  53. const tickCount = (message.match(re) || []).length;
  54. if (tickCount>1){
  55. data.modified = true
  56. const backtickPairs = Math.floor(tickCount / 2)
  57. for (let i=0; i<=backtickPairs; i++){
  58. message = message.replace(markdown, html[1])
  59. message = message.replace(markdown, html[2])
  60. }
  61. }
  62. }
  63.  
  64. data.message = message
  65. return data
  66. }
  67. correctTildeSend(){
  68. document.getElementById("chat-area-input").value = document.getElementById("chat-area-input").value.replace(/~/g, '⁓');
  69. Chat.send()
  70. }
  71. }
  72. const plugin = new MarkdownPlugin();
  73. IdlePixelPlus.registerPlugin(plugin);
  74. })();