|
Load a CSS file based on the current page's domain (e.g. "example.com").
Use:
https://stackoverflow.com/questions/13367376/get-the-domain-name-of-the-subdomain-javascript/13367604#13367604
to give us the hostname without any subdomains. This will apply the CSS
to all subdomains on the site. Not sure if this what we want yet, but it
seemed simpler to consolidate like this now rather than splitting CSS by
each subdomain or adding some kind of complicated matching/wildcard
logic.
Errors are ignored and just happen if the file for the current domain
doesn't exist. This seems okay to me to leave errors unhandled, as there
doesn't appear to be a clean way to handle them, and even if there was,
we'd just be either ignoring them anyway or conditionally executing
`insertCSS()`, which seems unnecessary.
|
|
A minimal prototype.
manifest.json:
* Include new content script
* Ask permissions for all HTTP and HTTPS site
css/.keep:
This directory will be "empty" in the release version. Add a dummy file
to ensure the directory appears in version control to make it clear
that's where user stylesheet files should go.
css/example.com.css:
Sample CSS file to see that styles are getting applied correctly.
background.js:
Load the `example.com.css` file into the current tab's web page. It does
this on a message from a content script. We do this because this file
only gets executed once and we can't call `browser.tabs.insertCSS()` in
the content script. While we could add the stylesheet directly in the
content script, it seemed to me that it would be simpler to call
`insertCSS()` instead of, say, adding a `<style>` tag etc.
content.js:
Sent a message to the background script to insert CSS. Just using a
dummy message for now. In the future, this will be replaced with the
domain of the current page, which will be matched against the CSS
filename.
Additionally, we assign `browser` to `chrome` because stupidly, Chrome
doesn't expose its `chrome` objects as `browser` as in the WebExtensions
standard. Normally this should work in Firefox too, but I haven't tested
it yet. My guess is that I'll probably need to change the:
var browser;
line to
var browser = browser;
But we'll see when we start testing that.
|