This repository has been archived by the owner on Jan 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SI.js
87 lines (75 loc) · 2.08 KB
/
SI.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
const _disableBrowsingHistory = () => {
console.debug(chrome.i18n.getMessage("disabling_browsing_history"));
const amazonEndpoint = "https://www.amazon.com/gp/mobile/ybh/handlers/click-stream.html";
const postParams = new URLSearchParams();
postParams.append("action", "disable");
postParams.append("deviceType", "desktop");
const params = {
method: "POST",
body: postParams,
credentials: "include",
};
return fetch(amazonEndpoint, params).then(
() => console.info(chrome.i18n.getMessage("disable_browsing_success")),
() => console.error(chrome.i18n.getMessage("disable_browsing_error")),
);
};
const _getSessionId = () => {
return new Promise((resolve, reject) => {
const params = {
url: "http://www.amazon.com",
name: "session-id"
};
chrome.cookies.get(params, cookie => {
if (cookie) {
resolve(cookie.value);
} else {
reject(new Error(chrome.i18n.getMessage("cookie_retrieval_error")));
}
});
});
};
class ShopIncognito {
constructor() {
this._sessionId = null;
}
start() {
console.debug(chrome.i18n.getMessage("checking_for_initial_session"));
_getSessionId().then(
sessionId => {
console.debug(chrome.i18n.getMessage("existing_session_found"));
return this._recordSessionId(sessionId);
},
() => {
console.info(chrome.i18n.getMessage("no_session_found"));
}
).then(
() => {
chrome.cookies.onChanged.addListener(this._onCookieChange.bind(this));
console.info(chrome.i18n.getMessage("cookie_monitoring_enabled"));
}
);
}
_onCookieChange(changeInfo) {
if (!changeInfo.removed) {
const cookie = changeInfo.cookie;
const isAmazon = (
cookie.domain === ".amazon.com" ||
cookie.domain === "smile.amazon.com");
if (isAmazon && cookie.name === "session-id") {
this._recordSessionId(cookie.value);
}
}
}
_recordSessionId(sessionId) {
if (sessionId !== this._sessionId) {
if (this._sessionId) {
console.debug(chrome.i18n.getMessage("new_session_found"));
}
this._sessionId = sessionId;
return _disableBrowsingHistory();
} else {
return Promise.resolve();
}
}
};