-
Notifications
You must be signed in to change notification settings - Fork 0
/
banggood-ref.js
63 lines (52 loc) · 1.26 KB
/
banggood-ref.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
var defaultRefCode = "H8052232981273201905";
function setRefCode(code)
{
browser.storage.sync.set({
refcode_banggood: code
});
if (!code.startsWith("p="))
{
code = "p=" + code;
}
var url = window.location.toString();
var codeExtended = code + "&";
if (!url.includes(codeExtended))
{
var prefixIndex = url.lastIndexOf('/') + 1;
var prefixUrl = url.substr(0, prefixIndex);
var postUrl = url.substr(prefixIndex);
if (postUrl.includes("?")) {
if (postUrl.includes("?p=") || postUrl.includes("&p=")) {
//replace existing ref code
var regex = new RegExp("(?<=([?|&]))p=\\w+&");
postUrl = postUrl.replace(regex, code + "&");
} else {
//add our very own ref code to the front
postUrl = postUrl.replace("?", "?" + code + "&");
}
} else {
postUrl = postUrl.concat("?" + code);
}
if (postUrl != null)
{
history.replaceState(null, null, postUrl);
//window.location = prefixUrl + postUrl;
}
}
}
var optRefCode = browser.storage.sync.get("refcode_banggood");
function onError(error) {
console.log(`Error: ${error}`);
setRefCode(defaultRefCode);
}
function onGot(item) {
if (item.refcode_banggood)
{
setRefCode(item.refcode_banggood);
}
else
{
setRefCode(defaultRefCode);
}
}
optRefCode.then(onGot, onError);