Open Bug 1473306 Opened 6 years ago Updated 1 month ago

EventTarget does not work as expected in content scripts of addons

Categories

(WebExtensions :: General, defect, P3)

defect

Tracking

(firefox61 affected, firefox62 affected, firefox63 affected)

Tracking Status
firefox61 --- affected
firefox62 --- affected
firefox63 --- affected

People

(Reporter: uhyohyo, Assigned: robwu)

References

(Depends on 1 open bug)

Details

User Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36

Steps to reproduce:

1. download and install an example addon from: https://gist.github.com/uhyo/895e91d2b7a4434a8690145f53ee8277
2. open whatever page you like and inspect the console output


Actual results:

Following two lines are printed by the addon:

```
Test 1: raw EventTarget
Test 2: subclass of EventTarget
```


Expected results:

Following four lines should be printed.

```
Test 1: raw EventTarget
foo event emitted: 123
Test 2: subclass of EventTarget
foo event emitted: 456
```

The problem is that the listeners added to the EventTargets are not fired by dispatchEvent().
It seems to happen only in a content script; indeed, the above four lines are printed
when the same script is run directly on a page.
I reproduced the issue on Firefox 61.0.1, Firefox 62.0b6 and Nightly 63.0a1 (2018-07-09) [Ubuntu 16.04].
Status: UNCONFIRMED → NEW
Component: Untriaged → Debugger
Ever confirmed: true
Product: Firefox → DevTools
Version: 63 Branch → Trunk
Component: Debugger → DOM: Events
Product: DevTools → Core
Component: DOM: Events → General
Product: Core → WebExtensions
Flags: needinfo?(rob)
Priority: -- → P3

This is a flaw in the global method proxy wrapper at [1].

A work-around for extension code is to use window.EventTarget instead of the EventTarget global, like this:

var e = new window.EventTarget();
e.addEventListener('foo', (e) => console.log('foo event emitted'));
e.dispatchEvent(new CustomEvent('foo'));

Assigning to myself because I intend to resume work on Sandbox globals at some point (bug 1468579 to start with).

[1] https://searchfox.org/mozilla-central/rev/c3ebaf6de2d481c262c04bb9657eaf76bf47e2ac/js/xpconnect/src/Sandbox.cpp#709-713

Assignee: nobody → rob
Flags: needinfo?(rob)

I would like to extend the bug report a bit.
window.EventTarget unfortunately does not support inheritance, so the window.EventTarget is not working for every case.

Severity: normal → S3
Duplicate of this bug: 1894258

Case 2 of this report depends on bug 1820521.
Case 1 was also shown at bug 1894258, and might or might not be fixed with the same fix.

Depends on: 1820521
You need to log in before you can comment on or make changes to this bug.