Skip to content

Instantly share code, notes, and snippets.

@YodaDaCoda
Last active March 16, 2020 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YodaDaCoda/7e0463ed4f3cc3ad84320bbe06989d9c to your computer and use it in GitHub Desktop.
Save YodaDaCoda/7e0463ed4f3cc3ad84320bbe06989d9c to your computer and use it in GitHub Desktop.
Dice roller for DnDBeyond character sheet
(function () {
let $;
let d20;
let toastr;
let attribute_map = {
STR : 'Strength',
DEX : 'Dexterity',
CON : 'Constitution',
INT : 'Intelligence',
WIS : 'Wisdom',
CHA : 'Charisma',
str : 'Strength',
dex : 'Dexterity',
con : 'Constitution',
int : 'Intelligence',
wis : 'Wisdom',
cha : 'Charisma',
};
let selector_list = {
'.ct-combat-attack__tohit' : function (el) {
return el.closest('.ct-combat-attack').find('.ct-combat-attack__label').text() + ' (to hit)';
},
'.ct-damage__value' : function (el) {
return el.closest('.ct-combat-attack, .ct-spells-spell').find('.ct-combat-attack__label, .ct-spell-name').first().text() + ' (' + el.closest('.ct-combat-attack, .ct-spells-spell').find('.ct-tooltip').first().attr('data-original-title') + ' damage)';
},
'.ct-spell-damage-effect__healing' : function (el) {
return el.closest('.ct-spells-spell, .ct-combat-attack').find('.ct-spell-name').first().text() + ' (healing)';
},
'.ct-initiative-box__value' : function () {
return 'Initiative';
},
'.ct-skills__list .ct-skills__col--modifier' : function (el) {
return el.parent().find('.ct-skills__col--skill').text() + ' Check';
},
'.ct-ability-summary__secondary' : function (el) {
return el.parent().find('.ct-ability-summary__label').text() + ' Check';
},
'.ct-saving-throws-summary__ability-modifier' : function (el) {
return attribute_map[el.closest('.ct-saving-throws-summary__ability').find('.ct-saving-throws-summary__ability-name').text().replace(/\s/, '')] + ' Save';
},
'.ct-combat-mobile__extra--initiative .ct-combat-mobile__extra-value' : function (el) {
return 'Initiative';
},
};
function createScript(src, callback) {
let el = document.createElement('script');
el.src = src;
if (typeof callback === 'function') {
el.onload = callback;
}
return el;
}
function appendScript(el) {
document.head.appendChild(el);
}
function createAndAppendScript(src, callback) {
appendScript(createScript(src, callback));
}
function createStyle(href, callback) {
let el = document.createElement('link');
el.rel = 'stylesheet';
el.href = href;
if (typeof callback === 'function') {
el.onload = callback;
}
return el;
}
function appendStyle(el) {
document.head.appendChild(el);
}
function createAndAppendStyle(href, callback) {
appendStyle(createStyle(href, callback));
}
function getElTitle(el) {
for (let [key, val] of Object.entries(selector_list)) {
if (el.is(key)) {
return val(el);
}
}
}
function createOverrideStyle() {
let el = document.createElement('style');
document.head.appendChild(el);
let style = el.sheet;
style.insertRule('#toast-container > div { opacity: 1; }');
style.insertRule('@media (min-width: 768px) { #toast-container > div { zoom: 2; } }');
style.insertRule(`${Object.keys(selector_list).join(', ')} { cursor: alias; }`);
style.insertRule(`${Object.keys(selector_list).join(', ')} { border-radius: 999px; box-shadow: inset 0 0 2px 1px #ff0000cc; }`);
style.insertRule(`${Object.keys(selector_list).join(':hover,')}:hover { background: #ff0000cc; }`);
}
function rollFormula(title, formula) {
let result = d20.roll(formula);
toastr['success'](result.output, `${title}: ${result.total}`);
}
function handleClick(e) {
e.stopPropagation();
let el = e.currentTarget;
let formula = el.innerText.replace(/\s/g, '');
if (formula[0] === '+' || formula[0] === '-') {
formula = 'd20' + formula;
}
rollFormula(getElTitle($(el)), formula);
}
function doInit() {
createOverrideStyle();
$(document.body).on('click', Object.keys(selector_list).join(', '), handleClick);
}
function handleInit() {
if (typeof $ === 'undefined') {
return;
}
if (typeof d20 === 'undefined') {
return;
}
if (typeof toastr === 'undefined') {
return;
}
doInit();
}
function jqueryCallback() {
$ = window.jQuery.noConflict(true);
handleInit();
}
function dicerollerCallback() {
d20 = new window.rpgDiceRoller.DiceRoller();
handleInit();
}
function toastrCallback() {
toastr = window.toastr;
toastr.options = {
closeButton : true,
debug : false,
newestOnTop : false,
progressBar : true,
positionClass : 'toast-bottom-center',
preventDuplicates : false,
onclick : null,
showDuration : '300',
hideDuration : '1000',
timeOut : '30000',
extendedTimeOut : '30000',
showEasing : 'swing',
hideEasing : 'linear',
showMethod : 'fadeIn',
hideMethod : 'fadeOut',
};
handleInit();
}
createAndAppendScript('https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js', jqueryCallback);
createAndAppendScript('https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js', toastrCallback);
createAndAppendStyle('https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.min.css');
createAndAppendScript('https://cdn.jsdelivr.net/npm/rpg-dice-roller@4.0.2/lib/umd/bundle.min.js', dicerollerCallback);
}());
javascript:!function(){let t,e,n,i={STR:"Strength",DEX:"Dexterity",CON:"Constitution",INT:"Intelligence",WIS:"Wisdom",CHA:"Charisma",str:"Strength",dex:"Dexterity",con:"Constitution",int:"Intelligence",wis:"Wisdom",cha:"Charisma"},o={".ct-combat-attack__tohit":function(t){return t.closest(".ct-combat-attack").find(".ct-combat-attack__label").text()+" (to hit)"},".ct-damage__value":function(t){return t.closest(".ct-combat-attack, .ct-spells-spell").find(".ct-combat-attack__label, .ct-spell-name").first().text()+" ("+t.closest(".ct-combat-attack, .ct-spells-spell").find(".ct-tooltip").first().attr("data-original-title")+" damage)"},".ct-spell-damage-effect__healing":function(t){return t.closest(".ct-spells-spell, .ct-combat-attack").find(".ct-spell-name").first().text()+" (healing)"},".ct-initiative-box__value":function(){return"Initiative"},".ct-skills__list .ct-skills__col--modifier":function(t){return t.parent().find(".ct-skills__col--skill").text()+" Check"},".ct-ability-summary__secondary":function(t){return t.parent().find(".ct-ability-summary__label").text()+" Check"},".ct-saving-throws-summary__ability-modifier":function(t){return i[t.closest(".ct-saving-throws-summary__ability").find(".ct-saving-throws-summary__ability-name").text().replace(/\s/,"")]+" Save"},".ct-combat-mobile__extra--initiative .ct-combat-mobile__extra-value":function(t){return"Initiative"}};function c(t,e){var n;n=function(t,e){let n=document.createElement("script");return n.src=t,"function"==typeof e&&(n.onload=e),n}(t,e),document.head.appendChild(n)}function a(i){i.stopPropagation();let c=i.currentTarget,a=c.innerText.replace(/\s/g,"");"+"!==a[0]&&"-"!==a[0]||(a="d20"+a),function(t,i){let o=e.roll(i);n.success(o.output,`${t}: ${o.total}`)}(function(t){for(let[e,n]of Object.entries(o))if(t.is(e))return n(t)}(t(c)),a)}function s(){!function(){let t=document.createElement("style");document.head.appendChild(t);let e=t.sheet;e.insertRule("#toast-container > div { opacity: 1; }"),e.insertRule("@media (min-width: 768px) { #toast-container > div { zoom: 2; } }"),e.insertRule(`${Object.keys(o).join(", ")} { cursor: alias; }`),e.insertRule(`${Object.keys(o).join(", ")} { border-radius: 999px; box-shadow: inset 0 0 2px 1px #ff0000cc; }`),e.insertRule(`${Object.keys(o).join(":hover,")}:hover { background: #ff0000cc; }`)}(),t(document.body).on("click",Object.keys(o).join(", "),a)}function l(){void 0!==t&&void 0!==e&&void 0!==n&&s()}var r,u;c("https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js",function(){t=window.jQuery.noConflict(!0),l()}),c("https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js",function(){(n=window.toastr).options={closeButton:!0,debug:!1,newestOnTop:!1,progressBar:!0,positionClass:"toast-bottom-center",preventDuplicates:!1,onclick:null,showDuration:"300",hideDuration:"1000",timeOut:"30000",extendedTimeOut:"30000",showEasing:"swing",hideEasing:"linear",showMethod:"fadeIn",hideMethod:"fadeOut"},l()}),u=function(t,e){let n=document.createElement("link");return n.rel="stylesheet",n.href=t,"function"==typeof e&&(n.onload=e),n}("https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.min.css",r),document.head.appendChild(u),c("https://cdn.jsdelivr.net/npm/rpg-dice-roller@4.0.2/lib/umd/bundle.min.js",function(){e=new window.rpgDiceRoller.DiceRoller,l()})}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment