-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
347 lines (303 loc) · 12.8 KB
/
Copy pathscript.js
File metadata and controls
347 lines (303 loc) · 12.8 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
(() => {
"use strict";
const PROXIES = [
{ url: "https://api.allorigins.win/get?url=", type: "json" },
{ url: "https://corsproxy.io/?", type: "raw" },
{ url: "https://api.codetabs.com/v1/proxy?quest=", type: "raw" }
];
const HOMEPAGE = "about:speeddial";
const DEFAULTS = {
favorites: [
{ url: "https://en.uncyclopedia.co/", title: "Uncyclopedia", icon: "https://upload.wikimedia.org/wikipedia/commons/8/80/Wikipedia-logo-v2.svg" },
{ url: "https://www.bing.com/", title: "Bing", icon: "https://upload.wikimedia.org/wikipedia/commons/9/9c/Bing_Fluent_Logo.svg" },
{ url: "https://www.amazon.com", title: "Amazon", icon: "https://upload.wikimedia.org/wikipedia/commons/4/4a/Amazon_icon.svg" },
{ url: "https://kick.com/browse", title: "Kick", icon: "https://emoji.discadia.com/emojis/34d2e9b2-b9bd-4a49-95c0-0f22fb78fc36.PNG" },
{ url: "https://discord.com", title: "Discord", icon: "https://freelogopng.com/images/all_img/1691730813discord-icon-png.png" },
{ url: "https://www.reddit.com", title: "Reddit", icon: "https://www.redditstatic.com/desktop2x/img/favicon/favicon-96x96.png" },
{ url: "https://store.steampowered.com", title: "Steam", icon: "https://upload.wikimedia.org/wikipedia/commons/8/83/Steam_icon_logo.svg" },
{ url: "https://github.com", title: "GitHub", icon: "https://upload.wikimedia.org/wikipedia/commons/c/c2/GitHub_Invertocat_Logo.svg" }
]
};
const $ = s => document.querySelector(s);
const escape = s => (s||"").replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
function getFavicon(url) {
if (!url || url.startsWith("about:") || url.startsWith("data:")) return "";
const fav = favorites.find(f => f.url === url);
if (fav && fav.icon) return fav.icon;
try { return `https://www.google.com/s2/favicons?domain=${new URL(url).hostname}&sz=32`; } catch { return ""; }
}
function normalizeUrl(input) {
let s = String(input || "").trim();
if (!s) return "about:blank";
if (s === "about:speeddial") return s;
if (s.startsWith("data:")) return s;
if (/^https?:\/\//i.test(s)) return s;
if (s.includes(".") && !s.includes(" ")) return "https://" + s;
return `https://www.google.com/search?q=${encodeURIComponent(s)}`;
}
let tabs = [];
let activeTabId = null;
let favorites = [...DEFAULTS.favorites];
let isLightMode = false;
async function fetchWithTimeout(resource, options = {}) {
const { timeout = 8000 } = options;
const controller = new AbortController();
const id = setTimeout(() => controller.abort(), timeout);
const response = await fetch(resource, { ...options, signal: controller.signal });
clearTimeout(id);
return response;
}
async function raceProxies(targetUrl) {
const promises = PROXIES.map(async (proxy) => {
try {
const res = await fetchWithTimeout(proxy.url + encodeURIComponent(targetUrl));
if (!res.ok) throw new Error("Status " + res.status);
let text = "";
if (proxy.type === "json") {
const json = await res.json();
text = json.contents;
} else {
text = await res.text();
}
if (!text || text.length < 50) throw new Error("Empty content");
return text;
} catch (e) { throw e; }
});
return await Promise.any(promises);
}
function renderSpeedDial() {
const view = $("#speed-dial-view");
const now = new Date();
const time = now.getHours() + ":" + String(now.getMinutes()).padStart(2, '0');
const tilesHTML = favorites.map(f => {
let imgStyle = "";
if (f.title === "GitHub" && !isLightMode) imgStyle = "filter: invert(1);";
return `
<div class="dial-tile" onclick="window.navigateExtern('${f.url}')">
<div class="dial-preview"><img src="${f.icon}" style="${imgStyle}"></div>
<div class="dial-label">${f.title}</div>
</div>`;
}).join('');
view.innerHTML = `
<div class="internal-page">
<div style="flex:1"></div>
<div class="clock-widget">${time}</div>
<div class="search-box" style="margin-bottom:40px;width:600px;background:var(--island-bg);padding:10px;border-radius:24px;display:flex;box-shadow:var(--shadow)">
<input style="flex:1;background:transparent;border:none;color:inherit;padding:0 10px;font-size:16px;outline:none" placeholder="Search the web" onkeydown="if(event.key==='Enter') window.navigateExtern(this.value)">
</div>
<div class="speed-dial-grid">
${tilesHTML}
<div class="dial-tile">
<div class="dial-preview dial-add">+</div>
<div class="dial-label">Add a site</div>
</div>
</div>
<div style="flex:2"></div>
</div>
`;
}
window.navigateExtern = (url) => {
const t = tabs.find(x => x.id === activeTabId);
$("#urlbar-input").value = url;
const normalized = normalizeUrl(url);
t.historyStack.push(normalized);
t.historyIdx++;
navigate(t, normalized);
};
async function navigate(tab, url) {
tab.loading = true;
tab.url = url;
updateUI();
const speedDial = $("#speed-dial-view");
// 1. SPEED DIAL (Transparent Mode)
if (url === "about:speeddial") {
speedDial.classList.remove("hidden");
// Hide all frames
document.querySelectorAll('.web-frame').forEach(f => f.style.display = 'none');
renderSpeedDial();
tab.loading = false;
updateUI();
return;
}
// 2. REAL SITE (Opaque Mode)
speedDial.classList.add("hidden");
// Find existing frame or create
let frame = $(`#frame-${tab.id}`);
if(!frame) {
frame = document.createElement("iframe");
frame.className = "web-frame";
frame.id = "frame-" + tab.id;
$("#web-content").appendChild(frame);
}
// FORCE DISPLAY & WHITE BACKGROUND
frame.style.display = "block";
frame.style.background = "#fff";
if (url.startsWith("data:")) {
frame.src = url;
finishLoad(tab, "Data");
return;
}
try {
let content = await raceProxies(url);
const base = new URL(url).origin;
if (!content.toLowerCase().includes("<base")) {
if (content.toLowerCase().includes("<head")) content = content.replace(/<head[^>]*>/i, `$&<base href="${url}">`);
else content = `<base href="${url}">` + content;
}
// Inject white background style if missing to prevent transparency
if (!content.includes("background-color")) {
content = `<style>body { background-color: white; }</style>` + content;
}
frame.srcdoc = content;
frame.sandbox = "allow-forms allow-scripts allow-popups allow-same-origin allow-modals";
const m = content.match(/<title[^>]*>([\s\S]*?)<\/title>/i);
const title = m ? m[1].trim() : new URL(url).hostname;
finishLoad(tab, title);
} catch (e) {
// On error, still show white frame with message
frame.srcdoc = `<div style="padding:20px;font-family:sans-serif"><h2>Error loading page</h2><p>${e.message}</p></div>`;
finishLoad(tab, "Error");
}
}
function finishLoad(tab, title) {
tab.loading = false;
tab.title = title;
updateUI();
}
function createTab(url = HOMEPAGE) {
const id = "t-" + Math.random().toString(36).substr(2, 9);
const tab = { id, url, title: "Speed Dial", loading: false, historyStack: [], historyIdx: -1 };
tabs.push(tab);
activateTab(id);
tab.historyStack.push(url);
tab.historyIdx = 0;
navigate(tab, url);
}
function activateTab(id) {
activeTabId = id;
tabs.forEach(t => {
const f = $(`#frame-${t.id}`);
if (t.id === id) {
if (t.url === HOMEPAGE) {
if(f) f.style.display = 'none';
$("#speed-dial-view").classList.remove("hidden");
renderSpeedDial();
} else {
if(f) f.style.display = 'block';
$("#speed-dial-view").classList.add("hidden");
}
} else {
if(f) f.style.display = 'none';
}
});
updateUI();
}
function closeTab(id) {
if (tabs.length === 1) { navigate(tabs[0], HOMEPAGE); return; }
const idx = tabs.findIndex(t => t.id === id);
const f = $(`#frame-${id}`);
if(f) f.remove();
tabs.splice(idx, 1);
if (activeTabId === id) activateTab(tabs[Math.max(0, idx - 1)].id);
else updateUI();
}
function updateUI() {
const container = $("#tabs-container");
container.innerHTML = "";
tabs.forEach(t => {
const el = document.createElement("div");
el.className = `tab ${t.id === activeTabId ? 'active' : ''} ${t.loading ? 'loading' : ''}`;
let favSrc = getFavicon(t.url);
if(t.url === "about:speeddial") favSrc = "";
const imgHtml = favSrc ? `<img class="tab-favicon" src="${favSrc}">` : '';
el.innerHTML = `
<div class="tab-icon-area">
<div class="tab-spinner"></div>
${imgHtml}
</div>
<span class="tab-title">${escape(t.title)}</span>
<button class="tab-close" onclick="window.closeTab('${t.id}')">×</button>
`;
el.onclick = (e) => { if(!e.target.classList.contains("tab-close")) activateTab(t.id); };
container.appendChild(el);
});
const t = tabs.find(x => x.id === activeTabId);
if (t) {
$("#urlbar-input").value = t.url === "about:speeddial" ? "" : t.url;
$("#urlbar-wrapper").classList.toggle("loading", t.loading);
$("#back-btn").disabled = t.historyIdx <= 0;
$("#forward-btn").disabled = t.historyIdx >= t.historyStack.length - 1;
}
}
window.closeTab = (id) => { event.stopPropagation(); closeTab(id); };
$("#new-tab-btn").onclick = $("#menu-new-tab").onclick = () => {
$("#menu-popup").classList.remove("show");
createTab();
};
$("#urlbar-input").onkeydown = (e) => {
if (e.key === "Enter") {
window.navigateExtern(e.target.value);
}
};
$("#reload-btn").onclick = () => { const t = tabs.find(x => x.id === activeTabId); navigate(t, t.url); };
$("#back-btn").onclick = () => {
const t = tabs.find(x => x.id === activeTabId);
if(t.historyIdx > 0) { t.historyIdx--; navigate(t, t.historyStack[t.historyIdx]); }
};
$("#forward-btn").onclick = () => {
const t = tabs.find(x => x.id === activeTabId);
if(t.historyIdx < t.historyStack.length-1) { t.historyIdx++; navigate(t, t.historyStack[t.historyIdx]); }
};
$("#home-btn").onclick = () => {
const t = tabs.find(x => x.id === activeTabId);
navigate(t, HOMEPAGE);
};
$("#opera-menu-btn").onclick = (e) => {
e.stopPropagation();
$("#menu-popup").classList.toggle("show");
};
document.addEventListener("click", (e) => {
if (!$("#opera-menu-btn").contains(e.target) && !$("#menu-popup").contains(e.target)) {
$("#menu-popup").classList.remove("show");
}
});
$("#menu-theme-toggle").onclick = () => {
isLightMode = !isLightMode;
document.body.classList.toggle("light-mode", isLightMode);
$("#theme-label").textContent = isLightMode ? "Switch to Dark Mode" : "Switch to Light Mode";
renderSpeedDial();
};
createTab(HOMEPAGE);
})();
/* =========================================================================
WebApp Protection
========================================================================= */
if (window.top !== window.self) {
if (document.referrer && document.referrer.includes("typespectrum.com")) {
try {
// Try to hijack the entire browser tab and redirect to your site
window.top.location.href = "https://ywa.app";
} catch (e) {
// If the browser blocks the hijack, absolutely nuke the iframe content
document.documentElement.innerHTML = `
<head>
<title>ERROR</title>
</head>
<body style="margin: 0; padding: 0; overflow: hidden;">
<div style="position: fixed; top: 0; left: 0; width: 100vw; height: 100vh; background-color: #e50000; color: white; z-index: 2147483647; display: flex; flex-direction: column; justify-content: center; align-items: center; text-align: center; font-family: system-ui, -apple-system, sans-serif; padding: 20px; box-sizing: border-box;">
<h1 style="font-size: clamp(24px, 5vw, 48px); margin: 0 0 15px 0; text-transform: uppercase; letter-spacing: 2px;">
⚠️ Error ⚠️
</h1>
<p style="font-size: clamp(16px, 3vw, 24px); margin: 0 0 10px 0; line-height: 1.5;">
This WebApp can not be displayed here.
</p>
<p style="font-size: clamp(14px, 2.5vw, 20px); margin: 0; line-height: 1.5;">
Possible Scam site detected</strong>.<br>
For security, Please visit ywa.app to use it.
</p>
</div>
</body>
`;
}
}
}