From 1a110a830c0a5e882084b61af565516c49c19a49 Mon Sep 17 00:00:00 2001 From: Evgeny Abramovich Date: Sat, 14 Dec 2024 19:32:33 -0400 Subject: [PATCH 1/3] Updated JSON detection --- .../json-detector/get-node-with-code.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/content-script/json-detector/get-node-with-code.ts b/src/content-script/json-detector/get-node-with-code.ts index b0702ee..a7b8892 100644 --- a/src/content-script/json-detector/get-node-with-code.ts +++ b/src/content-script/json-detector/get-node-with-code.ts @@ -1,19 +1,26 @@ +const testNode = (node: T | null): T | null => { + return node && /(^\s*[{[\d"])|(^\s*true)|(^\s*false)/.test(node?.innerText) + ? node + : null; +}; + export const getNodeWithCode = (list: NodeListOf): HTMLPreElement | HTMLDivElement | null => { const items = Array.from(list); const pre = items.find(node => node.nodeName === 'PRE') ?? null; if (!pre && navigator.userAgent.includes('Edg')) { const div = Array.from(list) - .find((node) => { + .find(node => { if (!(node instanceof HTMLDivElement)) { return false; } const attributes = node.getAttributeNames(); return attributes.length === 1 && attributes[0] === 'hidden'; - }) ?? null; + }) + ?? null; - return div as HTMLDivElement | null; + return testNode(div as HTMLDivElement | null); } - return pre as HTMLPreElement | null; + return testNode(pre as HTMLPreElement | null); }; From 088e4f4a5c84645b0d1467be7391a317ace831a0 Mon Sep 17 00:00:00 2001 From: Evgeny Abramovich Date: Sat, 14 Dec 2024 19:32:38 -0400 Subject: [PATCH 2/3] updated tests --- .../json-detector/get-node-with-code.test.ts | 72 ++++++++++--------- 1 file changed, 39 insertions(+), 33 deletions(-) diff --git a/src/content-script/json-detector/get-node-with-code.test.ts b/src/content-script/json-detector/get-node-with-code.test.ts index d65224a..add5416 100644 --- a/src/content-script/json-detector/get-node-with-code.test.ts +++ b/src/content-script/json-detector/get-node-with-code.test.ts @@ -3,24 +3,33 @@ import { edgeUserAgent } from '@testing/user-agents'; import { getNodeWithCode } from './get-node-with-code'; describe('getNodeWithCode', () => { + const addMarkup = (markup: string) => { + document.body.innerHTML = markup; + document.body.childNodes.forEach(node => { + if (node instanceof HTMLElement) { + node.innerText = node.innerHTML; + } + }) + } + describe('detects pre element in base chrome view', () => { const cases = [ { - markup: `
test
`, - expected: 'test', + markup: `
true
`, + expected: 'true', }, { - markup: `
toolbox
some other text
`, - expected: 'some other text', + markup: `
toolbox
"some other text"
`, + expected: '"some other text"', }, { - markup: `
toolbox
some text
`, - expected: 'some text', + markup: `
toolbox
{"id": 123}
`, + expected: '{"id": 123}', }, ]; test.each(cases)('%p', ({ markup, expected }) => { - document.body.innerHTML = markup; + addMarkup(markup); const pre = getNodeWithCode(document.body.childNodes); expect(pre).not.toBeNull(); expect(pre?.textContent).toBe(expected); @@ -29,19 +38,16 @@ describe('getNodeWithCode', () => { describe('detects not find element in other locations', () => { const cases = [ - { - markup: `
test
`, - }, - { - markup: `
toolbox
some other text
`, - }, - { - markup: `
toolbox
some text
`, - }, + { markup: `
test
` }, + { markup: `
toolbox
some other text
` }, + { markup: `
toolbox
some text
` }, + { markup: `
test
` }, + { markup: `
toolbox
some other text
` }, + { markup: `
toolbox
some text
` }, ]; test.each(cases)('%p', ({ markup }) => { - document.body.innerHTML = markup; + addMarkup(markup); const pre = getNodeWithCode(document.body.childNodes); expect(pre).toBeNull(); }); @@ -55,37 +61,37 @@ describe('getNodeWithCode', () => { const cases = [ { - markup: `
test
`, - expected: 'test', + markup: `
true
`, + expected: 'true', }, { - markup: `
toolbox
some other text
`, - expected: 'some other text', + markup: `
toolbox
"some other text"
`, + expected: '"some other text"', }, { - markup: `
toolbox
some text
`, - expected: 'some text', + markup: `
toolbox
{"id":123}
`, + expected: '{"id":123}', }, { - markup: ``, - expected: 'test', + markup: ``, + expected: 'false', }, { - markup: `
toolbox
`, - expected: 'some other text', + markup: `
toolbox
`, + expected: '\t"some other text"', }, { - markup: `
toolbox
`, - expected: 'some text', + markup: `
toolbox
`, + expected: '123123', }, { - markup: `
text
`, - expected: 'text', + markup: `
["test 2"]
`, + expected: '["test 2"]', }, ]; test.each(cases)('%p', ({ markup, expected }) => { - document.body.innerHTML = markup; + addMarkup(markup) const div = getNodeWithCode(document.body.childNodes); expect(div).not.toBeNull(); expect(div?.textContent).toBe(expected); @@ -112,7 +118,7 @@ describe('getNodeWithCode', () => { ]; test.each(cases)('%p', ({ markup }) => { - document.body.innerHTML = markup; + addMarkup(markup) const pre = getNodeWithCode(document.body.childNodes); expect(pre).toBeNull(); }); From fede631c767296cdf349e5da83b2e51d5fff24d4 Mon Sep 17 00:00:00 2001 From: Evgeny Abramovich Date: Sat, 14 Dec 2024 19:37:46 -0400 Subject: [PATCH 3/3] Fixed lint issues --- src/content-script/json-detector/get-node-with-code.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content-script/json-detector/get-node-with-code.ts b/src/content-script/json-detector/get-node-with-code.ts index a7b8892..90d0642 100644 --- a/src/content-script/json-detector/get-node-with-code.ts +++ b/src/content-script/json-detector/get-node-with-code.ts @@ -1,5 +1,5 @@ const testNode = (node: T | null): T | null => { - return node && /(^\s*[{[\d"])|(^\s*true)|(^\s*false)/.test(node?.innerText) + return node && /(^\s*[{[\d"])|(^\s*true)|(^\s*false)/.test(node.innerText) ? node : null; };