Skip to content

Instantly share code, notes, and snippets.

@sillvva
Last active June 19, 2019 05:03
Show Gist options
  • Save sillvva/04744e30b5a8cb054dab3a3ded278ab4 to your computer and use it in GitHub Desktop.
Save sillvva/04744e30b5a8cb054dab3a3ded278ab4 to your computer and use it in GitHub Desktop.
D&D Beyond Tweaks
// ==UserScript==">
// @name D&D Beyond Tweaks
// @namespace http://dndbeyond.com/
// @version 2.1
// @description Adds quality of life changes
// @author Sillvva
// @match https://www.dndbeyond.com/*
// @grant none
// ==/UserScript==
var ready = function() {
var i;
// Bulk Moderation Box Styles
var cbm = document.querySelector('.comment-bulk-moderation');
if (cbm) cbm.style.position = 'sticky';
if (cbm) cbm.style.width = '250px';
if (cbm) cbm.style.marginLeft = 'auto';
if (cbm) cbm.style.right = '0';
if (cbm) cbm.style.bottom = '0';
if (cbm) cbm.style.borderWidth = '0';
var jas = document.querySelector('.comment-bulk-moderation .j-apply-selection');
if (jas) jas.style.color = '#ffffff';
var cbms = document.querySelector('.comment-bulk-moderation select');
if (cbms) cbms.style.margin = '0';
if (cbms) cbms.style.width = '100%';
var cbmf = document.querySelector('.comment-bulk-moderation form');
if (cbmf) cbmf.style.margin = '0';
// Fixed Site Bar
addStyle('.site-bar {' +
' position: sticky;' +
' z-index: 51000 !important;' +
'}');
addStyle('#mega-menu-target {' +
' position: sticky;' +
' top: 64px;' +
' z-index: 2000;' +
'}');
if (inPages(['/sources', '/races', '/classes'])) {
// For bookmarkable section headers, create a link that can be bookmarked
var nodeList = document.querySelectorAll('.primary-content h1[id], .primary-content h2[id], .primary-content h3[id], .primary-content h4[id], ' +
'.content-container h1[id], .content-container h2[id], .content-container h3[id], .content-container h4[id]');
for (i = 0; i < nodeList.length; i++) {
nodeList[i].innerHTML += '<a href="#'+nodeList[i].id+'" style="float: right;">Link</a>';
}
// Sidebar Position
addStyle('.sidebar-menu {' +
' position: sticky !important;' +
' top: 114px !important;' +
' bottom: 0 !important;' +
' max-height: calc(100vh - 114px);' +
' overflow-y: auto;' +
' overflow-x: hidden;' +
'}');
addStyle('.sidebar-menu::-webkit-scrollbar {' +
' display: none;' +
'}');
}
if (inPages('/new-content')) {
addStyle('.select2-container {' +
' display: block;' +
' width: 600px !important;' +
'}');
}
window.addEventListener('hashchange', hashScroll, false);
setTimeout(function() {
var url = parseURL(window.location.href);
if (url.hash) hashScroll();
}, 200);
};
var hashScroll = function() {
setTimeout(function() {
window.scrollTo(0, window.pageYOffset - 124);
}, 20);
};
var parseURL = function(url) {
var parser = document.createElement('a'),
searchObject = {},
queries, split, i;
// Let the browser do the work
parser.href = url;
// Convert query string to object
queries = parser.search.replace(/^\?/, '').split('&');
if (parser.search.length > 0) {
for (i = 0; i < queries.length; i++) {
split = queries[i].split('=');
searchObject[split[0]] = split[1];
}
}
return {
protocol: parser.protocol,
host: parser.host,
hostname: parser.hostname,
port: parser.port,
pathname: parser.pathname,
search: parser.search,
searchObject: searchObject,
hash: parser.hash
};
};
var addStyle = function(newStyle) {
var styleElement = document.getElementById('styles_js');
if (!styleElement) {
styleElement = document.createElement('style');
styleElement.type = 'text/css';
styleElement.id = 'styles_js';
document.getElementsByTagName('head')[0].appendChild(styleElement);
}
styleElement.appendChild(document.createTextNode(newStyle));
};
var inPages = function(pages) {
var url = parseURL(window.location.href);
if (pages instanceof Array) {
for(var i = 0; i < pages.length; i++) {
if (url.pathname.indexOf(pages[i]) === 0) return true;
}
return false;
}
else if (typeof pages === 'string') {
return url.pathname.indexOf(pages) === 0;
}
};
ready();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment