Skip to content

Commit

Permalink
Merge pull request #87 from psiinon/tweak/input-data
Browse files Browse the repository at this point in the history
Add input field type, value and form index
  • Loading branch information
thc202 authored Dec 1, 2023
2 parents d1528a7 + 1d0f0d8 commit 119d785
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## Unreleased

### Added
- Input field type and form index.

### Changed
- Poll for storage changes.

Expand Down
14 changes: 14 additions & 0 deletions source/types/ReportedModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ class ReportedStorage extends ReportedObject {
}

class ReportedElement extends ReportedObject {
public tagType: string | null;

public formId: number | null;

public constructor(element: Element, url: string) {
super(
'nodeAdded',
Expand All @@ -109,6 +113,16 @@ class ReportedElement extends ReportedObject {
// This gets the full URL rather than a relative one
const a: HTMLAnchorElement = element as HTMLAnchorElement;
this.href = a.toString();
} else if (element.tagName === 'INPUT') {
// Capture extra useful info for input elements
const input: HTMLInputElement = element as HTMLInputElement;
this.tagType = input.type;
this.text = input.value;
const {form} = input;
if (form) {
// This will not work if form tags are not used
this.formId = Array.prototype.slice.call(document.forms).indexOf(form);
}
} else if (element.hasAttribute('href')) {
this.href = element.getAttribute('href');
}
Expand Down
22 changes: 13 additions & 9 deletions test/ContentScript/unitTests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,10 @@ test('Report node elements', () => {
// Then
expect(mockFn.mock.calls.length).toBe(2);
expect(mockFn.mock.calls[0][0].toNonTimestampString()).toBe(
'{"type":"nodeAdded","tagName":"INPUT","id":"input1","nodeName":"INPUT","url":"http://localhost/","text":""}'
'{"type":"nodeAdded","tagName":"INPUT","id":"input1","nodeName":"INPUT","url":"http://localhost/","text":"","tagType":"text","formId":-1}'
);
expect(mockFn.mock.calls[1][0].toNonTimestampString()).toBe(
'{"type":"nodeAdded","tagName":"INPUT","id":"input2","nodeName":"INPUT","url":"http://localhost/","text":""}'
'{"type":"nodeAdded","tagName":"INPUT","id":"input2","nodeName":"INPUT","url":"http://localhost/","text":"","tagType":"text","formId":-1}'
);
});

Expand Down Expand Up @@ -242,9 +242,10 @@ test('Reported page loaded', () => {
'<!DOCTYPE html><body>' +
'<a href="https://www.example.com/1">link1</a>' +
'<form id="form1">FormContent</form>' +
'<button id="button1"></button>' +
'<input id="input1"></input>' +
'<area href="https://www.example.com/1">'
'<button id="button1">Button</button>' +
'<input id="input1" value="default"/>' +
'<area href="https://www.example.com/1">' +
'<input id="submit" type="submit" value="Submit"/>'
);
const mockFn = jest.fn();
localStorage.setItem('lsKey', 'value1');
Expand All @@ -254,7 +255,7 @@ test('Reported page loaded', () => {
src.reportPageLoaded(dom.window.document, mockFn);

// Then
expect(mockFn.mock.calls.length).toBe(7);
expect(mockFn.mock.calls.length).toBe(8);
expect(mockFn.mock.calls[0][0].toNonTimestampString()).toBe(
'{"type":"nodeAdded","tagName":"A","id":"","nodeName":"A","url":"http://localhost/","href":"https://www.example.com/1","text":"link1"}'
);
Expand All @@ -265,15 +266,18 @@ test('Reported page loaded', () => {
'{"type":"nodeAdded","tagName":"FORM","id":"form1","nodeName":"FORM","url":"http://localhost/","text":"FormContent"}'
);
expect(mockFn.mock.calls[3][0].toNonTimestampString()).toBe(
'{"type":"nodeAdded","tagName":"INPUT","id":"input1","nodeName":"INPUT","url":"http://localhost/","text":""}'
'{"type":"nodeAdded","tagName":"INPUT","id":"input1","nodeName":"INPUT","url":"http://localhost/","text":"default","tagType":"text"}'
);
expect(mockFn.mock.calls[4][0].toNonTimestampString()).toBe(
'{"type":"nodeAdded","tagName":"BUTTON","id":"button1","nodeName":"BUTTON","url":"http://localhost/","text":""}'
'{"type":"nodeAdded","tagName":"INPUT","id":"submit","nodeName":"INPUT","url":"http://localhost/","text":"Submit","tagType":"submit"}'
);
expect(mockFn.mock.calls[5][0].toNonTimestampString()).toBe(
'{"type":"localStorage","tagName":"","id":"lsKey","nodeName":"","url":"http://localhost/","text":"value1"}'
'{"type":"nodeAdded","tagName":"BUTTON","id":"button1","nodeName":"BUTTON","url":"http://localhost/","text":"Button"}'
);
expect(mockFn.mock.calls[6][0].toNonTimestampString()).toBe(
'{"type":"localStorage","tagName":"","id":"lsKey","nodeName":"","url":"http://localhost/","text":"value1"}'
);
expect(mockFn.mock.calls[7][0].toNonTimestampString()).toBe(
'{"type":"sessionStorage","tagName":"","id":"ssKey","nodeName":"","url":"http://localhost/","text":"value2"}'
);

Expand Down

0 comments on commit 119d785

Please sign in to comment.