forked from lhall-amphibee/xdebug-ext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookies.js
134 lines (110 loc) · 3.87 KB
/
cookies.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
/* this is a CONTENT script that interact directly with web content
* http://www.quirksmode.org/js/cookies.html
*/
if (typeof options === "undefined") {
function onError(error) {
console.log(`Can't get var: ${error}`);
}
function onGot(item) {
options.checkCookies.XDEBUG_TRIGGER = item.idekey || "PHPSTORM";
options.samesite = item.cookieSameSite || "Strict";
options.secure = item.cookieSecure || false;
}
options = {
cookieName: "XDEBUG_TRIGGER",
checkCookies: {
XDEBUG_TRIGGER: '',
},
samesite: "Strict",
secure: false,
};
let getting = browser.storage.local.get(["idekey", "cookieSameSite", "cookieSecure"]);
getting.then(onGot, onError);
}
function createCookie(name, value, days, samesite = "Lax", secure = false) {
let expires = "";
if (days) {
let date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toUTCString();
}
if (typeof document.cookie != 'undefined') {
browser.runtime.sendMessage("Cookie " + name + " created with value " + value);
let cookieStr = name + "=" + value + expires + "; path=/; SameSite=" + samesite;
if (secure || samesite === "None") {
cookieStr += "; Secure";
}
document.cookie = cookieStr;
}
}
function readCookie(name) {
let nameEQ = name + "=";
if (typeof document.cookie === 'undefined') {
return null;
}
let ca = document.cookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) === ' ')
c = c.substring(1, c.length);
if (c.indexOf(nameEQ) === 0)
return c.substring(nameEQ.length, c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name, "", -1);
}
function isSet(name) {
return readCookie(name) !== null;
}
browser.runtime.sendMessage(options);
browser.runtime.sendMessage({currentState: currentState, userTriggered: userTriggered});
if (userTriggered === false) {
// tab changed or page loaded
let result = {};
for (const cookieName in options.checkCookies) {
browser.runtime.sendMessage("Checking cookie " + cookieName);
result[cookieName] = isSet(cookieName);
// if user changes values, we must immediately update cookies after any tab changing or page loading
if (isSet(cookieName)) {
let currentValue = readCookie(cookieName);
let newValue = options.checkCookies[cookieName];
let samesite = options.samesite;
let secure = options.secure;
browser.runtime.sendMessage("Cookie found with value " + currentValue);
if (newValue !== currentValue) {
browser.runtime.sendMessage("Cookie values mismatch, resetting (" + currentValue + " -> " + newValue + ")");
eraseCookie(cookieName);
createCookie(cookieName, newValue, 1, samesite, secure);
}
}
browser.runtime.sendMessage({"state": result[cookieName]});
}
} else {
// widget button pressed
let cookieName = options.cookieName;
let cookieValue = options.checkCookies[cookieName];
let samesite = options.samesite;
let secure = options.secure;
browser.runtime.sendMessage({"debug": 'Button pressed'});
if (!isSet(cookieName)) {
browser.runtime.sendMessage({"debug": 'Needs to be set'});
// sometimes URL is null e.g. when we're on about:addons under linux (is it true?)
if (typeof document.URL === 'string' && document.URL.substring(0, 4) === 'http') {
createCookie(cookieName, cookieValue, 1, samesite, secure);
// Cookies can be disabled
let state = isSet(cookieName);
if (!state) {
alert('Please enable cookies for this page');
}
browser.runtime.sendMessage({"state": state});
} else {
browser.runtime.sendMessage({"state": false});
}
} else {
browser.runtime.sendMessage({"debug": 'Needs to be removed'});
eraseCookie(cookieName);
browser.runtime.sendMessage({"state": false});
}
}