Toggling Firefox dark mode via bash script

It might be just me, but all this dark theme trend, while really nice and an advancement over past attempts (I used Windows with a black theme long ago and was hard for anything other than a gaming-only computer), feels still not perfect: My Ubuntu 20.04's dark theme has an almost white notifications & calendar widget, some applications don't support it, but where I find it still inconsistent is regarding browser support; MS Edge will use the same operating system setting, and Firefox allows fine-tuning for the theme (window borders, etc.) but for the actual browsing it's a config setting of either on or off.

The property I'm talking about, as of Firefox v89 is "ui.systemUsesDarkTheme", with integer values 0/1 for disabled/enabled. And you can only change it from the advanced settings, from about:config.

That this is what I desire:

  • Operating System: Always dark theme ✓
  • Browser theme: Always dark theme ✓
  • Browser tabs dark theme detection/preference: Switchable on demand ✗

If I could have one of the following regarding the last point, I'd be happy:

  • A button to toggle ui.systemUsesDarkTheme values (enabling/disabling it whenever I want)
  • A way to create a javascript bookmarklet to toggle the value (AFAIK for security reasons you cannot change about:config settings via JS scripts)
  • A way to access the property from an extension, to build a "toggle pages dark theme" one (browserSettings looks very limited)

As none of those are available options right now, I did an alternative approach: creating a bash script that will run upon login (and on demand whenever I want), to automatically set the property at my Firefox profile preferences file (prefs.js), depending on the time of the day. It is not perfect, as you have to have Firefox closed or not only won't detect the change, but will also override when closing the value you set with whatever value it had while running, but at least works for me if I set it to run when I login.

Anyway, this is the script:

PROFILE="<your-profile-folder-name-here>"
HOUR=$(date +%H)

DARK_MODE_SETTING=0
if [ $HOUR -gt 20 ] || [ $HOUR -lt 8 ]
then
    DARK_MODE_SETTING=1
fi

sed -i "s/user_pref(\"ui.systemUsesDarkTheme\",.*);/user_pref(\"ui.systemUsesDarkTheme\",$DARK_MODE_SETTING);/" ~/.mozilla/firefox/$PROFILE/prefs.js
Toggling Firefox dark mode via bash script published @ . Author: