import BasePlugin , { IXHRParameters } from "./basePlugin" ; export default abstract class BaseMessageModifierPlugin extends BasePlugin { public constructor ( name , settings ) { super ( name , settings ) ; this . shouldInterceptXHR = true ; } protected abstract doChange ( t ) ; protected abstract undoChange ( t ) ; public async init ( ) { const oldFormatWithOptions = await this . getElement ( ( ) => { const w = window as any ; return w . TS && w . TS . format && w . TS . format . formatWithOptions ; } ) ; const oldGetTokensString = await this . getElement ( ( ) => { const w = window as any ; return w . TSF && w . TSF . getTokensString ; } ) ; const w = window as any ; w . TS . format . formatWithOptions = ( t , n , r ) => { if ( r && r . for_edit ) { t = this . undoChange ( t ) ; } return oldFormatWithOptions ( t , n , r ) ; } ; w . TSF . getTokensString = ( e , l , r ) => { if ( l === "EDIT" ) { e = this . undoChange ( e ) ; } return oldGetTokensString ( e , l , r ) ; } ; } public interceptXHR ( request , parameters ) { if ( parameters . path . startsWith ( "/api/conversations.setTopic" ) || parameters . path . startsWith ( "/api/chat.postMessage" ) || parameters . path . startsWith ( "/api/chat.update" ) ) { const oldSend = request . send . bind ( request ) ; const send = e => { const key = e . has ( "topic" ) ? "topic" : "text" ; let text = e . get ( key ) ; const w = window as any ; const textBlocksRegex = new RegExp ( w . TSF . pre_rx . r ) ; const blocks = text . match ( textBlocksRegex ) || [ ] ; for ( let i = 0 ; i < blocks . length ; i ++ ) { text = text . replace ( blocks [ i ] , `||refinedBlock${ i }||` ) ; } const codeRegex = new RegExp ( w . TSF . code_rx . r ) ; const code = text . match ( codeRegex ) || [ ] ; for ( let i = 0 ; i < code . length ; i ++ ) { text = text . replace ( code [ i ] , `||refinedCode${ i }||` ) ; } w . TS . format . formatWithOptions ( text ) ; text = this . doChange ( text ) ; for ( let i = 0 ; i < code . length ; i ++ ) { text = text . replace ( `||refinedCode${ i }||` , code [ i ] . replace ( /\$/g , "$$$$" ) ) ; } for ( let i = 0 ; i < blocks . length ; i ++ ) { text = text . replace ( `||refinedBlock${ i }||` , blocks [ i ] . replace ( /\$/g , "$$$$" ) ) ; } e . set ( key , text ) ; if ( parameters . path . startsWith ( "/api/chat.update" ) ) { e . set ( "parse" , "default" ) ; } oldSend ( e ) ; } ; request . send = send . bind ( request ) ; } } }