-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
288 lines (245 loc) · 11 KB
/
test.js
File metadata and controls
288 lines (245 loc) · 11 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Advanced Embed.js Library with Scrollbar Customization
(function (global) {
class Embed {
constructor() {
this.frames = [];
}
// Add a new frame with custom options
create(options) {
const defaults = {
size: { width: "600px", height: "400px" },
source: "https://thejupitergroup.wixstudio.com/iframe",
uuid: this.generateUUID(),
parent: document.body,
styles: {},
allow: "", // Allow attributes like "fullscreen", "camera", etc.
sandbox: "", // Sandbox attributes like "allow-scripts"
title: "Embedded Frame",
className: "", // Custom CSS class
preventRedirects: false, // Block iframe navigation/redirects
onLoad: null, // onLoad callback
onPreventedRedirect: null, // Callback for prevented redirects
alignment: "left", // Placement: "left", "center", or "right"
scrollbar: { // Default custom scrollbar settings
width: "8px", // Scrollbar width
color: "#888", // Scrollbar color
hoverColor: "#555", // Scrollbar color on hover
backgroundColor: "rgba(0, 0, 0, 0.1)", // Scrollbar background color
hideOnIdle: false, // Hide scrollbar when not interacting
}
};
// Merge user options with defaults
const config = { ...defaults, ...options };
// Create the iframe container for alignment
const iframeContainer = document.createElement("div");
iframeContainer.style.display = "flex";
iframeContainer.style.justifyContent = this.getAlignment(config.alignment);
iframeContainer.style.margin = "10px 0";
// Create the iframe
const iframe = document.createElement("iframe");
iframe.src = config.source;
iframe.width = config.size.width.replace("px", "");
iframe.height = config.size.height.replace("px", "");
iframe.id = config.uuid;
iframe.title = config.title;
iframe.style.border = "0";
// Apply custom styles
Object.entries(config.styles).forEach(([key, value]) => {
iframe.style[key] = value;
});
// Apply custom scrollbar styles (for same-origin iframes)
if (iframe.src.startsWith(window.location.origin)) {
this.applyScrollbarStyles(iframe, config.scrollbar);
} else {
// For cross-origin, we can't modify the iframe's content directly, so we style the iframe itself.
this.applyIframeScrollbar(iframe, config.scrollbar);
}
// Add class name
if (config.className) {
iframe.className = config.className;
}
// Set allow and sandbox attributes
if (config.allow) iframe.setAttribute("allow", config.allow);
if (config.sandbox) iframe.setAttribute("sandbox", config.sandbox);
// Append iframe to the container
iframeContainer.appendChild(iframe);
// Append to the specified parent
if (config.parent instanceof HTMLElement) {
config.parent.appendChild(iframeContainer);
} else {
console.warn("Invalid parent element. Using document.body instead.");
document.body.appendChild(iframeContainer);
}
// Bind the onLoad event if provided
if (typeof config.onLoad === "function") {
iframe.onload = () => config.onLoad(iframe);
}
// Prevent redirects if enabled
if (config.preventRedirects) {
this.preventIframeRedirects(iframe, config.onPreventedRedirect);
}
// Store the frame's configuration
this.frames.push({ uuid: config.uuid, iframe, config, container: iframeContainer });
return iframe;
}
// Apply custom scrollbar styles for same-origin iframe
applyScrollbarStyles(iframe, scrollbar) {
const iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
const style = iframeDoc.createElement("style");
style.textContent = `
/* Custom scrollbar styles */
::-webkit-scrollbar {
width: ${scrollbar.width};
height: ${scrollbar.width}; /* For horizontal scrollbar */
}
::-webkit-scrollbar-thumb {
background-color: ${scrollbar.color};
border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover {
background-color: ${scrollbar.hoverColor};
}
::-webkit-scrollbar-track {
background-color: ${scrollbar.backgroundColor};
}
/* Optionally hide the scrollbar when idle */
${scrollbar.hideOnIdle ? `body {
overflow: hidden;
/* Enable dynamic visibility when user interacts */
}` : ""}
`;
iframeDoc.head.appendChild(style);
}
// Apply scrollbar styles to the iframe itself (for cross-origin iframes)
applyIframeScrollbar(iframe, scrollbar) {
iframe.style.scrollbarWidth = scrollbar.width; // For Firefox
iframe.style.scrollbarColor = `${scrollbar.color} ${scrollbar.backgroundColor}`; // For Firefox
iframe.style.webkitScrollbar = scrollbar.width; // For Chrome/Safari
iframe.style.webkitScrollbarThumb = scrollbar.color; // For Chrome/Safari
iframe.style.webkitScrollbarTrack = scrollbar.backgroundColor; // For Chrome/Safari
}
// Update iframe properties dynamically
update(uuid, options) {
const frame = this.get(uuid);
if (!frame) {
console.warn(`No frame found with UUID: ${uuid}`);
return false;
}
const iframe = frame.iframe;
// Update source
if (options.source) iframe.src = options.source;
// Update size
if (options.size) {
iframe.width = options.size.width.replace("px", "");
iframe.height = options.size.height.replace("px", "");
}
// Update styles
if (options.styles) {
Object.entries(options.styles).forEach(([key, value]) => {
iframe.style[key] = value;
});
}
// Update alignment
if (options.alignment) {
const container = frame.container;
container.style.justifyContent = this.getAlignment(options.alignment);
}
// Update scrollbar options
if (options.scrollbar) {
if (iframe.src.startsWith(window.location.origin)) {
this.applyScrollbarStyles(iframe, options.scrollbar);
} else {
this.applyIframeScrollbar(iframe, options.scrollbar);
}
}
// Update allow and sandbox attributes
if (options.allow) iframe.setAttribute("allow", options.allow);
if (options.sandbox) iframe.setAttribute("sandbox", options.sandbox);
// Update configuration in stored frames
frame.config = { ...frame.config, ...options };
return true;
}
// Remove a frame by its UUID
remove(uuid) {
const frameIndex = this.frames.findIndex((frame) => frame.uuid === uuid);
if (frameIndex !== -1) {
const frame = this.frames[frameIndex];
frame.container.remove(); // Remove the container and iframe
this.frames.splice(frameIndex, 1);
return true;
} else {
console.warn(`No frame found with UUID: ${uuid}`);
return false;
}
}
// Get a frame by its UUID
get(uuid) {
return this.frames.find((frame) => frame.uuid === uuid) || null;
}
// List all frames
list() {
return this.frames;
}
// Resize a frame by its UUID
resize(uuid, size) {
const frame = this.get(uuid);
if (!frame) {
console.warn(`No frame found with UUID: ${uuid}`);
return false;
}
const iframe = frame.iframe;
iframe.width = size.width.replace("px", "");
iframe.height = size.height.replace("px", "");
// Update configuration in stored frames
frame.config.size = size;
return true;
}
// Get CSS justify-content value for alignment
getAlignment(alignment) {
switch (alignment.toLowerCase()) {
case "center":
return "center";
case "right":
return "flex-end";
case "left":
default:
return "flex-start";
}
}
// Prevent iframe redirects
preventIframeRedirects(iframe, onPreventedRedirect) {
iframe.addEventListener("load", () => {
const iframeWindow = iframe.contentWindow;
// Override window.open to block new tabs or windows
iframeWindow.open = function () {
console.warn("Redirects are disabled inside the iframe.");
if (typeof onPreventedRedirect === "function") {
onPreventedRedirect("Blocked an attempt to open a new window.");
}
return null;
};
// Monitor changes to iframe's location
const originalLocation = iframeWindow.location.href;
const observer = setInterval(() => {
if (iframeWindow.location.href !== originalLocation) {
console.warn("Redirect attempt detected and prevented.");
iframeWindow.location.href = originalLocation; // Revert to original location
if (typeof onPreventedRedirect === "function") {
onPreventedRedirect("Blocked an attempt to navigate away.");
}
}
}, 100);
});
}
// Generate a unique identifier
generateUUID() {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
const r = (Math.random() * 16) | 0;
const v = c === "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}
}
// Attach the library to the global scope
global.embed = new Embed();
})(window);