diff --git a/src/index.test.ts b/src/index.test.ts index 11676cc..9d5feb8 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -37,6 +37,31 @@ describe('custom-html', () => { expect(executedJS[2]).toEqual(`console.log('Log 2 from MC')`) }) + it('executes simple html with backticks in the script', () => { + const executedJS: string[] = [] + const fakeEvent = new Event('pageview', {}) as MCEvent + fakeEvent.payload = { + htmlCode: '
`some text`
', + } + fakeEvent.client = { + emitter: 'browser', + userAgent: + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36', + language: 'en-US', + referer: '', + ip: '127.0.0.1', + url: new URL('http://127.0.0.1:1337'), + execute: jsString => { + executedJS.push(jsString) + return true + }, + } + handler(fakeEvent) + expect(executedJS[0]).toEqual( + "const d = document.createElement('div');d.innerHTML = `
\\`some text\\`
`;document.body.appendChild(d);" + ) + }) + it('executes html injection with scripts that wait for other scripts', () => { const executedJS: string[] = [] const fakeEvent = new Event('pageview', {}) as MCEvent diff --git a/src/index.ts b/src/index.ts index a13af34..beb96d7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -53,7 +53,8 @@ export const handler = ({ payload, client }: MCEvent) => { client.execute( `const d = document.createElement('div');d.innerHTML = \`${$.html() .trim() - .replaceAll('$', '\\$')}\`;document.body.appendChild(d);` + .replaceAll('$', '\\$') + .replaceAll('`', '\\`')}\`;document.body.appendChild(d);` ) links.forEach(({ attributes }) => {