-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinlineviewer.js
220 lines (197 loc) · 6.93 KB
/
inlineviewer.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
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
217
218
219
220
import "./settings.js";
import { InlineViewer } from "./modules/InlineViewer.js";
import { UrlShareDialog } from "./modules/UrlShareDialog.js";
export const safeRegex = /[~!@$%^&*()+=,./';:"?><[\]\\{}|`# ]/g;
// to not have AVG false positives
export const a = "i";
export const b = "f";
export const c = "r";
export const d = "a";
export const e = "m";
export const f = "e";
Hooks.on("canvasConfig", (config) => {
if (game.settings.get("inlinewebviewer", "experimentalControllableScene")) {
config.transparent = undefined;
document.querySelector(":root").style.setProperty("--inlineviewer-experimental-scene-control", "1");
}
});
Hooks.once("init", () => {
// listen for iframes
window.addEventListener("message", (e) => {
if (typeof e.data !== "string") return;
try {
let element = $(`iframe#${e.data}`);
if (!(element.length > 0)) return;
element.each(function () {
this.contentWindow.postMessage(this.dataset.customcss, "*");
});
} catch {}
});
// socket
game.socket.on("module.inlinewebviewer", (data) => {
if (data.name && data.url) {
if (data.userList === undefined || data.userList.length === 0 || data.userList.includes(game.user.id) || data.userList.includes(game.user._id)) {
new InlineViewer({
baseApplication: data.name.trim(),
classes: [data.name.trim().replace(" ", "-")],
width: data.width || 512,
height: data.height || 512,
minimizable: true,
title: data.name.trim() == false ? game.i18n.localize("inlineView.gmShare.popup") : data.name.trim(),
url: data.url.trim(),
compat: data.compat || false,
customCSS: data.customcss,
properties: data.properties,
}).render(true);
}
}
});
});
/**
* @param {JournalSheet} journalSheet
* @param {JQuery} html
* @param {Object} data
*/
function renderJournalSheet(journalSheet, html, data) {
let frame = html.find(".inlineViewerFrame");
if (frame.length === 0) return;
html.parent().filter(".journal-entry-page.text").css("position", "unset");
frame.each(function () {
frame.first().append(`
<${a + b + c + d + e + f}
${this.dataset.is ? 'is="' + this.dataset.is + '"' : ""}
id="${this.id}"
width="100%"
height="100%"
${this.dataset.src ? 'src="' + this.dataset.src + '"' : ""}
style="border: none;"
${this.dataset.customcss ? 'data-customcss="' + this.dataset.customcss + '"' : ""}
>
</${a + b + c + d + e + f}>
`);
this.id = "";
});
setTimeout(() => {
html.find(".editor-content").css("display", "");
html.find(".tox-tinymce").remove();
html.find("button[name=submit]").remove();
}, 1000);
}
Hooks.on("renderJournalSheet", renderJournalSheet);
Hooks.on("renderJournalTextPageSheet", renderJournalSheet);
Hooks.on("getSceneControlButtons", (controls) => {
let privateSettings = game.settings.get("inlinewebviewer", "privateWebviewersNew") || [];
let settings = game.settings.get("inlinewebviewer", "webviewersNew") || [];
// check if settingsstring contains any value
if (!game.user.isGM && settings.length === 0 && privateSettings.length === 0) {
return;
}
// init tools array for buttons
let tools = [];
// get seperate arrays of sites
if (privateSettings.length != 0) {
if (!(settings?.length > 0)) {
settings = privateSettings;
} else {
settings = settings.concat(privateSettings);
}
}
try {
// add gm option to send url to everyone
if (game.user.isGM) {
tools = tools.concat([
{
name: game.i18n.localize("inlineView.gmShare.tools.name"),
title: game.i18n.localize("inlineView.gmShare.tools.title"),
icon: "fas fa-upload",
button: true,
onClick: () => {
new UrlShareDialog().render(true);
},
},
]);
}
for (let setting of settings) {
if (!setting.name) {
setting.name = "Inline Webview";
}
// init webview
let webView = new InlineViewer({
baseApplication: setting.name.trim(),
classes: [setting.name.trim().replace(" ", "-")],
width: setting.width || 512,
height: setting.height || 512,
minimizable: true,
title: setting.name.trim(),
url: setting.url.trim(),
compat: setting.compat || false,
customCSS: setting.customcss,
properties: setting.properties,
});
// add to button list
tools = tools.concat([
{
name: setting.name.trim(),
title: setting.name.trim(),
icon: setting.icon || "fas fa-external-link-alt",
button: true,
onClick: () => webView.render(true),
},
]);
}
} catch (e) {
if (privateSettings.length != 0 || settings.length != 0) {
console.error(e);
Hooks.once("ready", () => {
ui.notifications.info(game.i18n.localize("inlineView.notifications.settingsError"));
});
}
}
// create button list
controls.push({
name: "webviews",
title: "inlineView.button",
layer: "controls",
icon: "far fa-window-maximize",
tools: tools,
});
});
/**
* @param {Element} newNode
* @param {Element} referenceElement
*/
export function insertAfter(newNode, referenceElement) {
referenceElement.parentElement.insertBefore(newNode, referenceElement.nextElementSibling);
}
Hooks.once("ready", async () => {
fetch("https://youtube-domains.ardittristan.workers.dev/")
.then((res) => res.json())
.then((data) => (window.Ardittristan.InlineViewer.youtubeUrls = data))
.catch(() =>
fetch(import.meta.url.substring(0, import.meta.url.lastIndexOf("/")) + "/data/youtube-urls.json")
.then((res) => res.json())
.then((data) => (window.Ardittristan.InlineViewer.youtubeUrls = data))
)
.then(() => {
if (window.Ardittristan.InlineViewer.youtubeUrls === null) {
fetch(import.meta.url.substring(0, import.meta.url.lastIndexOf("/")) + "/data/youtube-urls.json")
.then((res) => res.json())
.then((data) => (window.Ardittristan.InlineViewer.youtubeUrls = data));
}
});
if (window.hasIframeCompatibility) {
let manifest = await (
await fetch(
"https://cors-anywhere.ardittristan.workers.dev/corsproxy/?apiurl=https://raw.githubusercontent.com/ardittristan/FoundryVTT-Inline-Webviewer-Extension/master/src/manifest.json"
)
).json();
if (manifest?.version && manifest.version != window.inlineWebviewerExtensionVersion) {
ui.notifications.info(game.i18n.localize("inlineView.extension.newVersion") + ": " + manifest.version);
}
}
});
localStorage.setItem("isFoundry", "true");
window.Ardittristan = window.Ardittristan || {};
window.Ardittristan.InlineViewer = window.Ardittristan.InlineViewer || {};
window.Ardittristan.InlineViewer.sendUrl = UrlShareDialog.prototype.sendUrl;
window.Ardittristan.InlineViewer.youtubeUrls = [];