-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolving.js
More file actions
217 lines (182 loc) · 6.42 KB
/
Copy pathresolving.js
File metadata and controls
217 lines (182 loc) · 6.42 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
(async () => {
const params = new URLSearchParams(location.search);
const token = params.get("token");
const original = params.get("url");
// UI refs
const spinner = document.getElementById("spinner");
const checkIcon = document.getElementById("check-icon");
const headline = document.getElementById("headline");
const subtitle = document.getElementById("subtitle");
const sourceUrl = document.getElementById("source-url");
const progressFill = document.getElementById("progress-fill");
const progressStatus= document.getElementById("progress-status");
const progressTime = document.getElementById("progress-time");
const progressWrap = document.getElementById("progress-wrap");
const resultEl = document.getElementById("result");
const finalUrlText = document.getElementById("final-url-text");
const variantsEl = document.getElementById("variants");
if (original) {
sourceUrl.textContent = original;
document.title = `Resolving ${original}`;
}
// Progress animation — fills to 90% over ~4s, then jumps to 100% on done
const startAt = Date.now();
let done = false;
(function tick() {
const elapsed = (Date.now() - startAt) / 1000;
const pct = done ? 100 : Math.min((1 - Math.pow(1 - Math.min(elapsed / 4, 1), 3)) * 90, 90);
progressFill.style.width = pct + "%";
progressTime.textContent = elapsed.toFixed(1) + "s";
if (!done) requestAnimationFrame(tick);
})();
progressStatus.textContent = "Following redirects…";
// Tracking params to strip (utm_* caught by prefix check below)
const TRACKERS = new Set([
"fbclid","gclid","gclsrc","dclid","gbraid","wbraid",
"msclkid","mc_eid","mc_cid","twclid","igshid","s_cid","yclid",
"ref","referral","source","affiliate","_ga","_gl",
]);
/**
* Decodes a URL that contains mixed HTML entities and percent-encoding
* into the final clean URL that the browser would actually load.
*
* This function handles complex real-world cases like:
* - & → &
* - & → &
* - %26 → &
* - Nested combinations such as &amp%3B → &
*
* It repeatedly applies HTML entity decoding and percent-decoding
* until no more changes occur.
*
* Examples:
*
* Input: "https://example.com?a=1&b=2"
* Output: "https://example.com?a=1&b=2"
*
* Input: "https://example.com/search?q=hello%20world&lang=en"
* Output: "https://example.com/search?q=hello world&lang=en"
*/
function decodeMixedURL(input) {
if (!input || typeof input !== 'string') return input;
let url = input.trim();
// Repeated full decoding cycle until no more changes
let previous;
do {
previous = url;
// 1. Decode HTML entities (&, &, &, etc.)
const textarea = document.createElement('textarea');
textarea.value = url;
url = textarea.value;
// 2. Decode percent-encoding
try {
url = decodeURIComponent(url);
} catch (e) {
// If decode fails, try replacing common broken patterns manually
url = url.replace(/%26/g, '&')
.replace(/%3B/g, ';')
.replace(/%3D/g, '=');
}
} while (url !== previous && url.length < 2000); // safety limit
// Final cleanup
try {
url = decodeURI(url);
} catch (e) {}
return url;
}
function stripTracking(url) {
try {
const u = new URL(url);
const kept = new URLSearchParams();
for (const [k, v] of u.searchParams) {
const kl = k.toLowerCase();
if (!kl.startsWith("utm_") && !TRACKERS.has(kl)) kept.append(k, v);
}
u.search = kept.toString();
return u.toString();
} catch { return url; }
}
function stripAllParams(url) {
try {
const u = new URL(url);
u.search = "";
u.hash = "";
return u.toString();
} catch { return url; }
}
function makeVariantBtn(url, name, desc, primary = false) {
const btn = document.createElement("a");
btn.className = "variant-btn" + (primary ? " primary" : "");
btn.href = url;
const left = document.createElement("div");
left.className = "variant-left";
const nameEl = document.createElement("div");
nameEl.className = "variant-name";
nameEl.textContent = name;
const descEl = document.createElement("div");
descEl.className = "variant-desc";
descEl.textContent = desc;
left.appendChild(nameEl);
left.appendChild(descEl);
const arrow = document.createElement("span");
arrow.className = "variant-arrow";
arrow.textContent = "→";
btn.appendChild(left);
btn.appendChild(arrow);
return btn;
}
function showResult(finalUrl) {
done = true;
progressFill.style.width = "100%";
progressStatus.textContent = "Resolved";
setTimeout(() => {
// Swap spinner for check, update headline
spinner.classList.add("done");
setTimeout(() => {
spinner.style.display = "none";
checkIcon.classList.add("visible");
}, 300);
headline.textContent = "Where do you want to go?";
subtitle.textContent = "Choose how to open this link";
progressWrap.style.display = "none";
const finalUrlSanitized = decodeMixedURL(finalUrl);
finalUrlText.textContent = finalUrlSanitized;
const noTracking = stripTracking(finalUrlSanitized);
const noParams = stripAllParams(finalUrlSanitized);
// Always show full URL as primary CTA
variantsEl.appendChild(makeVariantBtn(
finalUrlSanitized,
"Open full URL",
finalUrlSanitized,
true
));
// Only show if actually different
if (noTracking !== finalUrlSanitized) {
variantsEl.appendChild(makeVariantBtn(
noTracking,
"Remove known tracking parameters",
noTracking,
false
));
}
if (noParams !== finalUrlSanitized) {
variantsEl.appendChild(makeVariantBtn(
noParams,
"Remove all parameters",
noParams,
false
));
}
resultEl.style.display = "flex";
}, 200);
}
// Poll background every 200ms
async function poll() {
try {
const res = await browser.runtime.sendMessage({ type: "CHECK_RESOLUTION", token });
if (res?.done) { showResult(res.url); return; }
} catch { /* background not ready yet */ }
setTimeout(poll, 200);
}
setTimeout(poll, 100);
})();