-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroll20.userscript.js
More file actions
128 lines (110 loc) · 4.21 KB
/
Copy pathroll20.userscript.js
File metadata and controls
128 lines (110 loc) · 4.21 KB
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
// ==UserScript==
// @name Roll20 7th Sea Dice Helper
// @namespace https://petergeneric.github.io
// @version 1.4
// @description Roll20 7th Sea Dice Rolling Solver Integration Plugin
// @author Peter Wright
// @match https://app.roll20.net/editor
// @match https://app.roll20.net/editor/
// @downloadURL https://petergeneric.github.io/7thsea-dice-solver/roll20.userscript.js
// @updateURL https://petergeneric.github.io/7thsea-dice-solver/roll20.userscript.js
// @grant none
// @inject-into page
// ==/UserScript==
(function () {
'use strict';
function init7thSeaRollingPlugin() {
const SLEEP_TIME = 5000;
const DESIRED_SHEET_NAME = '7thsea2e';
const DEBUG = false;
let attempts = 0;
function initialise() {
if (attempts++ > 60) {
if(DEBUG) console.log('[7th Sea Plugin] Could not find system and #textchat in time, giving up');
return;
}
const chatbox = document.getElementById('textchat');
const charsheetname = window.PRIMARY_CHARSHEET_NAME || null;
if (!charsheetname) {
if(DEBUG) console.log('[7th Sea Plugin] PRIMARY_CHARSHEET_NAME not found, retrying...');
setTimeout(initialise, SLEEP_TIME);
}
else if (!chatbox) {
if(DEBUG) console.log('[7th Sea Plugin] #textchat not found, retrying shortly...');
setTimeout(initialise, SLEEP_TIME);
}
else if (charsheetname !== DESIRED_SHEET_NAME) {
if(DEBUG) console.log(`[7th Sea Plugin] Does not appear to be 7th Sea (got ${charsheetname}, want ${DESIRED_SHEET_NAME}). Plugin quitting.`);
}
else {
if(DEBUG) console.log('[7th Sea Plugin] 7th Sea detected. Setting up chat monitoring...');
function linkify(el) {
function cleanText(text) {
return text.replace(/[^0-9+,]/g, '').replace(/\+/g, ',');
}
function getSkillRank(el) {
try {
const formulaEl = el.closest('.message')?.querySelector('.formula:not(.formattedformula)');
if (!formulaEl) return null;
const match = formulaEl.textContent.match(/\((\d+)/);
return match ? parseInt(match[1], 10) : null;
} catch (e) {
return null;
}
}
if (el.style.cursor === 'help')
return;
else
el.style.cursor = 'help';
el.addEventListener('click', (e) => {
e.preventDefault();
const params = new URLSearchParams();
params.set('dice', cleanText(el.innerText));
const skillRank = getSkillRank(el);
if (skillRank !== null && skillRank >= 4) {
params.set('mode', 'fifteens');
}
window.open(`https://petergeneric.github.io/7thsea-dice-solver/#${params}`, '_blank').opener = null;
});
}
document.querySelectorAll('.formula.formattedformula').forEach(linkify);
new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
mutation.addedNodes.forEach((node) => {
if (node.nodeType === 1 && node.matches('.formula.formattedformula'))
linkify(node);
if (node.querySelectorAll)
node.querySelectorAll('.formula.formattedformula').forEach(linkify);
});
});
}).observe(chatbox, {
childList: true,
subtree: true
});
}
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initialise);
}
else {
initialise();
}
}
// Inject script directly into page context
const script = document.createElement('script');
script.textContent = `
${init7thSeaRollingPlugin.toString()}
init7thSeaRollingPlugin();
`;
// Required for Tampermonkey loading
(document.head || document.documentElement).appendChild(script);
script.remove();
// Required for Violentmonkey page loading
// N.B. this will also run for Tampermonkey and if DEBUG=true will produce confusing double logs
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init7thSeaRollingPlugin);
}
else {
init7thSeaRollingPlugin();
}
})();