forked from Moustachauve/cookie-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookie-editor.js
282 lines (264 loc) · 8.61 KB
/
cookie-editor.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
import { BrowserDetector } from './interface/lib/browserDetector.js';
import { PermissionHandler } from './interface/lib/permissionHandler.js';
(function () {
console.log('starting background script');
// TODO: Separate connections from CookieHandler and OptionsHandler.
// It would also be cool to separate their whole behavior in separate class
// that extends a generic one.
const connections = {};
const browserDetector = new BrowserDetector();
const permissionHandler = new PermissionHandler(browserDetector);
browserDetector.getApi().runtime.onConnect.addListener(onConnect);
browserDetector.getApi().runtime.onMessage.addListener(handleMessage);
browserDetector.getApi().tabs.onUpdated.addListener(onTabsChanged);
if (!browserDetector.isSafari()) {
browserDetector.getApi().cookies.onChanged.addListener(onCookiesChanged);
}
isFirefoxAndroid(function (response) {
if (response) {
const popupOptions = {
popup: '/interface/popup-mobile/cookie-list.html',
};
browserDetector.getApi().action.setPopup(popupOptions);
}
});
isSafariIos(function (response) {
if (response) {
console.log('Setting up iOS popup');
const popupOptions = {
popup: '/interface/popup-mobile/cookie-list.html',
};
browserDetector.getApi().action.setPopup(popupOptions);
}
});
if (browserDetector.supportsSidePanel()) {
browserDetector
.getApi()
.sidePanel.setPanelBehavior({ openPanelOnActionClick: false })
// eslint-disable-next-line prettier/prettier
.catch((error) => {
console.error(error);
});
}
/**
* Handles messages coming from the front end, mostly from the dev tools.
* Devtools require special handling because not all APIs are available in
* there, such as tab and permissions.
* @param {object} request contains the message.
* @param {MessageSender} sender references the sender of the message, not
* used.
* @param {function} sendResponse callback to respond to the sender.
* @return {boolean} sometimes
*/
function handleMessage(request, sender, sendResponse) {
console.log('message received: ' + (request.type || 'unknown'));
switch (request.type) {
case 'getTabs': {
browserDetector.getApi().tabs.query({}, function (tabs) {
sendResponse(tabs);
});
return true;
}
case 'getCurrentTab': {
browserDetector
.getApi()
.tabs.query(
{ active: true, currentWindow: true },
function (tabInfo) {
sendResponse(tabInfo);
},
);
return true;
}
case 'getAllCookies': {
const getAllCookiesParams = {
url: request.params.url,
};
if (browserDetector.supportsPromises()) {
browserDetector
.getApi()
.cookies.getAll(getAllCookiesParams)
.then(sendResponse);
} else {
browserDetector
.getApi()
.cookies.getAll(getAllCookiesParams, sendResponse);
}
return true;
}
case 'saveCookie': {
if (browserDetector.supportsPromises()) {
browserDetector
.getApi()
.cookies.set(request.params.cookie)
.then(
(cookie) => {
sendResponse(null, cookie);
},
(error) => {
console.error('Failed to create cookie', error);
sendResponse(error.message, null);
},
);
} else {
browserDetector
.getApi()
.cookies.set(request.params.cookie, (cookie) => {
if (cookie) {
sendResponse(null, cookie);
} else {
const error = browserDetector.getApi().runtime.lastError;
console.error('Failed to create cookie', error);
sendResponse(error.message, cookie);
}
});
}
return true;
}
case 'removeCookie': {
const removeParams = {
name: request.params.name,
url: request.params.url,
};
if (browserDetector.supportsPromises()) {
browserDetector
.getApi()
.cookies.remove(removeParams)
.then(sendResponse);
} else {
browserDetector.getApi().cookies.remove(removeParams, sendResponse);
}
return true;
}
case 'permissionsContains': {
permissionHandler.checkPermissions(request.params).then(sendResponse);
return true;
}
case 'permissionsRequest': {
permissionHandler.requestPermission(request.params).then(sendResponse);
return true;
}
case 'optionsChanged': {
sendMessageToAllTabs('optionsChanged', {
from: request.params.from,
});
return true;
}
}
}
/**
* Handles connections from clients to this script.
* @param {Port} port An object which allows two way communication with other
* pages.
*/
function onConnect(port) {
const extensionListener = function (request, port) {
console.log('port message received: ' + (request.type || 'unknown'));
switch (request.type) {
case 'init_cookieHandler':
console.log(
'Devtool cookieHandler connected on tab ' + request.tabId,
);
connections[request.tabId] = port;
return;
case 'init_optionsHandler':
console.log('optionsHandler connected: ' + port.name);
connections[port.name] = port;
return;
}
// other message handling.
};
// Listen to messages sent from the DevTools page.
port.onMessage.addListener(extensionListener);
port.onDisconnect.addListener(function (port) {
port.onMessage.removeListener(extensionListener);
const tabs = Object.keys(connections);
for (let i = 0; i < tabs.length; i++) {
if (connections[tabs[i]] === port) {
console.log('script disconnected on tab ' + tabs[i]);
delete connections[tabs[i]];
break;
}
}
});
}
/**
* Sends a message to a script running in a specific tab.
* @param {number} tabId Id of the tab to send the message to.
* @param {string} type Type of message, used by the client to parse the data.
* @param {any} data Data to send to the client.
*/
function sendMessageToTab(tabId, type, data) {
if (tabId in connections) {
connections[tabId].postMessage({
type: type,
data: data,
});
}
}
/**
* Sends a message to all the tabs connected.
* @param {string} type Type of message, used by the client to parse the data.
* @param {any} data Data to send to the client.
*/
function sendMessageToAllTabs(type, data) {
const tabs = Object.keys(connections);
for (let i = 0; i < tabs.length; i++) {
sendMessageToTab(tabs[i], type, data);
}
}
/**
* Handles events that is triggered when a cookie changes.
* @param {object} changeInfo An object containing details of the change that
* occurred.
*/
function onCookiesChanged(changeInfo) {
console.log('cookies changed, notifying all devtools');
sendMessageToAllTabs('cookiesChanged', changeInfo);
}
/**
* Handles the event that is fired when a tab is updated.
* @param {number} tabId The id of the tab that changed.
* @param {object} changeInfo Properties of the tab that changed.
* @param {object} _tab The new state of the tab.
*/
function onTabsChanged(tabId, changeInfo, _tab) {
console.log('tabs changed', tabId, changeInfo, _tab);
sendMessageToTab(tabId, 'tabsChanged', changeInfo);
}
/**
* Special function to detect if we are running on Firefox for Android.
* @param {function} callback Responds true if it is Firefox on android,
* otherwise false.
*/
function isFirefoxAndroid(callback) {
if (!browserDetector.isFirefox()) {
callback(false);
return;
}
browserDetector
.getApi()
.runtime.getPlatformInfo()
.then((info) => {
callback(info.os === 'android');
});
}
/**
* Special function to detect if we are running on Safari on iOS.
* @param {function} callback Responds true if it is Safari on iOS,
* otherwise false.
*/
function isSafariIos(callback) {
if (!browserDetector.isSafari()) {
callback(false);
return;
}
browserDetector
.getApi()
.runtime.getPlatformInfo()
.then((info) => {
console.log('check for safari on ios: ', info.os);
callback(info.os === 'ios');
});
}
})();