mozilla-rally / web-science

WebScience is an open-source library for building browser-based research studies, including on Rally.
https://www.npmjs.com/package/@mozilla/web-science
Apache License 2.0
23 stars 6 forks source link

Provide method for content scripts to only run on certain domains in Chrome #284

Open rhelmer opened 2 years ago

rhelmer commented 2 years ago

Firefox supports dynamically loading content scripts, but until Chrome ships an equivalent API we don't have a way to restrict content scripts to only run on certain domains (specified as match patterns).

In PR https://github.com/mozilla-rally/web-science/issues/281 we attempted to write a roll-up plugin to generate entries , but Chrome cannot handle the ~4k sample of domains from the Beyond the Paywall study that I tested.

A few approaches come to mind:

  1. content scripts could message-pass with the background script/serviceworker to determine if they should run
  2. a list of allowed domains could be inlined into the content scripts

Since WebScience is built by the caller, I think both of these are pretty easy to do. The trade-offs are that (1) involves more messaging and runtime overhead, and (2) will cause content scripts to be larger on average, but no messaging would be required and the content scripts could run totally standalone.

(1) is probably the most flexible, and it might end up mattering if there are several content scripts that filter on a very large set of match patterns.

rhelmer commented 2 years ago

It looks like this might be supported in Chrome now, Manifest v3 only:

https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/scripting/registerContentScripts#browser_compatibility

Firefox supports it in MV3 as of 101, and MV2 as of 102+, so I think we might be able to switch to this now.

rhelmer commented 2 years ago

OK this seems to work testing latest Chrome + MV3, we will need to make a few adjustments: