-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathLoad.js
More file actions
216 lines (206 loc) · 8.37 KB
/
Load.js
File metadata and controls
216 lines (206 loc) · 8.37 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
(async function() { //don't leave clutter after load
const runtime = (chrome || browser).runtime; //detect extension context
function pageType(location) { // page type from location: [char, gamelog, campaign, vtt-player, vtt-dm, null]
//match campaign page exactly in case other pages ever get added like campaigns/join that we want to exclude
const isCampaignPage = location.pathname.match(/\/campaigns\/[0-9]+$/gi);
const isVttGamePage = location.search.includes("abovevtt=true");
const isPlayerPage = location.pathname.match("/characters");
const isPlainCharacterPage = !isVttGamePage && isPlayerPage //character, builder, or listing
const isDM = isCampaignPage && location.search.includes("dm=true");
const isPopoutGameLog = location.search.includes("popoutgamelog=true");
if(isPlainCharacterPage) return "char";
if(isPopoutGameLog && !isDM) return "gamelog";
if(isCampaignPage && !isVttGamePage) return "campaign";
if(isVttGamePage) return "vtt-" + ((isPlayerPage && !isDM) ? "player" : "dm");
return null;
}
const pgType = pageType(window.location); //only relevant in extension context;
const isIframe = window.self !== window.top;
if(runtime) { //we are in the extension context - decide whether to bail quickly
//with this scheme we should never load in iframes from here
console.log("🎲 AVTT extension: ", pgType);
if(!pgType || isIframe) { //block iFrame extension loading (eg older Safari) until we want it
console.log("⛔ AVTT: no extension loading here.")
return; //don't load anything
}
}
//setup to work in both contexts
const getExtURL = runtime ? ((url) => runtime.getURL(url))
: ((url) => document.querySelector("#extensionpath").dataset.path + url);
const getExtVersion = runtime ? (() => (chrome||browser).runtime?.getManifest()?.version)
: ((url) => document.querySelector("#avttversion").dataset.path);
const addChild = (c, where, head=false) => (where[head ? "head" : "body"] || where.documentElement).appendChild(c);
const avttScripts = [
// External Dependencies
"jquery-3.6.0.min.js",
"jquery-ui.min.js",
"jquery.csv.js",
"jquery.ui.touch-punch.js",
"jquery.contextMenu.js",
"jquery.magnific-popup.min.js",
"spectrum-2.0.8.min.js",
"purify.min.js",
"rpg-dice-roller.bundle.min.js",
"color-picker.js",
"mousetrap.1.6.5.min.js",
"peerjs.min.js",
"fuse.min.js",
"ajaxQueue/ajaxQueueIndex.mjs",
// AboveVTT Files
"environment.js",
"CoreFunctions.js", // Make sure CoreFunctions executes before anything else
"avttS3Upload.js",
"AboveApi.js",
"DDBApi.js",
"AOETemplates.js",
"Text.js",
"CombatTracker.js",
"EncounterHandler.js",
"Fog.js",
"Journal.js",
"KeypressHandler.js",
"MessageBroker.js",
"MonsterDice.js",
"PlayerPanel.js",
"ScenesHandler.js",
"ScenesPanel.js",
"Settings.js",
"SidebarPanel.js",
"StatHandler.js",
"Token.js",
"constants/names.js",
"TokenMenu.js",
"ChatObserver.js",
"DiceContextMenu/DiceContextMenu.js",
"TokensPanel.js",
"TokenCustomization.js",
"built-in-tokens.js",
"PeerManager.js",
"PeerCommunication.js",
"peerVideo.js",
"peerDice.js",
"DiceRoller.js",
"DMScreen.js",
"Main.js",
"MonsterStatBlock.js",
// AboveVTT files that execute when loaded
"onedrive/onedrivemsal.js",
"onedrive/onedrivepicker.js",
"audio/index.mjs",
"WeatherOverlay.js"
]
const avttCharacterScripts = [
"Load.js", //load Loader on character sheets to support DBB Character Overhaul Extension
// External Dependencies
"jquery-3.6.0.min.js",
"jquery.contextMenu.js",
"purify.min.js",
"ajaxQueue/ajaxQueueIndex.mjs",
// AboveVTT Files
"CoreFunctions.js", // Make sure CoreFunctions executes first
"DDBApi.js",
"MonsterDice.js",
"DiceRoller.js",
"DiceContextMenu/DiceContextMenu.js",
"MessageBroker.js",
"rpg-dice-roller.bundle.min.js",
// AboveVTT files that execute when loaded
"CharactersPage.js" // Make sure CharactersPage executes last
];
const avttStyles = [
"abovevtt.css",
"jquery-ui.min.css",
"jquery.ui.theme.min.css",
"jquery.contextMenu.css",
"color-picker.min.css",
"spectrum-2.0.8.min.css",
"magnific-popup.css",
"DiceContextMenu/DiceContextMenu.css"
];
const simpleAvttStyles = [
"DiceContextMenu/DiceContextMenu.css", "jquery.contextMenu.css"
]
function injectStyles(styles, where) {
for(const value of styles) {
const l = where.createElement('link');
l.href = getExtURL(value);
l.rel = "stylesheet";
console.log(`🎨 Loading Style ${value}`);
addChild(l, where, true);
}
}
async function injectScripts(scriptsToLoad, where) {
for (const script of scriptsToLoad) {
await new Promise((resolve, reject) => {
const s = where.createElement('script');
s.src = getExtURL(script);
if(script.endsWith(".mjs")) s.type = "module";
s.onload = resolve;
s.onerror = () => reject(new Error(`Could not find or load: ${script}`));
addChild(s, where, true);
});
console.log(`✅ Loaded: ${script}`);
//could catch errors here - but it doesn't help much in web extensions
}
}
async function inject(pgType, where) {
console.log("⌛ AVTT Loading", pgType, (isIframe && window.parent) ? ("parent: " + pageType(window.parent.location)) : "");
if(pgType.startsWith("vtt-")) {
const loadingOverlay = where.createElement('div');
loadingOverlay.id = "loading_overlay";
const loadingBg = where.createElement('div');
loadingBg.id = "loading_overlay_bg";
loadingBg.className = pgType.endsWith("-dm") ? "dm" : "player";
loadingOverlay.appendChild(loadingBg);
addChild(loadingOverlay, where);
}
const l = where.createElement('div');
l.id = "extensionpath"
l.style.display = "none";
l.setAttribute("data-path", getExtURL("/"));
addChild(l, where);
const avttVersion = where.createElement('div');
avttVersion.id = "avttversion";
avttVersion.style.display = "none";
avttVersion.setAttribute("data-version", getExtVersion());
addChild(avttVersion, where);
injectStyles(pgType === "char" ? simpleAvttStyles : avttStyles, where);
const scripts = pgType === "char" ?
avttCharacterScripts
: pgType === "gamelog" ? [
"jquery.magnific-popup.min.js",
"purify.min.js",
"environment.js",
"CoreFunctions.js",
"rpg-dice-roller.bundle.min.js",
"DiceContextMenu/DiceContextMenu.js",
"DiceRoller.js",
"DDBApi.js",
"Settings.js",
"MessageBroker.js",
"Journal.js",
"ChatObserver.js",
"MonsterStatBlock.js",
"MonsterDice.js",
"CampaignPage.js"
] : pgType === "campaign" ? [
"environment.js",
"CoreFunctions.js",
"DDBApi.js",
"Settings.js",
"CampaignPage.js"
] : [...avttScripts,
"Load.js", //load Loader on VTT full pages (for iframe inject - see below)
(pgType.endsWith("-dm") ? "SceneData.js" : "CharactersPage.js"),
];
if(pgType.startsWith("vtt-")) scripts.push("Startup.mjs");
await injectScripts(scripts, where);
console.log("🏁 AVTT: done loading",pgType);
}
if(runtime) { //extension loader context
await inject(pgType, document);
} else {
//in page context install loader on window for iframe injects later
window.AVTT_INJECT = inject;
}
}());