DnDB Sidebar

Automatically switches sidebar between fixed and overlay based on width. Escape closes if overlay

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name     		DnDB Sidebar
// @description		Automatically switches sidebar between fixed and overlay based on width. Escape closes if overlay
// @version  		1
// @grant    		none
// @match 	        https://www.dndbeyond.com/*
// @require 		http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
// @require 		https://greasyfork.org/scripts/6250-waitforkeyelements/code/waitForKeyElements.js?version=23756
// @namespace https://greasyfork.org/users/789634
// ==/UserScript==

/* global $ */
/* global waitForKeyElements */

// dndbeyond is a react app that doesn't always reload the page. We track style changes
// on the body so we know when the combat tracker screen is loaded, otherwise
// we're constantly polling for the right DOM elements to load, and then they only
// work on the first page change.
var observer = new MutationObserver(function(mutations) {
    if( observer.event_bound == 'undefined' ) {
        observer.event_bound = false;
    }
    if(document.URL == observer.url){
        return;
    }else{
        observer.url = document.URL;
        if(observer.url.match(/profile\/(.*)\/characters\/(.*)/g)){
            if($(".body-rpgcharacter-sheet").length){
                if(!observer.event_bound){
                    observer.event_bound = true;
                    window_resized();
                    window.addEventListener('resize', window_resized);
                    window.addEventListener('keydown', body_keydown);
                }
            }else{
                if(observer.event_bound){
                    observer.event_bound = false;
                    window.removeEventListener('resize', window_resized);
                    window.removeEventListener('keydown', window_resized);
                }
            }
    }
    }
});

observer.observe($(document.body)[0], {
    childList: true,
});

var body_keydown = function(event){
    if(event.code == 'Escape' && $(".ct-sidebar__body--overlay-right").length){
        $('.ct-sidebar__control--collapse').click();
    }
}

var window_resized = function(){
    if(window.innerWidth >= 1605 && $(".ct-sidebar--hidden").length){
        $('.ct-sidebar__control--expand').click();
        waitForKeyElements('.ct-sidebar__control--fixed', function(){
            $('.ct-sidebar__control--fixed').click();
        });
    }
    if(window.innerWidth < 1605 && $(".ct-sidebar--visible").length){
        $('.ct-sidebar__control--overlay').click();
    }
}