Greasemonkey: WebExt: Execute scripts at all three runAt times

Created on 27 Feb 2017  路  6Comments  路  Source: greasemonkey/greasemonkey

Come up with a working plan for how to execute user scripts, of course not known until run time. Compatible with all three runAt times -- critically document_start.

See https://github.com/arantius/greasemonkey/tree/8a255bde1aa6312715740fd0157d582a2d6cb183 for my research so far, where the approach is:

  • Use one background script. (It will have access to storage/whatever APIs, where the scripts are stored, so can know the content of scripts to execute.)
  • Observe something (what?) to trigger ..
  • .. call tabs.executeScript() to fire off each user script.

This "works" except there's no event I've yet found to observe that works for all three run times. There's webNavigation's onBeforeNavigate which is in fact too soon, it (as the name implies) fires before the navigation, when the current document is still active. But the next point is onCommitted, and at that point it's too late for start, inline scripts have already executed.

wontfix

All 6 comments

To research:

  • Observe webRequest instead of webNavigation. Does that give us a handle at start time?
  • Inject one webext content script, at document_start, for every page. Have it somehow know which scripts to execute, synchronously. (Because anything async would push us past start time.)

Re: https://bugzilla.mozilla.org/show_bug.cgi?id=1332273#c8

I'd like to be able to store (in background) a blob, later get (only) its URL and pass that URL (synchronously) to content script(s), but I know of no way to do any of those things.

I haven't worked out all the details myself, but i think the background page could store blobs in IndexedDB, then convert them to blob URIs via URL.createObjectURL() at startup time and pass them to the hypothetical synchronous configuration API and then maybe load them via