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

Fix no storage events #72

Closed
wants to merge 1 commit into from
Closed
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
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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI this was the bug - window was not defined in some cases

) {
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