-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfallback.js
More file actions
174 lines (155 loc) · 4 KB
/
fallback.js
File metadata and controls
174 lines (155 loc) · 4 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
(() => {
if (window.Fallback) return;
const style = document.createElement("style");
style.textContent = `
.fb-overlay {
position: fixed;
inset: 0;
background: rgba(0,0,0,.4);
backdrop-filter: blur(6px);
display: flex;
align-items: center;
justify-content: center;
z-index: 999999;
font-family: system-ui,-apple-system,BlinkMacSystemFont;
}
.fb-card {
background: white;
width: 340px;
border-radius: 18px;
padding: 20px;
box-shadow: 0 30px 60px rgba(0,0,0,.25);
}
.fb-title {
font-weight: 600;
font-size: 16px;
margin-bottom: 6px;
}
.fb-text {
font-size: 14px;
color: #555;
line-height: 1.5;
}
.fb-actions {
margin-top: 16px;
display: flex;
justify-content: flex-end;
gap: 12px;
}
.fb-btn {
background: #007aff;
color: white;
border: none;
border-radius: 10px;
padding: 6px 14px;
font-size: 14px;
cursor: pointer;
}
.fb-btn.secondary {
background: transparent;
color: #007aff;
}
.fb-banner {
position: fixed;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
background: rgba(255,255,255,.85);
backdrop-filter: blur(10px);
border-radius: 14px;
padding: 8px 14px;
font-size: 13px;
color: #444;
box-shadow: 0 8px 20px rgba(0,0,0,.12);
z-index: 999999;
}
.fb-img-fallback {
display: flex;
align-items: center;
justify-content: center;
background: #f2f2f2;
color: #777;
font-size: 12px;
border-radius: 12px;
}
`;
document.head.appendChild(style);
let overlayVisible = false;
let offlineBanner;
function showOverlay(title, text) {
if (overlayVisible) return;
overlayVisible = true;
const overlay = document.createElement("div");
overlay.className = "fb-overlay";
overlay.innerHTML = `
<div class="fb-card" role="dialog" aria-modal="true">
<div class="fb-title">${title}</div>
<div class="fb-text">${text}</div>
<div class="fb-actions">
<button class="fb-btn secondary">Close</button>
<button class="fb-btn">Reload</button>
</div>
</div>
`;
const close = () => {
overlayVisible = false;
overlay.remove();
};
overlay.querySelector(".secondary").onclick = close;
overlay.querySelector(".fb-btn:not(.secondary)").onclick = () =>
location.reload();
overlay.addEventListener("click", e => {
if (e.target === overlay) close();
});
document.addEventListener(
"keydown",
e => e.key === "Escape" && close(),
{ once: true }
);
document.body.appendChild(overlay);
}
window.onerror = () =>
showOverlay(
"Unexpected error",
"This page encountered an error. Reloading may help."
);
window.addEventListener("unhandledrejection", () =>
showOverlay(
"Background error",
"A background task failed. Reloading may help."
)
);
document.addEventListener(
"error",
e => {
const el = e.target;
if (el.tagName === "IMG") {
const wrapper = document.createElement("div");
wrapper.style.display = "flex";
wrapper.style.alignItems = "center";
wrapper.style.justifyContent = "center";
wrapper.style.width = (el.width || 240) + "px";
wrapper.style.height = (el.height || 160) + "px";
wrapper.className = "fb-img-fallback";
wrapper.textContent = "Image unavailable";
el.replaceWith(wrapper);
}
},
true
);
function showOffline() {
if (offlineBanner) return;
offlineBanner = document.createElement("div");
offlineBanner.className = "fb-banner";
offlineBanner.textContent = "Offline mode";
document.body.appendChild(offlineBanner);
}
function hideOffline() {
offlineBanner?.remove();
offlineBanner = null;
}
window.addEventListener("offline", showOffline);
window.addEventListener("online", hideOffline);
if (!navigator.onLine) showOffline();
window.Fallback = { version: "0.2.0" };
})();