@browserless.io/browserless 2.25.0-beta-2 → 2.25.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (224) hide show
  1. package/CHANGELOG.md +39 -3
  2. package/README.md +4 -5
  3. package/build/browserless.js +3 -1
  4. package/build/browsers/browsers.cdp.d.ts +3 -0
  5. package/build/browsers/browsers.cdp.js +4 -1
  6. package/build/browsers/browsers.playwright.d.ts +4 -0
  7. package/build/browsers/browsers.playwright.js +11 -2
  8. package/build/browsers/index.js +4 -3
  9. package/build/http.d.ts +9 -0
  10. package/build/http.js +9 -0
  11. package/build/routes/chrome/http/content.post.body.json +8 -8
  12. package/build/routes/chrome/http/pdf.post.body.json +8 -8
  13. package/build/routes/chrome/http/scrape.post.body.json +8 -8
  14. package/build/routes/chrome/http/screenshot.post.body.json +8 -8
  15. package/build/routes/chrome/tests/kill-sessions.spec.js +1 -1
  16. package/build/routes/chromium/http/content.post.body.json +8 -8
  17. package/build/routes/chromium/http/pdf.post.body.json +8 -8
  18. package/build/routes/chromium/http/scrape.post.body.json +8 -8
  19. package/build/routes/chromium/http/screenshot.post.body.json +8 -8
  20. package/build/routes/chromium/tests/kill-sessions.spec.js +1 -1
  21. package/build/routes/edge/http/content.post.body.json +579 -0
  22. package/build/routes/edge/http/content.post.d.ts +8 -0
  23. package/build/routes/edge/http/content.post.js +7 -0
  24. package/build/routes/edge/http/content.post.query.json +183 -0
  25. package/build/routes/edge/http/content.post.response.json +5 -0
  26. package/build/routes/edge/http/download.post.body.json +32 -0
  27. package/build/routes/edge/http/download.post.d.ts +8 -0
  28. package/build/routes/edge/http/download.post.js +7 -0
  29. package/build/routes/edge/http/download.post.query.json +120 -0
  30. package/build/routes/edge/http/download.post.response.json +4 -0
  31. package/build/routes/edge/http/function.post.body.json +32 -0
  32. package/build/routes/edge/http/function.post.d.ts +8 -0
  33. package/build/routes/edge/http/function.post.js +7 -0
  34. package/build/routes/edge/http/function.post.query.json +120 -0
  35. package/build/routes/edge/http/function.post.response.json +4 -0
  36. package/build/routes/edge/http/json-list.get.d.ts +5 -0
  37. package/build/routes/edge/http/json-list.get.js +5 -0
  38. package/build/routes/edge/http/json-list.get.response.json +52 -0
  39. package/build/routes/edge/http/json-new.put.d.ts +5 -0
  40. package/build/routes/edge/http/json-new.put.js +5 -0
  41. package/build/routes/edge/http/json-new.put.response.json +44 -0
  42. package/build/routes/edge/http/json-protocol.get.d.ts +5 -0
  43. package/build/routes/edge/http/json-protocol.get.js +5 -0
  44. package/build/routes/edge/http/json-protocol.get.response.json +6 -0
  45. package/build/routes/edge/http/json-version.get.d.ts +5 -0
  46. package/build/routes/edge/http/json-version.get.js +5 -0
  47. package/build/routes/edge/http/json-version.get.response.json +44 -0
  48. package/build/routes/edge/http/pdf.post.body.json +724 -0
  49. package/build/routes/edge/http/pdf.post.d.ts +8 -0
  50. package/build/routes/edge/http/pdf.post.js +7 -0
  51. package/build/routes/edge/http/pdf.post.query.json +120 -0
  52. package/build/routes/edge/http/pdf.post.response.json +5 -0
  53. package/build/routes/edge/http/performance.post.body.json +26 -0
  54. package/build/routes/edge/http/performance.post.d.ts +8 -0
  55. package/build/routes/edge/http/performance.post.js +7 -0
  56. package/build/routes/edge/http/performance.post.query.json +120 -0
  57. package/build/routes/edge/http/performance.post.response.json +7 -0
  58. package/build/routes/edge/http/scrape.post.body.json +626 -0
  59. package/build/routes/edge/http/scrape.post.d.ts +8 -0
  60. package/build/routes/edge/http/scrape.post.js +7 -0
  61. package/build/routes/edge/http/scrape.post.query.json +183 -0
  62. package/build/routes/edge/http/scrape.post.response.json +334 -0
  63. package/build/routes/edge/http/screenshot.post.body.json +669 -0
  64. package/build/routes/edge/http/screenshot.post.d.ts +8 -0
  65. package/build/routes/edge/http/screenshot.post.js +7 -0
  66. package/build/routes/edge/http/screenshot.post.query.json +120 -0
  67. package/build/routes/edge/http/screenshot.post.response.json +5 -0
  68. package/build/routes/edge/tests/content.spec.d.ts +1 -0
  69. package/build/routes/edge/tests/content.spec.js +312 -0
  70. package/build/routes/edge/tests/download.spec.d.ts +1 -0
  71. package/build/routes/edge/tests/download.spec.js +67 -0
  72. package/build/routes/edge/tests/function.spec.d.ts +1 -0
  73. package/build/routes/edge/tests/function.spec.js +277 -0
  74. package/build/routes/edge/tests/json-version.spec.d.ts +1 -0
  75. package/build/routes/edge/tests/json-version.spec.js +37 -0
  76. package/build/routes/edge/tests/kill-sessions.spec.d.ts +1 -0
  77. package/build/routes/edge/tests/kill-sessions.spec.js +80 -0
  78. package/build/routes/edge/tests/page-websocket.spec.d.ts +1 -0
  79. package/build/routes/edge/tests/page-websocket.spec.js +97 -0
  80. package/build/routes/edge/tests/pdf.spec.d.ts +1 -0
  81. package/build/routes/edge/tests/pdf.spec.js +345 -0
  82. package/build/routes/edge/tests/performance.spec.d.ts +1 -0
  83. package/build/routes/edge/tests/performance.spec.js +124 -0
  84. package/build/routes/edge/tests/scrape.spec.d.ts +1 -0
  85. package/build/routes/edge/tests/scrape.spec.js +354 -0
  86. package/build/routes/edge/tests/screenshot.spec.d.ts +1 -0
  87. package/build/routes/edge/tests/screenshot.spec.js +339 -0
  88. package/build/routes/edge/tests/websocket.spec.d.ts +1 -0
  89. package/build/routes/edge/tests/websocket.spec.js +384 -0
  90. package/build/routes/edge/ws/browser.d.ts +7 -0
  91. package/build/routes/edge/ws/browser.js +6 -0
  92. package/build/routes/edge/ws/browser.query.json +120 -0
  93. package/build/routes/edge/ws/cdp.d.ts +8 -0
  94. package/build/routes/edge/ws/cdp.js +7 -0
  95. package/build/routes/edge/ws/cdp.query.json +120 -0
  96. package/build/routes/edge/ws/page.d.ts +8 -0
  97. package/build/routes/edge/ws/page.js +7 -0
  98. package/build/routes/edge/ws/page.query.json +120 -0
  99. package/build/routes/edge/ws/playwright.d.ts +8 -0
  100. package/build/routes/edge/ws/playwright.js +7 -0
  101. package/build/routes/edge/ws/playwright.query.json +100 -0
  102. package/build/routes/firefox/tests/kill-sessions.spec.js +1 -1
  103. package/build/routes/management/http/meta.get.js +3 -1
  104. package/build/routes/webkit/tests/kill-sessions.spec.js +1 -1
  105. package/build/sdk-utils.js +1 -1
  106. package/build/shared/scrape.http.js +2 -2
  107. package/build/types.d.ts +32 -0
  108. package/build/types.js +18 -0
  109. package/build/utils.d.ts +1 -0
  110. package/build/utils.js +16 -2
  111. package/docker/chrome/Dockerfile +14 -14
  112. package/docker/chromium/Dockerfile +14 -14
  113. package/docker/edge/.dockerignore +16 -0
  114. package/docker/edge/Dockerfile +43 -0
  115. package/docker/firefox/Dockerfile +14 -14
  116. package/docker/multi/Dockerfile +18 -18
  117. package/docker/sdk/Dockerfile +10 -0
  118. package/extensions/ublock/_locales/ar/messages.json +3 -3
  119. package/extensions/ublock/_locales/bg/messages.json +1 -1
  120. package/extensions/ublock/_locales/br_FR/messages.json +2 -2
  121. package/extensions/ublock/_locales/cy/messages.json +11 -11
  122. package/extensions/ublock/_locales/el/messages.json +2 -2
  123. package/extensions/ublock/_locales/hu/messages.json +1 -1
  124. package/extensions/ublock/_locales/id/messages.json +1 -1
  125. package/extensions/ublock/_locales/lv/messages.json +4 -4
  126. package/extensions/ublock/_locales/mk/messages.json +130 -130
  127. package/extensions/ublock/_locales/oc/messages.json +1 -1
  128. package/extensions/ublock/_locales/pt_BR/messages.json +1 -1
  129. package/extensions/ublock/_locales/pt_PT/messages.json +2 -2
  130. package/extensions/ublock/_locales/si/messages.json +100 -100
  131. package/extensions/ublock/_locales/sr/messages.json +4 -4
  132. package/extensions/ublock/_locales/vi/messages.json +19 -19
  133. package/extensions/ublock/_locales/zh_TW/messages.json +28 -28
  134. package/extensions/ublock/assets/assets.json +33 -29
  135. package/extensions/ublock/assets/thirdparties/easylist/easylist.txt +2984 -3287
  136. package/extensions/ublock/assets/thirdparties/easylist/easyprivacy.txt +150 -171
  137. package/extensions/ublock/assets/thirdparties/pgl.yoyo.org/as/serverlist +37 -27
  138. package/extensions/ublock/assets/thirdparties/publicsuffix.org/list/effective_tld_names.dat +802 -888
  139. package/extensions/ublock/assets/thirdparties/urlhaus-filter/urlhaus-filter-online.txt +2355 -2071
  140. package/extensions/ublock/assets/ublock/badlists.txt +9 -1
  141. package/extensions/ublock/assets/ublock/badware.min.txt +354 -243
  142. package/extensions/ublock/assets/ublock/filters.min.txt +5837 -5737
  143. package/extensions/ublock/assets/ublock/privacy.min.txt +151 -38
  144. package/extensions/ublock/assets/ublock/quick-fixes.min.txt +83 -127
  145. package/extensions/ublock/assets/ublock/unbreak.min.txt +66 -50
  146. package/extensions/ublock/css/codemirror.css +4 -0
  147. package/extensions/ublock/document-blocked.html +3 -1
  148. package/extensions/ublock/js/arglist-parser.js +116 -0
  149. package/extensions/ublock/js/background.js +1 -1
  150. package/extensions/ublock/js/logger-ui.js +1 -1
  151. package/extensions/ublock/js/messaging.js +9 -2
  152. package/extensions/ublock/js/pagestore.js +3 -1
  153. package/extensions/ublock/js/redirect-engine.js +3 -1
  154. package/extensions/ublock/{assets/resources/set-attr.js → js/resources/attribute.js} +115 -11
  155. package/extensions/ublock/js/resources/base.js +38 -0
  156. package/extensions/ublock/js/resources/cookie.js +419 -0
  157. package/extensions/ublock/js/resources/href-sanitizer.js +188 -0
  158. package/extensions/ublock/js/resources/localstorage.js +235 -0
  159. package/extensions/ublock/js/resources/parse-replace.js +54 -0
  160. package/extensions/ublock/js/resources/prevent-settimeout.js +236 -0
  161. package/extensions/ublock/js/resources/proxy-apply.js +109 -0
  162. package/extensions/ublock/js/resources/replace-argument.js +120 -0
  163. package/extensions/ublock/{assets → js}/resources/run-at.js +20 -4
  164. package/extensions/ublock/{assets → js}/resources/safe-self.js +5 -4
  165. package/extensions/ublock/{assets → js}/resources/scriptlets.js +90 -1589
  166. package/extensions/ublock/js/resources/set-constant.js +287 -0
  167. package/extensions/ublock/js/resources/shared.js +44 -0
  168. package/extensions/ublock/js/resources/spoof-css.js +163 -0
  169. package/extensions/ublock/js/s14e-serializer.js +2 -1
  170. package/extensions/ublock/js/scriptlet-filtering-core.js +1 -1
  171. package/extensions/ublock/js/scriptlet-filtering.js +1 -31
  172. package/extensions/ublock/js/static-dnr-filtering.js +143 -129
  173. package/extensions/ublock/js/static-filtering-parser.js +27 -117
  174. package/extensions/ublock/js/static-net-filtering.js +53 -141
  175. package/extensions/ublock/js/traffic.js +1 -1
  176. package/extensions/ublock/js/urlskip.js +166 -0
  177. package/extensions/ublock/js/vapi-background-ext.js +38 -14
  178. package/extensions/ublock/manifest.json +1 -1
  179. package/package.json +22 -22
  180. package/src/browserless.ts +4 -0
  181. package/src/browsers/browsers.cdp.ts +5 -0
  182. package/src/browsers/browsers.playwright.ts +14 -1
  183. package/src/browsers/index.ts +5 -2
  184. package/src/http.ts +9 -0
  185. package/src/routes/chrome/tests/kill-sessions.spec.ts +1 -1
  186. package/src/routes/chromium/tests/kill-sessions.spec.ts +1 -1
  187. package/src/routes/edge/http/content.post.ts +20 -0
  188. package/src/routes/edge/http/download.post.ts +20 -0
  189. package/src/routes/edge/http/function.post.ts +20 -0
  190. package/src/routes/edge/http/json-list.get.ts +7 -0
  191. package/src/routes/edge/http/json-new.put.ts +7 -0
  192. package/src/routes/edge/http/json-protocol.get.ts +7 -0
  193. package/src/routes/edge/http/json-version.get.ts +7 -0
  194. package/src/routes/edge/http/pdf.post.ts +20 -0
  195. package/src/routes/edge/http/performance.post.ts +20 -0
  196. package/src/routes/edge/http/scrape.post.ts +20 -0
  197. package/src/routes/edge/http/screenshot.post.ts +20 -0
  198. package/src/routes/edge/tests/content.spec.ts +376 -0
  199. package/src/routes/edge/tests/download.spec.ts +77 -0
  200. package/src/routes/edge/tests/function.spec.ts +317 -0
  201. package/src/routes/edge/tests/json-version.spec.ts +52 -0
  202. package/src/routes/edge/tests/kill-sessions.spec.ts +99 -0
  203. package/src/routes/edge/tests/page-websocket.spec.ts +129 -0
  204. package/src/routes/edge/tests/pdf.spec.ts +389 -0
  205. package/src/routes/edge/tests/performance.spec.ts +155 -0
  206. package/src/routes/edge/tests/scrape.spec.ts +417 -0
  207. package/src/routes/edge/tests/screenshot.spec.ts +387 -0
  208. package/src/routes/edge/tests/websocket.spec.ts +510 -0
  209. package/src/routes/edge/ws/browser.ts +10 -0
  210. package/src/routes/edge/ws/cdp.ts +17 -0
  211. package/src/routes/edge/ws/page.ts +10 -0
  212. package/src/routes/edge/ws/playwright.ts +17 -0
  213. package/src/routes/firefox/tests/kill-sessions.spec.ts +1 -1
  214. package/src/routes/management/http/meta.get.ts +6 -1
  215. package/src/routes/management/http/static.get.ts +1 -1
  216. package/src/routes/webkit/tests/kill-sessions.spec.ts +1 -1
  217. package/src/sdk-utils.ts +1 -1
  218. package/src/shared/scrape.http.ts +2 -2
  219. package/src/types.ts +19 -0
  220. package/src/utils.ts +38 -16
  221. package/static/docs/swagger.json +2097 -10
  222. package/static/docs/swagger.min.json +2096 -9
  223. package/static/function/client.js +141 -253
  224. package/static/function/index.html +141 -253
@@ -0,0 +1,188 @@
1
+ /*******************************************************************************
2
+
3
+ uBlock Origin - a comprehensive, efficient content blocker
4
+ Copyright (C) 2019-present Raymond Hill
5
+
6
+ This program is free software: you can redistribute it and/or modify
7
+ it under the terms of the GNU General Public License as published by
8
+ the Free Software Foundation, either version 3 of the License, or
9
+ (at your option) any later version.
10
+
11
+ This program is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU General Public License for more details.
15
+
16
+ You should have received a copy of the GNU General Public License
17
+ along with this program. If not, see {http://www.gnu.org/licenses/}.
18
+
19
+ Home: https://github.com/gorhill/uBlock
20
+
21
+ */
22
+
23
+ import { registerScriptlet } from './base.js';
24
+ import { runAt } from './run-at.js';
25
+ import { safeSelf } from './safe-self.js';
26
+ import { urlSkip } from '../urlskip.js';
27
+
28
+ /******************************************************************************/
29
+
30
+ registerScriptlet(urlSkip, {
31
+ name: 'urlskip.fn',
32
+ });
33
+
34
+ /**
35
+ * @scriptlet href-sanitizer
36
+ *
37
+ * @description
38
+ * Set the `href` attribute to a value found in the DOM at, or below the
39
+ * targeted `a` element, and optionally with transformation steps.
40
+ *
41
+ * @param selector
42
+ * A plain CSS selector for elements which `href` property must be sanitized.
43
+ *
44
+ * @param source
45
+ * One or more tokens to lookup the source of the `href` property, and
46
+ * optionally the transformation steps to perform:
47
+ * - `text`: Use the text content of the element as the URL
48
+ * - `[name]`: Use the value of the attribute `name` as the URL
49
+ * - Transformation steps: see `urlskip` documentation
50
+ *
51
+ * If `text` or `[name]` is not present, the URL will be the value of `href`
52
+ * attribute.
53
+ *
54
+ * @example
55
+ * `example.org##+js(href-sanitizer, a)`
56
+ * `example.org##+js(href-sanitizer, a[title], [title])`
57
+ * `example.org##+js(href-sanitizer, a[href*="/away.php?to="], ?to)`
58
+ * `example.org##+js(href-sanitizer, a[href*="/redirect"], ?url ?url -base64)`
59
+ *
60
+ * */
61
+
62
+ function hrefSanitizer(
63
+ selector = '',
64
+ source = ''
65
+ ) {
66
+ if ( typeof selector !== 'string' ) { return; }
67
+ if ( selector === '' ) { return; }
68
+ const safe = safeSelf();
69
+ const logPrefix = safe.makeLogPrefix('href-sanitizer', selector, source);
70
+ if ( source === '' ) { source = 'text'; }
71
+ const sanitizeCopycats = (href, text) => {
72
+ let elems = [];
73
+ try {
74
+ elems = document.querySelectorAll(`a[href="${href}"`);
75
+ }
76
+ catch(ex) {
77
+ }
78
+ for ( const elem of elems ) {
79
+ elem.setAttribute('href', text);
80
+ }
81
+ return elems.length;
82
+ };
83
+ const validateURL = text => {
84
+ if ( typeof text !== 'string' ) { return ''; }
85
+ if ( text === '' ) { return ''; }
86
+ if ( /[\x00-\x20\x7f]/.test(text) ) { return ''; }
87
+ try {
88
+ const url = new URL(text, document.location);
89
+ return url.href;
90
+ } catch(ex) {
91
+ }
92
+ return '';
93
+ };
94
+ const extractParam = (href, source) => {
95
+ if ( Boolean(source) === false ) { return href; }
96
+ const recursive = source.includes('?', 1);
97
+ const end = recursive ? source.indexOf('?', 1) : source.length;
98
+ try {
99
+ const url = new URL(href, document.location);
100
+ let value = url.searchParams.get(source.slice(1, end));
101
+ if ( value === null ) { return href }
102
+ if ( recursive ) { return extractParam(value, source.slice(end)); }
103
+ return value;
104
+ } catch(x) {
105
+ }
106
+ return href;
107
+ };
108
+ const extractURL = (elem, source) => {
109
+ if ( /^\[.*\]$/.test(source) ) {
110
+ return elem.getAttribute(source.slice(1,-1).trim()) || '';
111
+ }
112
+ if ( source === 'text' ) {
113
+ return elem.textContent
114
+ .replace(/^[^\x21-\x7e]+/, '') // remove leading invalid characters
115
+ .replace(/[^\x21-\x7e]+$/, '') // remove trailing invalid characters
116
+ ;
117
+ }
118
+ if ( source.startsWith('?') === false ) { return ''; }
119
+ const steps = source.replace(/(\S)\?/g, '\\1?').split(/\s+/);
120
+ const url = steps.length === 1
121
+ ? extractParam(elem.href, source)
122
+ : urlSkip(elem.href, false, steps);
123
+ if ( url === undefined ) { return; }
124
+ return url.replace(/ /g, '%20');
125
+ };
126
+ const sanitize = ( ) => {
127
+ let elems = [];
128
+ try {
129
+ elems = document.querySelectorAll(selector);
130
+ }
131
+ catch(ex) {
132
+ return false;
133
+ }
134
+ for ( const elem of elems ) {
135
+ if ( elem.localName !== 'a' ) { continue; }
136
+ if ( elem.hasAttribute('href') === false ) { continue; }
137
+ const href = elem.getAttribute('href');
138
+ const text = extractURL(elem, source);
139
+ const hrefAfter = validateURL(text);
140
+ if ( hrefAfter === '' ) { continue; }
141
+ if ( hrefAfter === href ) { continue; }
142
+ elem.setAttribute('href', hrefAfter);
143
+ const count = sanitizeCopycats(href, hrefAfter);
144
+ safe.uboLog(logPrefix, `Sanitized ${count+1} links to\n${hrefAfter}`);
145
+ }
146
+ return true;
147
+ };
148
+ let observer, timer;
149
+ const onDomChanged = mutations => {
150
+ if ( timer !== undefined ) { return; }
151
+ let shouldSanitize = false;
152
+ for ( const mutation of mutations ) {
153
+ if ( mutation.addedNodes.length === 0 ) { continue; }
154
+ for ( const node of mutation.addedNodes ) {
155
+ if ( node.nodeType !== 1 ) { continue; }
156
+ shouldSanitize = true;
157
+ break;
158
+ }
159
+ if ( shouldSanitize ) { break; }
160
+ }
161
+ if ( shouldSanitize === false ) { return; }
162
+ timer = safe.onIdle(( ) => {
163
+ timer = undefined;
164
+ sanitize();
165
+ });
166
+ };
167
+ const start = ( ) => {
168
+ if ( sanitize() === false ) { return; }
169
+ observer = new MutationObserver(onDomChanged);
170
+ observer.observe(document.body, {
171
+ subtree: true,
172
+ childList: true,
173
+ });
174
+ };
175
+ runAt(( ) => { start(); }, 'interactive');
176
+ }
177
+ registerScriptlet(hrefSanitizer, {
178
+ name: 'href-sanitizer.js',
179
+ world: 'ISOLATED',
180
+ aliases: [
181
+ 'urlskip.js',
182
+ ],
183
+ dependencies: [
184
+ runAt,
185
+ safeSelf,
186
+ urlSkip,
187
+ ],
188
+ });
@@ -0,0 +1,235 @@
1
+ /*******************************************************************************
2
+
3
+ uBlock Origin - a comprehensive, efficient content blocker
4
+ Copyright (C) 2019-present Raymond Hill
5
+
6
+ This program is free software: you can redistribute it and/or modify
7
+ it under the terms of the GNU General Public License as published by
8
+ the Free Software Foundation, either version 3 of the License, or
9
+ (at your option) any later version.
10
+
11
+ This program is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU General Public License for more details.
15
+
16
+ You should have received a copy of the GNU General Public License
17
+ along with this program. If not, see {http://www.gnu.org/licenses/}.
18
+
19
+ Home: https://github.com/gorhill/uBlock
20
+
21
+ */
22
+
23
+ import { getSafeCookieValuesFn } from './cookie.js';
24
+ import { registerScriptlet } from './base.js';
25
+ import { safeSelf } from './safe-self.js';
26
+
27
+ /******************************************************************************/
28
+
29
+ export function getAllLocalStorageFn(which = 'localStorage') {
30
+ const storage = self[which];
31
+ const out = [];
32
+ for ( let i = 0; i < storage.length; i++ ) {
33
+ const key = storage.key(i);
34
+ const value = storage.getItem(key);
35
+ return { key, value };
36
+ }
37
+ return out;
38
+ }
39
+ registerScriptlet(getAllLocalStorageFn, {
40
+ name: 'get-all-local-storage.fn',
41
+ });
42
+
43
+ /******************************************************************************/
44
+
45
+ export function setLocalStorageItemFn(
46
+ which = 'local',
47
+ trusted = false,
48
+ key = '',
49
+ value = '',
50
+ ) {
51
+ if ( key === '' ) { return; }
52
+
53
+ // For increased compatibility with AdGuard
54
+ if ( value === 'emptyArr' ) {
55
+ value = '[]';
56
+ } else if ( value === 'emptyObj' ) {
57
+ value = '{}';
58
+ }
59
+
60
+ const trustedValues = [
61
+ '',
62
+ 'undefined', 'null',
63
+ '{}', '[]', '""',
64
+ '$remove$',
65
+ ...getSafeCookieValuesFn(),
66
+ ];
67
+
68
+ if ( trusted ) {
69
+ if ( value.includes('$now$') ) {
70
+ value = value.replaceAll('$now$', Date.now());
71
+ }
72
+ if ( value.includes('$currentDate$') ) {
73
+ value = value.replaceAll('$currentDate$', `${Date()}`);
74
+ }
75
+ if ( value.includes('$currentISODate$') ) {
76
+ value = value.replaceAll('$currentISODate$', (new Date()).toISOString());
77
+ }
78
+ } else {
79
+ const normalized = value.toLowerCase();
80
+ const match = /^("?)(.+)\1$/.exec(normalized);
81
+ const unquoted = match && match[2] || normalized;
82
+ if ( trustedValues.includes(unquoted) === false ) {
83
+ if ( /^-?\d+$/.test(unquoted) === false ) { return; }
84
+ const n = parseInt(unquoted, 10) || 0;
85
+ if ( n < -32767 || n > 32767 ) { return; }
86
+ }
87
+ }
88
+
89
+ try {
90
+ const storage = self[`${which}Storage`];
91
+ if ( value === '$remove$' ) {
92
+ const safe = safeSelf();
93
+ const pattern = safe.patternToRegex(key, undefined, true );
94
+ const toRemove = [];
95
+ for ( let i = 0, n = storage.length; i < n; i++ ) {
96
+ const key = storage.key(i);
97
+ if ( pattern.test(key) ) { toRemove.push(key); }
98
+ }
99
+ for ( const key of toRemove ) {
100
+ storage.removeItem(key);
101
+ }
102
+ } else {
103
+ storage.setItem(key, `${value}`);
104
+ }
105
+ } catch(ex) {
106
+ }
107
+ }
108
+ registerScriptlet(setLocalStorageItemFn, {
109
+ name: 'set-local-storage-item.fn',
110
+ dependencies: [
111
+ getSafeCookieValuesFn,
112
+ safeSelf,
113
+ ],
114
+ });
115
+
116
+ /******************************************************************************/
117
+
118
+ export function removeCacheStorageItem(
119
+ cacheNamePattern = '',
120
+ requestPattern = ''
121
+ ) {
122
+ if ( cacheNamePattern === '' ) { return; }
123
+ const safe = safeSelf();
124
+ const logPrefix = safe.makeLogPrefix('remove-cache-storage-item', cacheNamePattern, requestPattern);
125
+ const cacheStorage = self.caches;
126
+ if ( cacheStorage instanceof Object === false ) { return; }
127
+ const reCache = safe.patternToRegex(cacheNamePattern, undefined, true);
128
+ const reRequest = safe.patternToRegex(requestPattern, undefined, true);
129
+ cacheStorage.keys().then(cacheNames => {
130
+ for ( const cacheName of cacheNames ) {
131
+ if ( reCache.test(cacheName) === false ) { continue; }
132
+ if ( requestPattern === '' ) {
133
+ cacheStorage.delete(cacheName).then(result => {
134
+ if ( safe.logLevel > 1 ) {
135
+ safe.uboLog(logPrefix, `Deleting ${cacheName}`);
136
+ }
137
+ if ( result !== true ) { return; }
138
+ safe.uboLog(logPrefix, `Deleted ${cacheName}: ${result}`);
139
+ });
140
+ continue;
141
+ }
142
+ cacheStorage.open(cacheName).then(cache => {
143
+ cache.keys().then(requests => {
144
+ for ( const request of requests ) {
145
+ if ( reRequest.test(request.url) === false ) { continue; }
146
+ if ( safe.logLevel > 1 ) {
147
+ safe.uboLog(logPrefix, `Deleting ${cacheName}/${request.url}`);
148
+ }
149
+ cache.delete(request).then(result => {
150
+ if ( result !== true ) { return; }
151
+ safe.uboLog(logPrefix, `Deleted ${cacheName}/${request.url}: ${result}`);
152
+ });
153
+ }
154
+ });
155
+ });
156
+ }
157
+ });
158
+ }
159
+ registerScriptlet(removeCacheStorageItem, {
160
+ name: 'remove-cache-storage-item.fn',
161
+ world: 'ISOLATED',
162
+ dependencies: [
163
+ safeSelf,
164
+ ],
165
+ });
166
+
167
+ /*******************************************************************************
168
+ *
169
+ * set-local-storage-item.js
170
+ * set-session-storage-item.js
171
+ *
172
+ * Set a local/session storage entry to a specific, allowed value.
173
+ *
174
+ * Reference:
175
+ * https://github.com/AdguardTeam/Scriptlets/blob/master/src/scriptlets/set-local-storage-item.js
176
+ * https://github.com/AdguardTeam/Scriptlets/blob/master/src/scriptlets/set-session-storage-item.js
177
+ *
178
+ **/
179
+
180
+ export function setLocalStorageItem(key = '', value = '') {
181
+ setLocalStorageItemFn('local', false, key, value);
182
+ }
183
+ registerScriptlet(setLocalStorageItem, {
184
+ name: 'set-local-storage-item.js',
185
+ world: 'ISOLATED',
186
+ dependencies: [
187
+ setLocalStorageItemFn,
188
+ ],
189
+ });
190
+
191
+ export function setSessionStorageItem(key = '', value = '') {
192
+ setLocalStorageItemFn('session', false, key, value);
193
+ }
194
+ registerScriptlet(setSessionStorageItem, {
195
+ name: 'set-session-storage-item.js',
196
+ world: 'ISOLATED',
197
+ dependencies: [
198
+ setLocalStorageItemFn,
199
+ ],
200
+ });
201
+
202
+ /*******************************************************************************
203
+ *
204
+ * trusted-set-local-storage-item.js
205
+ *
206
+ * Set a local storage entry to an arbitrary value.
207
+ *
208
+ * Reference:
209
+ * https://github.com/AdguardTeam/Scriptlets/blob/master/src/scriptlets/trusted-set-local-storage-item.js
210
+ *
211
+ **/
212
+
213
+ export function trustedSetLocalStorageItem(key = '', value = '') {
214
+ setLocalStorageItemFn('local', true, key, value);
215
+ }
216
+ registerScriptlet(trustedSetLocalStorageItem, {
217
+ name: 'trusted-set-local-storage-item.js',
218
+ requiresTrust: true,
219
+ world: 'ISOLATED',
220
+ dependencies: [
221
+ setLocalStorageItemFn,
222
+ ],
223
+ });
224
+
225
+ export function trustedSetSessionStorageItem(key = '', value = '') {
226
+ setLocalStorageItemFn('session', true, key, value);
227
+ }
228
+ registerScriptlet(trustedSetSessionStorageItem, {
229
+ name: 'trusted-set-session-storage-item.js',
230
+ requiresTrust: true,
231
+ world: 'ISOLATED',
232
+ dependencies: [
233
+ setLocalStorageItemFn,
234
+ ],
235
+ });
@@ -0,0 +1,54 @@
1
+ /*******************************************************************************
2
+
3
+ uBlock Origin - a comprehensive, efficient content blocker
4
+ Copyright (C) 2019-present Raymond Hill
5
+
6
+ This program is free software: you can redistribute it and/or modify
7
+ it under the terms of the GNU General Public License as published by
8
+ the Free Software Foundation, either version 3 of the License, or
9
+ (at your option) any later version.
10
+
11
+ This program is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU General Public License for more details.
15
+
16
+ You should have received a copy of the GNU General Public License
17
+ along with this program. If not, see {http://www.gnu.org/licenses/}.
18
+
19
+ Home: https://github.com/gorhill/uBlock
20
+
21
+ */
22
+
23
+ import { createArglistParser } from './shared.js';
24
+ import { registerScriptlet } from './base.js';
25
+
26
+ /******************************************************************************/
27
+
28
+ export function parseReplaceFn(s) {
29
+ if ( s.charCodeAt(0) !== 0x2F /* / */ ) { return; }
30
+ const parser = createArglistParser('/');
31
+ parser.nextArg(s, 1);
32
+ let pattern = s.slice(parser.argBeg, parser.argEnd);
33
+ if ( parser.transform ) {
34
+ pattern = parser.normalizeArg(pattern);
35
+ }
36
+ if ( pattern === '' ) { return; }
37
+ parser.nextArg(s, parser.separatorEnd);
38
+ let replacement = s.slice(parser.argBeg, parser.argEnd);
39
+ if ( parser.separatorEnd === parser.separatorBeg ) { return; }
40
+ if ( parser.transform ) {
41
+ replacement = parser.normalizeArg(replacement);
42
+ }
43
+ const flags = s.slice(parser.separatorEnd);
44
+ try {
45
+ return { re: new RegExp(pattern, flags), replacement };
46
+ } catch(_) {
47
+ }
48
+ }
49
+ registerScriptlet(parseReplaceFn, {
50
+ name: 'parse-replace.fn',
51
+ dependencies: [
52
+ createArglistParser,
53
+ ],
54
+ });