-
Notifications
You must be signed in to change notification settings - Fork 30
/
remove-cookie.js
107 lines (99 loc) · 3.14 KB
/
remove-cookie.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
import { hit, toRegExp } from '../helpers';
/* eslint-disable max-len */
/**
* @scriptlet remove-cookie
*
* @description
* Removes current page cookies by passed string matching with name. For current domain and subdomains.
* Runs on load and before unload.
*
* Related UBO scriptlet:
* https://github.com/gorhill/uBlock/wiki/Resources-Library#cookie-removerjs-
*
* Related ABP source:
* https://gitlab.com/eyeo/snippets/-/blob/main/source/behavioral/cookie-remover.js
*
* ### Syntax
*
* ```text
* example.org#%#//scriptlet('remove-cookie'[, match])
* ```
*
* - `match` — optional, string or regex matching the cookie name.
* If not specified all accessible cookies will be removed.
*
* ### Examples
*
* 1. Removes all cookies
*
* ```adblock
* example.org#%#//scriptlet('remove-cookie')
* ```
*
* 1. Removes cookies which name contains `example` string
*
* ```adblock
* example.org#%#//scriptlet('remove-cookie', 'example')
* ```
*
* For instance this cookie will be removed:
*
* ```javascript
* document.cookie = '__example=randomValue';
* ```
*
* @added v1.0.4.
*/
/* eslint-enable max-len */
export function removeCookie(source, match) {
const matchRegexp = toRegExp(match);
const removeCookieFromHost = (cookieName, hostName) => {
const cookieSpec = `${cookieName}=`;
const domain1 = `; domain=${hostName}`;
const domain2 = `; domain=.${hostName}`;
const path = '; path=/';
const expiration = '; expires=Thu, 01 Jan 1970 00:00:00 GMT';
document.cookie = cookieSpec + expiration;
document.cookie = cookieSpec + domain1 + expiration;
document.cookie = cookieSpec + domain2 + expiration;
document.cookie = cookieSpec + path + expiration;
document.cookie = cookieSpec + domain1 + path + expiration;
document.cookie = cookieSpec + domain2 + path + expiration;
hit(source);
};
const rmCookie = () => {
document.cookie.split(';').forEach((cookieStr) => {
const pos = cookieStr.indexOf('=');
if (pos === -1) {
return;
}
const cookieName = cookieStr.slice(0, pos).trim();
if (!matchRegexp.test(cookieName)) {
return;
}
const hostParts = document.location.hostname.split('.');
for (let i = 0; i <= hostParts.length - 1; i += 1) {
const hostName = hostParts.slice(i).join('.');
if (hostName) {
removeCookieFromHost(cookieName, hostName);
}
}
});
};
rmCookie();
window.addEventListener('beforeunload', rmCookie);
}
export const removeCookieNames = [
'remove-cookie',
// aliases are needed for matching the related scriptlet converted into our syntax
'cookie-remover.js',
'ubo-cookie-remover.js',
'ubo-cookie-remover',
'remove-cookie.js',
'ubo-remove-cookie.js',
'ubo-remove-cookie',
'abp-cookie-remover',
];
// eslint-disable-next-line prefer-destructuring
removeCookie.primaryName = removeCookieNames[0];
removeCookie.injections = [toRegExp, hit];