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

API limitation: content scripts URL matches and History API, Navigation API, BFCache #523

Open
bershanskiy opened this issue Jan 17, 2024 · 6 comments
Labels
spec clarification Needs clarification when specified

Comments

@bershanskiy
Copy link
Member

Summary

Content Script declarations can contain match patterns with non-empty paths (e.g., https://example.com/subpage with path /subpage). The match is evaluated only on the initial document navigation and not on URL replacement in response to History API invocation. (BFCache discards a document if there was an extension-related event affecting it between document entering BFCache and getting restored.)
To sum up, currently CS match patterns with non-/* or /*/*paths may not be reliable when pages use History or Navigation API.

Example

Example extension

manifest.json

{
  "name": "Demo",
  "manifest_version": 2,
  "version":"1",
  "content_scripts": [{
    "js": ["content-script.js"],
    "matches": ["https://example.com/sub"]
  }]
}

content-script.js

console.error('ran');

Steps

  1. Go to https://example.com/
  2. Open console and observe no error logged ran
  3. Replace URL via History API by running this in console: history.replaceState(undefined,undefined,"/sub")
  4. Observe that the URL changed to https://example.com/sub but the ran did not appear
  5. Reload the page via reload button and observe ran error message logged in the console
@tophf
Copy link

tophf commented Jan 18, 2024

This would help avoid injecting unnecessarily in the entire site with a listener for navigation.onnavigate (Chrome only as of now) or using the background script with chrome.tabs.onUpdated (wakes up the background script for unrelated URLs) or chrome.webNavigation.onHistoryStateUpdated (adds a scary installation warning).

Since it would be a breaking change, a new key in the declaration is necessary e.g. "match_soft_navigation": true

@oliverdunk
Copy link
Member

This seems worth discussion, but I expect this may be behaviour we want to keep and just ensure we have adequately documented. When the URL changes to one that matches a content script, this isn't too tricky - we can just inject the script. This gets a lot harder if we try to be consistent with when you navigate away though - there's no way to "uninject" a content script that is already running and may have made changes to the page.

@fregante
Copy link

there's no way to "uninject" a content script that is already running and may have made changes to the page.

That applies to the reverse situation though: first load matches, successive AJAX loads and URL changes no longer match.

@oliverdunk
Copy link
Member

That applies to the reverse situation though: first load matches, successive AJAX loads and URL changes no longer match.

For sure. I think the thing we need to decide is whether having some soft navigation controls increases the confusion because now it seems like we have handling for that.

@xeenon
Copy link
Collaborator

xeenon commented Jan 20, 2024

This is pretty much the same request as #446.

@bershanskiy
Copy link
Member Author

Meeting on 15 Feb 2024: agreed to keep implementation as is and add notes to MDN.

@Rob--W Rob--W added spec clarification Needs clarification when specified and removed needs-triage labels Feb 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
spec clarification Needs clarification when specified
Projects
None yet
Development

No branches or pull requests

6 participants