Facebook Dark Theme Sync

Switch Facebook's theme to match the system

Versione datata 27/12/2020. Vedi la nuova versione l'ultima versione.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

You will need to install an extension such as Tampermonkey to install this script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         Facebook Dark Theme Sync
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Switch Facebook's theme to match the system
// @author       You
// @match        https://www.facebook.com/*
// @require      http://code.jquery.com/jquery-3.4.1.min.js
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    $( document ).ready(function() {
        let systemDarkEnabled = (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches)
        let isDarkEnabled = $('body').html().indexOf('["CometDarkModeSetting",[],{"initialSetting":"ENABLED"}') != -1;
        console.log("systemDarkEnabled " + systemDarkEnabled + " isDarkEnabled " + isDarkEnabled);
        //Check if themes are not in sync
        if(systemDarkEnabled != isDarkEnabled){
            //Open account menu
            let accountMenu = $("[aria-label=Account]");
            accountMenu.click();
            console.log("account menu clicked");
            setTimeout(function() {
                //Open theme switcher
                let switcher = $("span:contains('Display & Accessibility')").last();
                switcher.click();
                //Delay a second to allow the menu to open
                setTimeout(function() {
                    let darkModeMenu = $("[aria-label=\"Dark Mode\"]");
                    //Click the appropriate radio button
                    if(systemDarkEnabled){
                        darkModeMenu.children().first().next().children().first().click()
                    }else{
                        darkModeMenu.children().first().children().first().click()
                    }
                }, 1000);
            }, 2500);
        }
    });
})();