1

WhatsApp Web doesn't have an option to make it dark mode by stock as of right now, but it can be enabled by inspecting element and changing: body class="web" to class="web dark".

Now I don't have any actual programming knowledge but is there a way to make such change automatically every time I open https://web.whatsapp.com/? As of right now I need to manually do it each time.

Thanks for the help.

1 Answers1

2

Yes anything's possible, especially this

in order to run a script every time a page loads, you would want to make a browser extension (usually chrome extension, but same principles would apply to firefox also or possibly other browsers).

But first you need to just think of what that script would be. If you want to change to class nameof the body to something, in this case web dark, then the JavaScript would be

document.body.className = "web dark"

now the slightly hard part is making the actual extension I'll go over the instructions in chrome but similar concepts may apply to other browsers.

First open up a text editor. It can even be notepad, just when you go to file -> save as, change the extension ".txt" to "all files".

Anyways, name the new file "manifest.json" in a brand new folder somewhere, making sure to change the type of "all files" when you save it

Now we need to add some content to it. There are a few mandatory fields for every extension to hae, you can just copy the following and change the parts you want:

 {
    "name": "the actual name of it",
    "version": "1.0",
    "description": "put anything you want here!?",
    "manifest_version": 2
  }

OK now go open chrome, go to the extensions page (usually under tools, or chrome://extensions), then there should be a little tick mark that says "developer mode". If you see it, click it, now you should see a new menu appear that says, as one of the options "load unpacked". Click it.

Now select that new folder you made, that contains the manifest.json file.

If all goes well so far, meaning it loads with no errors, then we can move on.

Now we just need to add whats called a Content Script, which will execute some JS when the page loads.

To do so, add a new field in the manifest.json file, called "content_scripts", with a field called "matches" that contains the base URL of the ste you want to inject the scirpt to (in this case probably whatsapp.com), and another field called "js", containg the path to a new JS file that we are about to make, let's call it myscript.js (which we will get to in a sec). So so far the manifest.json should look like this:

 {
    "name": "the actual name of it",
    "version": "1.0",
    "description": "put anything you want here!?",
    "manifest_version": 2,
    "content_scripts": [{
        "matches": ["https://*.whatsapp.com/*"],
        "js":["myscript.js"]
    }]
  }

then reload it to see if there are any errors.

If not, we can move on.

now, create a new file in that same directory as manifest.json, called myscript.js. You can use notepad even, just make sure to set the type to "all files".

In that new file, simply add

function doIt() {
    document.body.className = "web dark"
}

if(document.readyState == "complete") 
    doIt()
else
    addEventListener("load", doIt)
    
wow ow
  • 352
  • 1
  • 10
  • Don't you think using extensions like Greasemokey/Tampermonkey would be easier? – Ivan Shatsky Jun 22 '20 at 04:45
  • [GreaseMonkey](https://addons.mozilla.org/en-US/firefox/addon/greasemonkey/) is for Firefox, [Tampermonkey](https://chrome.google.com/webstore/detail/tampermonkey/dhdgffkkebhmkfjojejmpbldmpobfkfo) is for Chrome (works for me with the latest Chrome version). – Ivan Shatsky Jun 22 '20 at 04:49
  • 1
    @IvanShatsky interesting, didn't know about tambermonkey, but one reason why I wouldn't reccomend it, even after installing it and testing it, is because my viewpoint is to use as few 3rd party libraries as possible, I think its always better to do things from scrtah, when practicle. For a newcomer, its almost as many steps to go to the chrome store, install the extension, learn how to use the extension, make a script on the extension, add a custom rule, etc., then it is to simply create 2 text iles and switch a setting on in chrome, so I would prefer to do it from scratch – wow ow Jun 22 '20 at 06:20
  • now if they were planning on making 100+ unique scripts, maybe tampermonkey would be good, but just for one script its better to make one extension – wow ow Jun 22 '20 at 06:21
  • @wowow Thanks so much for the thorough explanation! It works perfectly, I just needed to surround the value of the ```content_scripts``` field in square brackets, otherwise it would give me an error and not load. – Marcus Kuhn Jun 22 '20 at 07:10
  • @MarcusKuhn ah yes, glad it worked out, just curious but do u mean the values following `:match` and `js`? – wow ow Jun 22 '20 at 07:16
  • @MarcusKuhn oh interesting, right didn't realize :) cool glad it worked – wow ow Jun 22 '20 at 07:19
  • @wowow actually its the whole thing with both `"matches"` and `"js"` surrounded by a square bracket, like shown [here](https://stackoverflow.com/questions/2769525/chrome-extension-manifest-matches), where I found the solution. – Marcus Kuhn Jun 22 '20 at 07:23
  • @wowow thanks so much again, I'm sorry I can't upvote your answer because I don't have a reputation of 15 yet hahaha – Marcus Kuhn Jun 22 '20 at 07:29
  • @MarcusKuhn lol its all good, we're all stuck in the S.O. communist system... glad its all fixed and if you have any other questions, let me know :) (u can reply to this again and I get notified, even if its for another question, if u want :)) – wow ow Jun 22 '20 at 07:30