This repository has been archived by the owner on Oct 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparams.js
303 lines (246 loc) · 8.66 KB
/
params.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
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
(function () {
"use strict";
var storageKey = "assembly_snippet_params";
var whitelistedURLs = ["assemblyai.com", "calendly.com"];
var blacklistedSchema = "mailto";
function previouslyStoredParams() {
if (window.localStorage) {
var storedParams = localStorage.getItem(storageKey);
if (storedParams && storedParams.length > 0) {
return storedParams;
}
}
return undefined;
}
function storeParamsLocally(params) {
if (window.localStorage) {
localStorage.setItem(storageKey, params);
}
}
// Get the raw string from a search string
function searchParamsString(search, prefixes) {
// Sanity check
if (!search || search.length <= 0 || typeof search !== "string") {
return;
}
var parsedSearch = search;
var hasParsedSearch = false;
// If the browser supports URLSearchParams, use it and
// its toString() convenience method (which stripes the
// question mark from it)
//
// If the browser doesn't support it, check to see if
// the string starts with a question mark -- if it does,
// remove it. Otherwise, return the search params.
if (typeof window.URLSearchParams !== "undefined") {
parsedSearch = "";
var searchParams = new window.URLSearchParams(search);
if (prefixes) {
for (var [key, value] of searchParams) {
const startsWith = prefixes.filter((prefix) =>
key.startsWith(prefix)
);
if (startsWith.length > 0) {
parsedSearch += key + "=" + value + "&";
hasParsedSearch = true;
}
}
// Removing the that last &
if (hasParsedSearch) {
parsedSearch = parsedSearch.slice(0, -1);
}
} else {
parsedSearch = searchParams.toString();
}
} else if (search.startsWith("?")) {
parsedSearch = search.substring(1);
}
return parsedSearch;
}
// Merge two searches without duplicating params
function mergeSearchParams(lhs, rhs) {
if (!lhs && !rhs) return "";
if (!lhs) return rhs;
if (!rhs) return lhs;
var merged = "";
if (typeof window.URLSearchParams !== "undefined") {
// If the browser supports URLSearchParams, use it.
var lhsSearchParams = new window.URLSearchParams(lhs);
var rhsSearchParams = new window.URLSearchParams(rhs);
// We start with the left-hand side as our base
var urlSearchParamsResult = new window.URLSearchParams(
lhsSearchParams.toString()
);
// Loop through the right-side
for (var param of rhsSearchParams) {
// Sanity checking the right-side param
if (!param || !Array.isArray(param) || param.length <= 0) {
continue;
}
// Get the name and the value
var paramName = param[0];
var paramValue = param.length > 1 ? param[1] : "";
// If that param doesn't exist on lhs, add it to the final
// search params
if (paramName && !lhsSearchParams.get(paramName)) {
urlSearchParamsResult.set(paramName, paramValue);
}
}
// Convert the final search params into a string and return it
merged = urlSearchParamsResult.toString();
} else {
// If the browser doesn't support URLSearchParams...
// Get every param from the left- and right-side by splitting
// the strings on the "&" character
var lhsSplit = lhs.split("&");
var rhsSplit = rhs.split("&");
// Our finalSearchParam starts with a copy of the left-side (our
// base)
var finalSearchParams = lhs.slice();
// Loop through every param of the right-side
for (var i = 0; i < rhsSplit.length; i++) {
var shouldMerge = true;
// Get the right-side name by splitting it on the "=" character
var newParam = rhsSplit[i];
var newParamName = newParam.split("=")[0];
// Loop through the left-side to make sure it doesn't exist there
for (var j = 0; j < lhsSplit.length; j++) {
var existingParam = lhsSplit[j];
var existingParamName = existingParam.split("=")[0];
// If it exists, break the loop
if (newParamName === existingParamName) {
shouldMerge = false;
break;
}
}
if (!shouldMerge) {
continue;
}
if (newParam) {
// Add the new param to the existing finalSearchParams
finalSearchParams = finalSearchParams + "&" + newParam;
}
}
merged = finalSearchParams;
}
return merged;
}
// DOMContentLoaded, unlike onload, will fire only after the
// DOM's been rendered and analyzed.
document.addEventListener("DOMContentLoaded", function () {
// Try getting the params from localStorage
var prevParams = previouslyStoredParams();
var additionalParams;
if (prevParams && prevParams) {
additionalParams = prevParams;
} else {
// We start by getting the current search (?param1=foo¶m2=bar)
var search =
typeof location !== "undefined"
? location.search
: window.location.search;
// We then turn it into a string
additionalParams = searchParamsString(search, ["utm_", "ref"]);
if (additionalParams && additionalParams.length > 0) {
// And then save them to local storage
storeParamsLocally(additionalParams);
}
}
if (
!additionalParams ||
typeof additionalParams !== "string" ||
additionalParams.length <= 0
) {
return;
}
// Listing all links available on the page
var linksCollection = document.getElementsByTagName("a");
var linksArray;
// It can be an HTMLCollection, so we must make it an array
if (typeof Array.from !== "undefined") {
// If the browser supports Array.from, we call it just to
// be sure -- won't hurt.
linksArray = Array.from(linksCollection);
} else {
// If the browser doesn't support it, we check if it's an
// array. If it is, do nothing. Otherwise, convert it
// using slice.call (creating an empty array and then
// iterating through the collection).
if (Array.isArray(linksCollection)) {
linksArray = linksCollection;
} else {
linksArray = [].slice.call(linksCollection);
}
}
linksArray = linksArray.filter((link) => {
var href = link.getAttribute("href");
if (!href) return false;
if (href.indexOf(blacklistedSchema) > -1) return false;
const foundIndex = whitelistedURLs.findIndex(
(url) => href.indexOf(url) > -1
);
return foundIndex > -1;
});
// If there are no links available, ignore it
if (!linksArray || linksArray.length <= 0) {
return;
}
// Now that we know that the links are in an array, create
// a loop
for (var i = 0; i < linksArray.length; i++) {
var link = linksArray[i];
var href = link.getAttribute("href");
if (href && href.indexOf("#") > -1) {
continue;
}
// There are two pieces for every link: the base and the
// existing URL params
var linkBase = "";
var existingParams = "";
if (typeof window.URL !== "undefined") {
// If the browser supports URL object, create a new
// instance and get the base and params from it.
try {
var url = new URL(link);
var linkSearch = url.search;
linkBase = url.origin + url.pathname;
existingParams = searchParamsString(linkSearch);
} catch (error) {
continue;
}
} else {
// If it doesn't, get those values by splitting the href
// on the "?" character. The first element will always be
// the base. The rest, we consider to be the existing
// params.
var href = link.getAttribute("href");
var split = href.split("?");
if (split && split.length >= 1) {
linkBase = split[0];
if (split.length > 1) {
existingParams = split.slice(1, split.length);
}
} else {
continue;
}
}
// The final params of the link will be the existing params and
// the additional ones merged together.
var finalParams = additionalParams;
if (existingParams) {
finalParams = mergeSearchParams(additionalParams, existingParams);
}
// One final sanity check
if (
!finalParams ||
finalParams.length <= 0 ||
!linkBase ||
linkBase.length <= 0
) {
continue;
}
// Update the href attribute to the base + params
link.setAttribute("href", linkBase.concat("?", finalParams));
}
});
})();