Skip to content

Commit

Permalink
Fix no storage events
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Bennetts <[email protected]>
  • Loading branch information
psiinon committed Sep 19, 2023
1 parent 9189cf4 commit 98f1906
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ 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

### Fixed
- Storage events not being reported.
2 changes: 1 addition & 1 deletion source/Background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ async function handleMessage(
case REPORT_OBJECT: {
const repObj = JSON.parse(request.data);
if (repObj.type === LOCAL_STORAGE || repObj.type === SESSION_STORAGE) {
const repStorage = new ReportedStorage('', '', '', '', '');
const repStorage = new ReportedStorage('', '', '', '', '', '');
Object.assign(repStorage, repObj);
const repStorStr = repStorage.toShortString();
if (reportedStorage.has(repStorStr)) {
Expand Down
12 changes: 8 additions & 4 deletions source/ContentScript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ function reportStorage(
storage: Storage,
fn: (re: ReportedStorage) => void
): void {
const url = window.location.href;
for (const key of Object.keys(storage)) {
fn(new ReportedStorage(name, '', key, '', storage.getItem(key)));
fn(new ReportedStorage(name, '', key, '', storage.getItem(key), url));
}
}

Expand Down Expand Up @@ -131,19 +132,21 @@ function reportPageForms(
doc: Document,
fn: (re: ReportedObject) => void
): void {
const url = window.location.href;
Array.prototype.forEach.call(doc.forms, (form: HTMLFormElement) => {
fn(new ReportedElement(form));
fn(new ReportedElement(form, url));
});
}

function reportPageLinks(
doc: Document,
fn: (re: ReportedObject) => void
): void {
const url = window.location.href;
Array.prototype.forEach.call(
doc.links,
(link: HTMLAnchorElement | HTMLAreaElement) => {
fn(new ReportedElement(link));
fn(new ReportedElement(link, url));
}
);
}
Expand All @@ -152,8 +155,9 @@ function reportElements(
collection: HTMLCollection,
fn: (re: ReportedObject) => void
): void {
const url = window.location.href;
Array.prototype.forEach.call(collection, (element: Element) => {
fn(new ReportedElement(element));
fn(new ReportedElement(element, url));
});
}

Expand Down
7 changes: 4 additions & 3 deletions source/types/ReportedModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ReportedObject {
id: string,
nodeName: string,
text: string | null,
url: string = window.location.href
url: string
) {
this.timestamp = Date.now();
this.type = type;
Expand Down Expand Up @@ -96,13 +96,14 @@ class ReportedStorage extends ReportedObject {
}

class ReportedElement extends ReportedObject {
public constructor(element: Element) {
public constructor(element: Element, url: string) {
super(
'nodeAdded',
element.tagName,
element.id,
element.nodeName,
element.textContent
element.textContent,
url
);
if (element.tagName === 'A') {
// This gets the full URL rather than a relative one
Expand Down
13 changes: 10 additions & 3 deletions test/ContentScript/unitTests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ test('ReportedObject toString as expected', () => {
'b',
'c',
'd',
'e'
'e',
'http://localhost/'
);

// Then
Expand All @@ -59,7 +60,10 @@ test('ReportedObject toString as expected', () => {
test('ReportedElement P toString as expected', () => {
// Given / When
const el: Element = document.createElement('p');
const ro: src.ReportedElement = new src.ReportedElement(el);
const ro: src.ReportedElement = new src.ReportedElement(
el,
'http://localhost/'
);

// Then
expect(ro.toNonTimestampString()).toBe(
Expand All @@ -73,7 +77,10 @@ test('ReportedElement A toString as expected', () => {
const linkText = document.createTextNode('Title');
a.appendChild(linkText);
a.setAttribute('href', 'https://example.com');
const ro: src.ReportedElement = new src.ReportedElement(a);
const ro: src.ReportedElement = new src.ReportedElement(
a,
'http://localhost/'
);

// Then
expect(ro.toNonTimestampString()).toBe(
Expand Down

0 comments on commit 98f1906

Please sign in to comment.