Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Poll for storage changes #86

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ All notable changes to this add-on will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## Unreleased

### Changed
- Poll for storage changes.

## 0.0.7 - 2023-10-23

### Changed
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Then run the following:
- `yarn run build:firefox` to build firefox addon
- `yarn run build:opera` to build opera extension
- `yarn run build` builds and packs extensions all at once to extension/ directory
- `yarn run lint` to lint the code
- `yarn run test` to run the test suite (you should not have anything listening on port 8080)

### Development

Expand Down
5 changes: 5 additions & 0 deletions source/ContentScript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ function enableExtension(): void {
subtree: true,
});

setInterval(() => {
// Have to poll to pickup storage changes in a timely fashion
reportAllStorage();
}, 500);

// This is needed for more traditional apps
reportPageLoaded(document, reportObject);
}
Expand Down
46 changes: 40 additions & 6 deletions test/ContentScript/integrationTests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,8 @@ function integrationTests(
await page.close();
// Then
const expectedData =
'["{\\"action\\":{\\"action\\":\\"reportEvent\\"},\\"body\\":{\\"eventJson\\":\\"{TIMESTAMP,\\"eventName\\":\\"pageLoad\\",\\"url\\":\\"http://localhost:1801/webpages/localStorage.html\\",\\"count\\":1}\\",\\"apikey\\":\\"not set\\"}}",' +
'"{\\"action\\":{\\"action\\":\\"reportObject\\"},\\"body\\":{\\"objectJson\\":\\"{TIMESTAMP,\\"type\\":\\"localStorage\\",\\"tagName\\":\\"\\",\\"id\\":\\"test\\",\\"nodeName\\":\\"\\",\\"url\\":\\"http://localhost:1801/webpages/localStorage.html\\",\\"text\\":\\"localData\\"}\\",\\"apikey\\":\\"not set\\"}}"]';
expect(JSON.stringify(Array.from(actualData))).toBe(expectedData);
'"{\\"action\\":{\\"action\\":\\"reportObject\\"},\\"body\\":{\\"objectJson\\":\\"{TIMESTAMP,\\"type\\":\\"localStorage\\",\\"tagName\\":\\"\\",\\"id\\":\\"test\\",\\"nodeName\\":\\"\\",\\"url\\":\\"http://localhost:1801/webpages/localStorage.html\\",\\"text\\":\\"localData\\"}\\",\\"apikey\\":\\"not set\\"}}"';
expect(JSON.stringify(Array.from(actualData))).toContain(expectedData);
});

test('Should record set sessionStorage', async () => {
Expand All @@ -318,9 +317,44 @@ function integrationTests(
await page.close();
// Then
const expectedData =
'["{\\"action\\":{\\"action\\":\\"reportEvent\\"},\\"body\\":{\\"eventJson\\":\\"{TIMESTAMP,\\"eventName\\":\\"pageLoad\\",\\"url\\":\\"http://localhost:1801/webpages/sessionStorage.html\\",\\"count\\":1}\\",\\"apikey\\":\\"not set\\"}}",' +
'"{\\"action\\":{\\"action\\":\\"reportObject\\"},\\"body\\":{\\"objectJson\\":\\"{TIMESTAMP,\\"type\\":\\"sessionStorage\\",\\"tagName\\":\\"\\",\\"id\\":\\"test\\",\\"nodeName\\":\\"\\",\\"url\\":\\"http://localhost:1801/webpages/sessionStorage.html\\",\\"text\\":\\"sessionData\\"}\\",\\"apikey\\":\\"not set\\"}}"]';
expect(JSON.stringify(Array.from(actualData))).toBe(expectedData);
'"{\\"action\\":{\\"action\\":\\"reportObject\\"},\\"body\\":{\\"objectJson\\":\\"{TIMESTAMP,\\"type\\":\\"sessionStorage\\",\\"tagName\\":\\"\\",\\"id\\":\\"test\\",\\"nodeName\\":\\"\\",\\"url\\":\\"http://localhost:1801/webpages/sessionStorage.html\\",\\"text\\":\\"sessionData\\"}\\",\\"apikey\\":\\"not set\\"}}"';
expect(JSON.stringify(Array.from(actualData))).toContain(expectedData);
});

test('Should record set localStorage with page open', async () => {
// Given / When
server = getFakeZapServer(actualData, _JSONPORT, true);
const context = await driver.getContext(_JSONPORT);
const page = await context.newPage();
await page.goto(
`http://localhost:${_HTTPPORT}/webpages/localStorageDelay.html`
);
await page.waitForLoadState('networkidle');
await page.waitForTimeout(2000);
// Then
const expectedData =
'"{\\"action\\":{\\"action\\":\\"reportObject\\"},\\"body\\":{\\"objectJson\\":\\"{TIMESTAMP,\\"type\\":\\"localStorage\\",\\"tagName\\":\\"\\",\\"id\\":\\"test\\",\\"nodeName\\":\\"\\",\\"url\\":\\"http://localhost:1801/webpages/localStorageDelay.html\\",\\"text\\":\\"localData\\"}\\",\\"apikey\\":\\"not set\\"}}"';
expect(JSON.stringify(Array.from(actualData))).toContain(expectedData);
// Tidy up
await page.close();
});

test('Should record set sessionStorage with page open', async () => {
// Given / When
server = getFakeZapServer(actualData, _JSONPORT);
const context = await driver.getContext(_JSONPORT);
const page = await context.newPage();
await page.goto(
`http://localhost:${_HTTPPORT}/webpages/sessionStorageDelay.html`
);
await page.waitForLoadState('networkidle');
await page.waitForTimeout(2000);
// Then
const expectedData =
'"{\\"action\\":{\\"action\\":\\"reportObject\\"},\\"body\\":{\\"objectJson\\":\\"{TIMESTAMP,\\"type\\":\\"sessionStorage\\",\\"tagName\\":\\"\\",\\"id\\":\\"test\\",\\"nodeName\\":\\"\\",\\"url\\":\\"http://localhost:1801/webpages/sessionStorageDelay.html\\",\\"text\\":\\"sessionData\\"}\\",\\"apikey\\":\\"not set\\"}}"';
expect(JSON.stringify(Array.from(actualData))).toContain(expectedData);
// Tidy up
await page.close();
});

test('Should record dup added link once ', async () => {
Expand Down
13 changes: 13 additions & 0 deletions test/ContentScript/webpages/localStorageDelay.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<script>
setInterval(() => {
localStorage.setItem("test", "localData");
}, 1000);
</script>
</body>
</html>
13 changes: 13 additions & 0 deletions test/ContentScript/webpages/sessionStorageDelay.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<script>
setInterval(() => {
sessionStorage.setItem("test", "sessionData");
}, 1000);
</script>
</body>
</html>