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

Add content scripts section in specification #542

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
80 changes: 76 additions & 4 deletions specification/index.bs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Group: WECG
URL: https://w3c.github.io/webextensions
Editor: Mukul Purohit, Microsoft Corporation https://www.microsoft.com, [email protected]
Editor: Tomislav Jovanovic, Mozilla https://www.mozilla.org/, [email protected]
Editor: Oliver Dunk, Google https://www.google.com, [email protected]
Abstract: [Placeholder] Abstract.
Markup Shorthands: markdown yes
</pre>
Expand All @@ -27,11 +28,11 @@ An optional directory containing strings as defined in <a href="#localization">l

### Other files

An extension may also contain other files, such as those referenced in the <a href="#key-content_scripts">content_scripts</a> and <a href="#key-background">background</a> part of the <a href="#manifest">Manifest</a>.
An extension may also contain other files, such as those referenced in the [[#key-content_scripts]] and [[#key-background]] part of the [=manifest=].
oliverdunk marked this conversation as resolved.
Show resolved Hide resolved

## Manifest

A WebExtension must have a manifest file at its root directory.
A WebExtension must have a <dfn>manifest</dfn> file at its root directory.

### Manifest file

Expand Down Expand Up @@ -112,7 +113,7 @@ This key may be present.

#### Key `content_scripts`

This key may be present.
The <a href="#key-content_scripts">`content_scripts`</a> key is a [=list=] of items representing [=content scripts=] that should be registered.

#### Key `content_security_policy`

Expand Down Expand Up @@ -154,6 +155,8 @@ Filenames beginning with an underscore (`_`) are reserved for use by user agent.

## Isolated worlds

<dfn>Worlds</dfn> are isolated JavaScript contexts with access to the same underlying DOM tree but their own set of wrappers around those DOM objects.
oliverdunk marked this conversation as resolved.
Show resolved Hide resolved

## Unavailable APIs

## The `browser` global
Expand All @@ -172,6 +175,12 @@ Issue(62): Specify localization handling.

## Match patterns

A <dfn>match pattern</dfn> is a pattern used to match URLs.
oliverdunk marked this conversation as resolved.
Show resolved Hide resolved

## Globs

A <dfn>glob</dfn> can be any [=string=]. It can contain any number of wildcards where * can match zero or more characters and ? matches exactly one character.
oliverdunk marked this conversation as resolved.
Show resolved Hide resolved

## Concepts

### Uniqueness of extension IDs
Expand All @@ -190,7 +199,70 @@ Issue(62): Specify localization handling.

### Content scripts

#### Isolated worlds
<dfn>Content scripts</dfn> represent a set of JS and CSS files that should be injected into pages loaded by the user agent.
oliverdunk marked this conversation as resolved.
Show resolved Hide resolved

#### Key `matches`

A [=list=] of [=match patterns=] that are used to decide where the content script runs. This key is required.
oliverdunk marked this conversation as resolved.
Show resolved Hide resolved

#### Key `exclude_matches`

A [=list=] of [=match patterns=] that should be used to exclude URLs from where the content script runs.
oliverdunk marked this conversation as resolved.
Show resolved Hide resolved

#### Key `js`

A [=list=] of file paths that should be injected as scripts.
oliverdunk marked this conversation as resolved.
Show resolved Hide resolved

#### Key `css`

A [=list=] of file paths that should be injected as stylesheets.

#### Key `all_frames`

If `all_frames` is true, the content script must be injected into subframes. Defaults to false.
oliverdunk marked this conversation as resolved.
Show resolved Hide resolved

#### Key `match_about_blank`

If this is `true`, the content script will also be injected into an additional user agent specified set of pages used to represent empty frames. This will only happen if the content script matches the page that embedded the frame. Defaults to `false`.
oliverdunk marked this conversation as resolved.
Show resolved Hide resolved

#### Key `match_origin_as_fallback`

Used to match frames with an opaque or otherwise missing origin. The origin to match against is determined in the following order of priority:

1. If the frame has an [=opaque origin=], such as with a [=blob URLs=], use the non-opaque origin.
oliverdunk marked this conversation as resolved.
Show resolved Hide resolved
1. If available, use the origin of the parent frame.
1. Otherwise, no origin is found and this frame can never be matched.

#### Key `run_at`

Specifies when the content script should be injected. Valid values are `document_start`, `document_end` and `document_idle`.
oliverdunk marked this conversation as resolved.
Show resolved Hide resolved

#### Key `include_globs`

A list of [=globs=] that a page should match in addition to matches.
oliverdunk marked this conversation as resolved.
Show resolved Hide resolved

#### Key `exclude_globs`

A list of [=globs=] that should be used to exclude URLs from where the content script runs.
oliverdunk marked this conversation as resolved.
Show resolved Hide resolved

#### Key `world`

The [=world=] any JavaScript scripts should be injected into. Defaults to `ISOLATED`. Valid values are `MAIN` and `ISOLATED`.
oliverdunk marked this conversation as resolved.
Show resolved Hide resolved

#### Injecting a content script

Issue: If the same extension specifies the same script twice, what should happen? ([bug](https://crbug.com/324096753))

Issue: The below algorithm needs to be updated to include `match_about_blank` and `match_origin_as_fallback`.

To determine if a content script should be injected in a frame:

1. If the extension does not have access to the origin, return.
1. If the origin is not included in `matches`, return.
oliverdunk marked this conversation as resolved.
Show resolved Hide resolved
1. If `include_globs` is present and the origin is not matched, return.
1. If the origin matches an entry in `exclude_matches` or `exclude_globs`, return.
1. If this is a frame, and `all_frames` is not `true`, return.
oliverdunk marked this conversation as resolved.
Show resolved Hide resolved
1. Otherwise, inject the content script. This should be done based on the `run_at` setting.
oliverdunk marked this conversation as resolved.
Show resolved Hide resolved

### Extension pages

Expand Down
Loading