Open Bug 1334891 Opened 7 years ago Updated 2 years ago

Support Web Workers created from content scripts

Categories

(WebExtensions :: General, enhancement, P5)

enhancement

Tracking

(Not tracked)

REOPENED

People

(Reporter: Fallen, Unassigned)

References

Details

(Whiteboard: triaged)

When creating a new Worker from a content script, it seems to use the same origin as the page and possibly creates the worker in page context. Instead, the Worker should be created with the origin/context of the content script.

It is therefore not possible to do new Worker(chrome.runtime.getURL('worker.js')), which results in SecurityError: Failed to load worker script at "moz-extension://ffb83def-dae7-3343-9063-794353cf4b9a/worker.js".

This also does not work in Chrome, but it would be great if it does in Firefox. See http://crbug.com/357664
kris is filing corresponding platform bug
Flags: needinfo?(kmaglione+bmo)
Priority: -- → P2
Whiteboard: triaged
Status: NEW → RESOLVED
Closed: 7 years ago
Flags: needinfo?(kmaglione+bmo)
Resolution: --- → DUPLICATE
Kris, can you explain why you marked this as a duplicate of bug 1260548? That other bug does not seem to be very relevant.
Flags: needinfo?(kmaglione+bmo)
I didn't intend to.
Status: RESOLVED → REOPENED
Flags: needinfo?(kmaglione+bmo)
Resolution: DUPLICATE → ---
Severity: normal → enhancement
Priority: P2 → P5
Bug 1423442 has a test case.

A partial work-around is to fetch the script text and then create a worker from the script URL: https://stackoverflow.com/questions/10343913/how-to-create-a-web-worker-from-a-string

This work-around is imperfect, because it is subject to the page's Content Security Policy.
For example, run this from a content script on Github (which has a restrictive CSP):

    new Worker(URL.createObjectURL(new Blob(['console.log("test");close()'])))

Tested with Nightly 61.0a1, 2018-04-29, the output in the tab's console is:
Content Security Policy: The page's setings blocked the loading of a resource at blob:https://github.com/[UUID] ("script-src")
Product: Toolkit → WebExtensions
Bulk move of bugs per https://bugzilla.mozilla.org/show_bug.cgi?id=1483958
Component: Untriaged → General
Severity: normal → S3
You need to log in before you can comment on or make changes to this bug.