-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredirect-utility.js
68 lines (59 loc) · 1.86 KB
/
redirect-utility.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
(() => {
window.wfTools = window.wfTools || {};
const parseUrlParams = (override) => {
const domParser = new DOMParser();
const decode = (value) => {
try {
const decodedValue = decodeURIComponent(value);
return decodedValue.length === value.length
? decodedValue
: decode(decodedValue);
} catch {
return value;
}
};
const sanitize = (name, value) => {
const decodedValue = decode(value);
const dom = domParser.parseFromString(
`<!doctype html><body>${decodedValue}</body>`,
"text/html"
);
if (dom.body.childElementCount > 0) {
window.intellimize.log(
`[url-params] unsafe url param |${name}| with value |${value}|${decodedValue}| generated ${dom.body.childElementCount} elements`,
4
);
return "";
}
return [...dom.body.childNodes]
.filter((node) => node.constructor.name === "Text")
.map((node) => node.textContent || "")
.join("");
};
const params = {};
const valueToParse = override || window.location.search;
if (!valueToParse) return params;
valueToParse
.slice(1)
.split("&")
.forEach((param) => {
const [key, value = ""] = param.split("=");
const safeValue = sanitize(key, value);
if (!params[key]) params[key] = [];
params[key].push(safeValue);
});
return params;
};
const redirectTo = (targetURL) => {
const params = parseUrlParams();
const keys = Object.keys(params);
let newURL = targetURL;
document.querySelector("html").classList.add("i-hide");
keys.forEach((key, i) => {
const separator = i > 0 || targetURL.includes("?") ? "&" : "?";
newURL += separator + `${key}=${params[key][0]}`;
});
return newURL;
};
window.wfTools.redirectTo = redirectTo;
})();