forked from mivalsten/ddb-dm-screen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddb-dm-screen.user.js
181 lines (161 loc) · 5.62 KB
/
ddb-dm-screen.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// ==UserScript==
// @name ddb-dm-screen
// @namespace https://github.com/mivalsten/ddb-dm-screen
// @version 2.0.5
// @description Poor man's DM screen for DDB campaigns
// @author Mivalsten
// @match https://www.dndbeyond.com/campaigns/*
// @grant none
// @license MIT; https://github.com/mivalsten/ddb-dm-screen/blob/master/LICENSE
// ==/UserScript==
//
var $ = window.jQuery;
class Character {
constructor(name) {
this.name = name;
};
get level() {
var classes = this.iframe.find('.ct-character-tidbits__classes').text().split('/').map(function(i){return parseInt(i.replace(/[^0-9]+/g, ''))});
return classes.reduce((a, b) => a + b, 0);
}
get proficiency() {
return Math.ceil(this.level / 4) + 1;
}
get id(){
return this.name.replace(/[^0-9a-zA-Z]+/g, '');
}
get iframe(){
return $(`#frame-${this.id}`).contents();
}
get ac(){
return parseInt(this.iframe.find(".ct-armor-class-box__value").text());
}
get currentHP(){
return parseInt(this.iframe.find(".ct-status-summary-mobile__hp-current").text());
}
get maxHP(){
return parseInt(this.iframe.find(".ct-status-summary-mobile__hp-max").text());
}
get passivePerception(){
var selector = ".ct-senses .ct-senses__callout:has(.ct-senses__callout-label:contains(Perception))";
return parseInt(this.iframe.find(selector).find(".ct-senses__callout-value").text());
}
get passiveInsight(){
var selector = ".ct-senses .ct-senses__callout:has(.ct-senses__callout-label:contains(Insight))";
return parseInt(this.iframe.find(selector).find(".ct-senses__callout-value").text());
}
get passiveInvestigation(){
var selector = ".ct-senses .ct-senses__callout:has(.ct-senses__callout-label:contains(Investigation))";
return parseInt(this.iframe.find(selector).find(".ct-senses__callout-value").text());
}
get stats(){
var stats = {};
var iframe = this.iframe;
iframe.find('.ct-ability-summary').each(function(index){
let name = $(this).find('.ct-ability-summary__abbr').text();
stats[name] = {
value: Math.max(
parseInt($(this).find('.ct-ability-summary__primary').text()),
parseInt($(this).find('.ct-ability-summary__secondary').text())
),
modifier: Math.min(
parseInt($(this).find('.ct-ability-summary__primary').text()),
parseInt($(this).find('.ct-ability-summary__secondary').text())
),
savingThrow: iframe.find(`.ct-saving-throws-summary__ability--${name} .ct-signed-number`).text()
};
});
return stats;
}
get skills(){
var skills = {};
this.iframe.find('.ct-skills__item').each(function() {
var name = $(this).children('.ct-skills__col--skill').text();
var value = $(this).children('.ct-skills__col--modifier').text();
skills[name] = value;
});
return skills;
}
};
function render(character, node){
var tableId = `character-details-${character.id}`;
var div = `
<div>
<table id="${tableId}">
<thead>
<tr>
<th></th>
<th align="center">Value</th>
<th align="center">Modifier</th>
<th align="center">Saving throw</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
`;
var statRow = `
<tr>
<th>title</th>
<td align="center">value</td>
<td align="center">mod</td>
<td align="center">save</td>
</tr>
`;
var otherRow = `
<tr>
<th>name</th>
<td align="center">value</td>
<td></td><td></td>
</tr>
`;
node.parents('.ddb-campaigns-character-card').after(div);
var footer = $(`#${tableId} > tbody:last-child`);
for(var s in character.stats){
var text = statRow
.replace("title", s.toUpperCase())
.replace("value", character.stats[s].value)
.replace("mod", character.stats[s].modifier)
.replace("save", character.stats[s].savingThrow);
footer.append(text);
}
otherInfo = {
"Proficiency": `+${character.proficiency}`,
"HP": `${character.currentHP} / ${character.maxHP}`,
"AC": character.ac,
"Passive Investigation": character.passiveInvestigation,
"Passive Perception": character.passivePerception,
"Passive Insight": character.passiveInsight
}
for (name in otherInfo){
footer.append(otherRow.replace("name", name).replace("value", otherInfo[name]));
}
}
function prerender(character, node, times) {
if (!isNaN(character.ac)) {render(character, node);}
else {
times += 1;
if (times < 80) {setTimeout(function() {prerender(character, node, times);}, 500);};
}
}
(function() {
$('#site').after('<div id="iframeDiv" style="opacity: 0; position: absolute;"></div>'); //visibility: hidden;
let chars = $('.ddb-campaigns-detail-body-listing-active').find('.ddb-campaigns-character-card-footer-links-item-view');
chars.each(function(index, value) {
let node = $(this);
let name = node.parents('.ddb-campaigns-character-card').find('.ddb-campaigns-character-card-header-upper-character-info-primary').text();
let character = new Character(name);
let newIframe = document.createElement('iframe');
//after loading iframe, wait for a second to let JS create content.
newIframe.onload = function(){prerender(character, node, 0)};
newIframe.id = `frame-${character.id}`;
newIframe.style = "position: absolute;"; //visibility: hidden;
newIframe.width = 1000;
newIframe.height = 1;
newIframe.seamless = "";
newIframe.src = node.attr('href');
document.body.appendChild(newIframe);
$('#iframeDiv').append(newIframe);
}
);
})();