Skip to content

Commit

Permalink
Poll for storage changes
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Bennetts <[email protected]>
  • Loading branch information
psiinon committed Nov 28, 2023
1 parent bc7c850 commit 9d34f66
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 0 deletions.
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
38 changes: 38 additions & 0 deletions test/ContentScript/integrationTests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,44 @@ function integrationTests(
expect(JSON.stringify(Array.from(actualData))).toBe(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(1000);
// Then
const expectedData =
'["{\\"action\\":{\\"action\\":\\"reportEvent\\"},\\"body\\":{\\"eventJson\\":\\"{TIMESTAMP,\\"eventName\\":\\"pageLoad\\",\\"url\\":\\"http://localhost:1801/webpages/localStorageDelay.html\\",\\"count\\":1}\\",\\"apikey\\":\\"not set\\"}}",' +
'"{\\"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))).toBe(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(1000);
// Then
const expectedData =
'["{\\"action\\":{\\"action\\":\\"reportEvent\\"},\\"body\\":{\\"eventJson\\":\\"{TIMESTAMP,\\"eventName\\":\\"pageLoad\\",\\"url\\":\\"http://localhost:1801/webpages/sessionStorageDelay.html\\",\\"count\\":1}\\",\\"apikey\\":\\"not set\\"}}",' +
'"{\\"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))).toBe(expectedData);
// Tidy up
await page.close();
});

test('Should record dup added link once ', async () => {
// Given / When
server = getFakeZapServer(actualData, _JSONPORT);
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");
}, 500);
</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");
}, 500);
</script>
</body>
</html>

0 comments on commit 9d34f66

Please sign in to comment.