@browserless.io/browserless 2.0.0-beta-5 → 2.0.0-beta-7

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 (246) hide show
  1. package/LICENSE +2 -0
  2. package/README.md +11 -11
  3. package/bin/browserless.js +169 -11
  4. package/bin/scaffold/README.md +415 -0
  5. package/bin/scaffold/package.json +21 -0
  6. package/bin/scaffold/src/hello-world.http.ts +27 -0
  7. package/bin/scaffold/tsconfig.json +4 -0
  8. package/build/browserless.js +18 -15
  9. package/build/browsers/index.d.ts +2 -18
  10. package/build/browsers/index.js +43 -14
  11. package/build/file-system.d.ts +5 -0
  12. package/build/file-system.js +20 -5
  13. package/build/file-system.spec.d.ts +1 -0
  14. package/build/file-system.spec.js +44 -0
  15. package/build/http.d.ts +3 -3
  16. package/build/http.js +3 -3
  17. package/build/router.js +2 -4
  18. package/build/routes/chromium/http/content-post.body.json +8 -8
  19. package/build/routes/chromium/http/content-post.d.ts +15 -3
  20. package/build/routes/chromium/http/content-post.js +14 -15
  21. package/build/routes/chromium/http/download-post.d.ts +16 -3
  22. package/build/routes/chromium/http/download-post.js +17 -22
  23. package/build/routes/chromium/http/function-post.d.ts +16 -3
  24. package/build/routes/chromium/http/function-post.js +17 -22
  25. package/build/routes/chromium/http/pdf-post.body.json +8 -8
  26. package/build/routes/chromium/http/pdf-post.d.ts +15 -3
  27. package/build/routes/chromium/http/pdf-post.js +19 -15
  28. package/build/routes/chromium/http/performance.d.ts +15 -3
  29. package/build/routes/chromium/http/performance.js +15 -23
  30. package/build/routes/chromium/http/scrape-post.body.json +8 -8
  31. package/build/routes/chromium/http/scrape-post.d.ts +15 -3
  32. package/build/routes/chromium/http/scrape-post.js +15 -16
  33. package/build/routes/chromium/http/screenshot-post.body.json +8 -8
  34. package/build/routes/chromium/http/screenshot-post.d.ts +15 -3
  35. package/build/routes/chromium/http/screenshot-post.js +18 -15
  36. package/build/routes/chromium/tests/websocket.spec.js +20 -1
  37. package/build/routes/chromium/utils/function/handler.js +2 -2
  38. package/build/routes/chromium/ws/browser.d.ts +13 -3
  39. package/build/routes/chromium/ws/browser.js +10 -11
  40. package/build/routes/chromium/ws/cdp-chromium.d.ts +13 -3
  41. package/build/routes/chromium/ws/cdp-chromium.js +10 -11
  42. package/build/routes/chromium/ws/page.d.ts +13 -3
  43. package/build/routes/chromium/ws/page.js +10 -11
  44. package/build/routes/chromium/ws/playwright-chromium.d.ts +13 -3
  45. package/build/routes/chromium/ws/playwright-chromium.js +11 -12
  46. package/build/routes/firefox/ws/playwright-firefox.d.ts +13 -3
  47. package/build/routes/firefox/ws/playwright-firefox.js +11 -12
  48. package/build/routes/management/http/config-get.d.ts +15 -3
  49. package/build/routes/management/http/config-get.js +15 -20
  50. package/build/routes/management/http/metrics-get.d.ts +15 -3
  51. package/build/routes/management/http/metrics-get.js +16 -21
  52. package/build/routes/management/http/metrics-total-get.d.ts +15 -3
  53. package/build/routes/management/http/metrics-total-get.js +16 -21
  54. package/build/routes/management/http/sessions-get.d.ts +15 -3
  55. package/build/routes/management/http/sessions-get.js +16 -20
  56. package/build/routes/management/http/static-get.d.ts +15 -3
  57. package/build/routes/management/http/static-get.js +15 -20
  58. package/build/routes/webkit/ws/playwright-webkit.d.ts +13 -3
  59. package/build/routes/webkit/ws/playwright-webkit.js +11 -12
  60. package/build/server.js +0 -1
  61. package/build/types.d.ts +48 -38
  62. package/build/types.js +135 -0
  63. package/extensions/ublock/1p-filters.html +0 -1
  64. package/extensions/ublock/3p-filters.html +0 -2
  65. package/extensions/ublock/_locales/bg/messages.json +6 -6
  66. package/extensions/ublock/_locales/br_FR/messages.json +14 -14
  67. package/extensions/ublock/_locales/bs/messages.json +8 -8
  68. package/extensions/ublock/_locales/ca/messages.json +1 -1
  69. package/extensions/ublock/_locales/da/messages.json +5 -5
  70. package/extensions/ublock/_locales/fa/messages.json +1 -1
  71. package/extensions/ublock/_locales/fi/messages.json +6 -6
  72. package/extensions/ublock/_locales/hr/messages.json +4 -4
  73. package/extensions/ublock/_locales/nb/messages.json +1 -1
  74. package/extensions/ublock/_locales/no/messages.json +1 -1
  75. package/extensions/ublock/_locales/ro/messages.json +2 -2
  76. package/extensions/ublock/_locales/ru/messages.json +1 -1
  77. package/extensions/ublock/_locales/sk/messages.json +1 -1
  78. package/extensions/ublock/_locales/sv/messages.json +2 -2
  79. package/extensions/ublock/_locales/te/messages.json +17 -17
  80. package/extensions/ublock/_locales/vi/messages.json +12 -12
  81. package/extensions/ublock/_locales/zh_TW/messages.json +13 -13
  82. package/extensions/ublock/assets/assets.json +3 -3
  83. package/extensions/ublock/assets/resources/scriptlets.js +218 -97
  84. package/extensions/ublock/assets/thirdparties/easylist/easylist.txt +3010 -2056
  85. package/extensions/ublock/assets/thirdparties/easylist/easyprivacy.txt +624 -433
  86. package/extensions/ublock/assets/thirdparties/pgl.yoyo.org/as/serverlist +93 -24
  87. package/extensions/ublock/assets/thirdparties/publicsuffix.org/list/effective_tld_names.dat +7 -15
  88. package/extensions/ublock/assets/thirdparties/urlhaus-filter/urlhaus-filter-online.txt +795 -777
  89. package/extensions/ublock/assets/ublock/badware.min.txt +138 -72
  90. package/extensions/ublock/assets/ublock/filters.min.txt +1929 -2735
  91. package/extensions/ublock/assets/ublock/privacy.min.txt +57 -26
  92. package/extensions/ublock/assets/ublock/quick-fixes.min.txt +125 -74
  93. package/extensions/ublock/assets/ublock/unbreak.min.txt +46 -53
  94. package/extensions/ublock/css/codemirror.css +8 -7
  95. package/extensions/ublock/css/dom-inspector.css +40 -0
  96. package/extensions/ublock/css/logger-ui-inspector.css +7 -1
  97. package/extensions/ublock/css/logger-ui.css +12 -5
  98. package/extensions/ublock/css/popup-fenix.css +1 -1
  99. package/extensions/ublock/devtools.html +1 -0
  100. package/extensions/ublock/js/1p-filters.js +4 -3
  101. package/extensions/ublock/js/3p-filters.js +25 -31
  102. package/extensions/ublock/js/about.js +1 -1
  103. package/extensions/ublock/js/advanced-settings.js +1 -1
  104. package/extensions/ublock/js/asset-viewer.js +1 -1
  105. package/extensions/ublock/js/assets.js +74 -44
  106. package/extensions/ublock/js/background.js +9 -3
  107. package/extensions/ublock/js/base64-custom.js +1 -1
  108. package/extensions/ublock/js/benchmarks.js +1 -1
  109. package/extensions/ublock/js/biditrie.js +1 -1
  110. package/extensions/ublock/js/broadcast.js +75 -0
  111. package/extensions/ublock/js/cachestorage.js +68 -45
  112. package/extensions/ublock/js/click2load.js +1 -1
  113. package/extensions/ublock/js/cloud-ui.js +1 -1
  114. package/extensions/ublock/js/code-viewer.js +1 -1
  115. package/extensions/ublock/js/codemirror/search-thread.js +1 -1
  116. package/extensions/ublock/js/codemirror/search.js +1 -1
  117. package/extensions/ublock/js/codemirror/ubo-dynamic-filtering.js +1 -1
  118. package/extensions/ublock/js/codemirror/ubo-static-filtering.js +98 -24
  119. package/extensions/ublock/js/commands.js +1 -1
  120. package/extensions/ublock/js/console.js +1 -1
  121. package/extensions/ublock/js/contentscript-extra.js +1 -1
  122. package/extensions/ublock/js/contentscript.js +1 -3
  123. package/extensions/ublock/js/contextmenu.js +1 -1
  124. package/extensions/ublock/js/cosmetic-filtering.js +4 -4
  125. package/extensions/ublock/js/dashboard-common.js +1 -1
  126. package/extensions/ublock/js/dashboard.js +1 -1
  127. package/extensions/ublock/js/devtools.js +23 -15
  128. package/extensions/ublock/js/diff-updater.js +3 -3
  129. package/extensions/ublock/js/document-blocked.js +1 -1
  130. package/extensions/ublock/js/dom-inspector.js +68 -0
  131. package/extensions/ublock/js/dom.js +1 -1
  132. package/extensions/ublock/js/dyna-rules.js +1 -1
  133. package/extensions/ublock/js/dynamic-net-filtering.js +1 -1
  134. package/extensions/ublock/js/epicker-ui.js +35 -59
  135. package/extensions/ublock/js/fa-icons.js +1 -1
  136. package/extensions/ublock/js/filtering-context.js +1 -1
  137. package/extensions/ublock/js/filtering-engines.js +1 -1
  138. package/extensions/ublock/js/hnswitches.js +1 -1
  139. package/extensions/ublock/js/hntrie.js +1 -1
  140. package/extensions/ublock/js/html-filtering.js +1 -1
  141. package/extensions/ublock/js/httpheader-filtering.js +1 -1
  142. package/extensions/ublock/js/i18n.js +1 -1
  143. package/extensions/ublock/js/is-webrtc-supported.js +1 -1
  144. package/extensions/ublock/js/logger-ui-inspector.js +203 -145
  145. package/extensions/ublock/js/logger-ui.js +21 -5
  146. package/extensions/ublock/js/logger.js +6 -2
  147. package/extensions/ublock/js/lz4.js +2 -2
  148. package/extensions/ublock/js/messaging.js +266 -166
  149. package/extensions/ublock/js/mrucache.js +58 -0
  150. package/extensions/ublock/js/pagestore.js +1 -1
  151. package/extensions/ublock/js/popup-fenix.js +2 -1
  152. package/extensions/ublock/js/redirect-engine.js +1 -1
  153. package/extensions/ublock/js/redirect-resources.js +1 -12
  154. package/extensions/ublock/js/reverselookup-worker.js +1 -1
  155. package/extensions/ublock/js/reverselookup.js +1 -1
  156. package/extensions/ublock/js/scriptlet-filtering-core.js +300 -0
  157. package/extensions/ublock/js/scriptlet-filtering.js +122 -350
  158. package/extensions/ublock/js/scriptlets/cosmetic-logger.js +36 -47
  159. package/extensions/ublock/js/scriptlets/cosmetic-off.js +1 -1
  160. package/extensions/ublock/js/scriptlets/cosmetic-on.js +1 -1
  161. package/extensions/ublock/js/scriptlets/cosmetic-report.js +1 -1
  162. package/extensions/ublock/js/scriptlets/dom-inspector.js +341 -323
  163. package/extensions/ublock/js/scriptlets/dom-survey-elements.js +1 -1
  164. package/extensions/ublock/js/scriptlets/dom-survey-scripts.js +1 -1
  165. package/extensions/ublock/js/scriptlets/epicker.js +80 -89
  166. package/extensions/ublock/js/scriptlets/load-3p-css.js +1 -1
  167. package/extensions/ublock/js/scriptlets/load-large-media-all.js +1 -1
  168. package/extensions/ublock/js/scriptlets/load-large-media-interactive.js +1 -1
  169. package/extensions/ublock/js/scriptlets/noscript-spoof.js +1 -1
  170. package/extensions/ublock/js/scriptlets/should-inject-contentscript.js +1 -1
  171. package/extensions/ublock/js/scriptlets/subscriber.js +1 -1
  172. package/extensions/ublock/js/scriptlets/updater.js +20 -3
  173. package/extensions/ublock/js/settings.js +1 -1
  174. package/extensions/ublock/js/start.js +19 -20
  175. package/extensions/ublock/js/static-dnr-filtering.js +1 -1
  176. package/extensions/ublock/js/static-ext-filtering-db.js +1 -1
  177. package/extensions/ublock/js/static-ext-filtering.js +1 -1
  178. package/extensions/ublock/js/static-filtering-io.js +1 -1
  179. package/extensions/ublock/js/static-filtering-parser.js +5 -3
  180. package/extensions/ublock/js/static-net-filtering.js +57 -37
  181. package/extensions/ublock/js/storage.js +49 -29
  182. package/extensions/ublock/js/support.js +4 -4
  183. package/extensions/ublock/js/tab.js +1 -1
  184. package/extensions/ublock/js/tasks.js +1 -1
  185. package/extensions/ublock/js/text-encode.js +1 -1
  186. package/extensions/ublock/js/text-utils.js +1 -1
  187. package/extensions/ublock/js/theme.js +1 -1
  188. package/extensions/ublock/js/traffic.js +2 -1
  189. package/extensions/ublock/js/ublock.js +15 -11
  190. package/extensions/ublock/js/uri-utils.js +1 -1
  191. package/extensions/ublock/js/url-net-filtering.js +1 -1
  192. package/extensions/ublock/js/utils.js +1 -73
  193. package/extensions/ublock/js/vapi-background-ext.js +1 -1
  194. package/extensions/ublock/js/vapi-background.js +92 -83
  195. package/extensions/ublock/js/vapi-client.js +4 -33
  196. package/extensions/ublock/js/vapi-common.js +16 -30
  197. package/extensions/ublock/js/vapi.js +1 -1
  198. package/extensions/ublock/js/wasm/biditrie.wat +1 -1
  199. package/extensions/ublock/js/wasm/hntrie.wat +1 -1
  200. package/extensions/ublock/js/webext.js +1 -1
  201. package/extensions/ublock/js/whitelist.js +1 -1
  202. package/extensions/ublock/logger-ui.html +2 -2
  203. package/extensions/ublock/manifest.json +1 -1
  204. package/extensions/ublock/support.html +0 -1
  205. package/extensions/ublock/web_accessible_resources/dom-inspector.html +25 -0
  206. package/extensions/ublock/web_accessible_resources/epicker-ui.html +0 -1
  207. package/extensions/ublock/web_accessible_resources/googletagservices_gpt.js +1 -0
  208. package/package.json +7 -19
  209. package/scripts/build-open-api.js +7 -4
  210. package/src/browserless.ts +42 -18
  211. package/src/browsers/index.ts +48 -20
  212. package/src/file-system.spec.ts +58 -0
  213. package/src/file-system.ts +36 -8
  214. package/src/http.ts +3 -3
  215. package/src/router.ts +2 -6
  216. package/src/routes/chromium/http/content-post.ts +13 -16
  217. package/src/routes/chromium/http/download-post.ts +16 -27
  218. package/src/routes/chromium/http/function-post.ts +16 -25
  219. package/src/routes/chromium/http/pdf-post.ts +19 -15
  220. package/src/routes/chromium/http/performance.ts +14 -26
  221. package/src/routes/chromium/http/scrape-post.ts +14 -16
  222. package/src/routes/chromium/http/screenshot-post.ts +18 -15
  223. package/src/routes/chromium/tests/websocket.spec.ts +28 -1
  224. package/src/routes/chromium/utils/function/handler.ts +2 -1
  225. package/src/routes/chromium/ws/browser.ts +10 -12
  226. package/src/routes/chromium/ws/cdp-chromium.ts +10 -12
  227. package/src/routes/chromium/ws/page.ts +10 -12
  228. package/src/routes/chromium/ws/playwright-chromium.ts +10 -12
  229. package/src/routes/firefox/ws/playwright-firefox.ts +10 -12
  230. package/src/routes/management/http/config-get.ts +14 -23
  231. package/src/routes/management/http/metrics-get.ts +15 -24
  232. package/src/routes/management/http/metrics-total-get.ts +15 -26
  233. package/src/routes/management/http/sessions-get.ts +15 -23
  234. package/src/routes/management/http/static-get.ts +14 -22
  235. package/src/routes/webkit/ws/playwright-webkit.ts +10 -12
  236. package/src/server.ts +0 -1
  237. package/src/types.ts +59 -45
  238. package/static/docs/browserless-logo-inline.svg +1 -0
  239. package/static/docs/index.html +27 -0
  240. package/static/docs/swagger.json +33 -33
  241. package/static/function/client.js +626 -78
  242. package/extensions/ublock/js/vapi-client-extra.js +0 -312
  243. package/extensions/ublock/web_accessible_resources/addthis_widget.js +0 -39
  244. package/extensions/ublock/web_accessible_resources/ligatus_angular-tag.js +0 -29
  245. package/extensions/ublock/web_accessible_resources/monkeybroker.js +0 -43
  246. package/extensions/ublock/web_accessible_resources/mxpnl_mixpanel.js +0 -51
@@ -0,0 +1,58 @@
1
+ /*******************************************************************************
2
+
3
+ uBlock Origin - a comprehensive, efficient content blocker
4
+ Copyright (C) 2014-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
+ 'use strict';
23
+
24
+ export class MRUCache {
25
+ constructor(maxSize) {
26
+ this.maxSize = maxSize;
27
+ this.array = [];
28
+ this.map = new Map();
29
+ this.resetTime = Date.now();
30
+ }
31
+ add(key, value) {
32
+ const found = this.map.has(key);
33
+ this.map.set(key, value);
34
+ if ( found ) { return; }
35
+ if ( this.array.length === this.maxSize ) {
36
+ this.map.delete(this.array.pop());
37
+ }
38
+ this.array.unshift(key);
39
+ }
40
+ remove(key) {
41
+ if ( this.map.delete(key) === false ) { return; }
42
+ this.array.splice(this.array.indexOf(key), 1);
43
+ }
44
+ lookup(key) {
45
+ const value = this.map.get(key);
46
+ if ( value === undefined ) { return; }
47
+ if ( this.array[0] === key ) { return value; }
48
+ const i = this.array.indexOf(key);
49
+ this.array.copyWithin(1, 0, i);
50
+ this.array[0] = key;
51
+ return value;
52
+ }
53
+ reset() {
54
+ this.array = [];
55
+ this.map.clear();
56
+ this.resetTime = Date.now();
57
+ }
58
+ }
@@ -1,6 +1,6 @@
1
1
  /*******************************************************************************
2
2
 
3
- uBlock Origin - a browser extension to block requests.
3
+ uBlock Origin - a comprehensive, efficient content blocker
4
4
  Copyright (C) 2014-present Raymond Hill
5
5
 
6
6
  This program is free software: you can redistribute it and/or modify
@@ -1,6 +1,6 @@
1
1
  /*******************************************************************************
2
2
 
3
- uBlock Origin - a browser extension to block requests.
3
+ uBlock Origin - a comprehensive, efficient content blocker
4
4
  Copyright (C) 2014-present Raymond Hill
5
5
 
6
6
  This program is free software: you can redistribute it and/or modify
@@ -1479,6 +1479,7 @@ const getPopupData = async function(tabId, first = false) {
1479
1479
  selfURL.searchParams.get('portrait')
1480
1480
  ) {
1481
1481
  dom.cl.add(dom.root, 'portrait');
1482
+ dom.cl.remove(dom.root, 'desktop');
1482
1483
  } else if ( dom.cl.has(dom.root, 'desktop') ) {
1483
1484
  await nextFrames(8);
1484
1485
  const main = qs$('#main');
@@ -1,6 +1,6 @@
1
1
  /*******************************************************************************
2
2
 
3
- uBlock Origin - a browser extension to block requests.
3
+ uBlock Origin - a comprehensive, efficient content blocker
4
4
  Copyright (C) 2015-present Raymond Hill
5
5
 
6
6
  This program is free software: you can redistribute it and/or modify
@@ -1,6 +1,6 @@
1
1
  /*******************************************************************************
2
2
 
3
- uBlock Origin - a browser extension to block requests.
3
+ uBlock Origin - a comprehensive, efficient content blocker
4
4
  Copyright (C) 2015-present Raymond Hill
5
5
 
6
6
  This program is free software: you can redistribute it and/or modify
@@ -48,9 +48,6 @@ export default new Map([
48
48
  alias: '32x32-transparent.png',
49
49
  data: 'blob',
50
50
  } ],
51
- [ 'addthis_widget.js', {
52
- alias: 'addthis.com/addthis_widget.js',
53
- } ],
54
51
  [ 'amazon_ads.js', {
55
52
  alias: 'amazon-adsystem.com/aax2/amzn_ads.js',
56
53
  data: 'text',
@@ -116,14 +113,6 @@ export default new Map([
116
113
  } ],
117
114
  [ 'hd-main.js', {
118
115
  } ],
119
- [ 'ligatus_angular-tag.js', {
120
- alias: 'ligatus.com/*/angular-tag.js',
121
- } ],
122
- [ 'mxpnl_mixpanel.js', {
123
- } ],
124
- [ 'monkeybroker.js', {
125
- alias: 'd3pkae9owd2lcf.cloudfront.net/mb105.js',
126
- } ],
127
116
  [ 'nobab.js', {
128
117
  alias: [ 'bab-defuser.js', 'prevent-bab.js' ],
129
118
  data: 'text',
@@ -1,6 +1,6 @@
1
1
  /*******************************************************************************
2
2
 
3
- uBlock Origin - a browser extension to block requests.
3
+ uBlock Origin - a comprehensive, efficient content blocker
4
4
  Copyright (C) 2015-present Raymond Hill
5
5
 
6
6
  This program is free software: you can redistribute it and/or modify
@@ -1,6 +1,6 @@
1
1
  /*******************************************************************************
2
2
 
3
- uBlock Origin - a browser extension to block requests.
3
+ uBlock Origin - a comprehensive, efficient content blocker
4
4
  Copyright (C) 2015-present Raymond Hill
5
5
 
6
6
  This program is free software: you can redistribute it and/or modify
@@ -0,0 +1,300 @@
1
+ /*******************************************************************************
2
+
3
+ uBlock Origin - a comprehensive, efficient content blocker
4
+ Copyright (C) 2017-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
+ 'use strict';
23
+
24
+ /******************************************************************************/
25
+
26
+ import { redirectEngine as reng } from './redirect-engine.js';
27
+ import { StaticExtFilteringHostnameDB } from './static-ext-filtering-db.js';
28
+
29
+ /******************************************************************************/
30
+
31
+ // Increment when internal representation changes
32
+ const VERSION = 1;
33
+
34
+ const $scriptlets = new Set();
35
+ const $exceptions = new Set();
36
+ const $mainWorldMap = new Map();
37
+ const $isolatedWorldMap = new Map();
38
+
39
+ /******************************************************************************/
40
+
41
+ const normalizeRawFilter = (parser, sourceIsTrusted = false) => {
42
+ const args = parser.getScriptletArgs();
43
+ if ( args.length !== 0 ) {
44
+ let token = `${args[0]}.js`;
45
+ if ( reng.aliases.has(token) ) {
46
+ token = reng.aliases.get(token);
47
+ }
48
+ if ( parser.isException() !== true ) {
49
+ if ( sourceIsTrusted !== true ) {
50
+ if ( reng.tokenRequiresTrust(token) ) { return; }
51
+ }
52
+ }
53
+ args[0] = token.slice(0, -3);
54
+ }
55
+ return JSON.stringify(args);
56
+ };
57
+
58
+ const lookupScriptlet = (rawToken, mainMap, isolatedMap, debug = false) => {
59
+ if ( mainMap.has(rawToken) || isolatedMap.has(rawToken) ) { return; }
60
+ const args = JSON.parse(rawToken);
61
+ const token = `${args[0]}.js`;
62
+ const details = reng.contentFromName(token, 'text/javascript');
63
+ if ( details === undefined ) { return; }
64
+ const targetWorldMap = details.world !== 'ISOLATED' ? mainMap : isolatedMap;
65
+ const content = patchScriptlet(details.js, args.slice(1));
66
+ const dependencies = details.dependencies || [];
67
+ while ( dependencies.length !== 0 ) {
68
+ const token = dependencies.shift();
69
+ if ( targetWorldMap.has(token) ) { continue; }
70
+ const details = reng.contentFromName(token, 'fn/javascript') ||
71
+ reng.contentFromName(token, 'text/javascript');
72
+ if ( details === undefined ) { continue; }
73
+ targetWorldMap.set(token, details.js);
74
+ if ( Array.isArray(details.dependencies) === false ) { continue; }
75
+ dependencies.push(...details.dependencies);
76
+ }
77
+ targetWorldMap.set(rawToken, [
78
+ 'try {',
79
+ '// >>>> scriptlet start',
80
+ content,
81
+ '// <<<< scriptlet end',
82
+ '} catch (e) {',
83
+ debug ? 'console.error(e);' : '',
84
+ '}',
85
+ ].join('\n'));
86
+ };
87
+
88
+ // Fill-in scriptlet argument placeholders.
89
+ const patchScriptlet = (content, arglist) => {
90
+ if ( content.startsWith('function') && content.endsWith('}') ) {
91
+ content = `(${content})({{args}});`;
92
+ }
93
+ for ( let i = 0; i < arglist.length; i++ ) {
94
+ content = content.replace(`{{${i+1}}}`, arglist[i]);
95
+ }
96
+ return content.replace('{{args}}',
97
+ JSON.stringify(arglist).slice(1,-1).replace(/\$/g, '$$$')
98
+ );
99
+ };
100
+
101
+ const decompile = json => {
102
+ const args = JSON.parse(json).map(s => s.replace(/,/g, '\\,'));
103
+ if ( args.length === 0 ) { return '+js()'; }
104
+ return `+js(${args.join(', ')})`;
105
+ };
106
+
107
+ /******************************************************************************/
108
+
109
+ export class ScriptletFilteringEngine {
110
+ constructor() {
111
+ this.acceptedCount = 0;
112
+ this.discardedCount = 0;
113
+ this.scriptletDB = new StaticExtFilteringHostnameDB(1, VERSION);
114
+ this.duplicates = new Set();
115
+ }
116
+
117
+ getFilterCount() {
118
+ return this.scriptletDB.size;
119
+ }
120
+
121
+ reset() {
122
+ this.scriptletDB.clear();
123
+ this.duplicates.clear();
124
+ this.acceptedCount = 0;
125
+ this.discardedCount = 0;
126
+ }
127
+
128
+ freeze() {
129
+ this.duplicates.clear();
130
+ this.scriptletDB.collectGarbage();
131
+ }
132
+
133
+ // parser: instance of AstFilterParser from static-filtering-parser.js
134
+ // writer: instance of CompiledListWriter from static-filtering-io.js
135
+ compile(parser, writer) {
136
+ writer.select('SCRIPTLET_FILTERS');
137
+
138
+ // Only exception filters are allowed to be global.
139
+ const isException = parser.isException();
140
+ const normalized = normalizeRawFilter(parser, writer.properties.get('trustedSource'));
141
+
142
+ // Can fail if there is a mismatch with trust requirement
143
+ if ( normalized === undefined ) { return; }
144
+
145
+ // Tokenless is meaningful only for exception filters.
146
+ if ( normalized === '[]' && isException === false ) { return; }
147
+
148
+ if ( parser.hasOptions() === false ) {
149
+ if ( isException ) {
150
+ writer.push([ 32, '', 1, normalized ]);
151
+ }
152
+ return;
153
+ }
154
+
155
+ // https://github.com/gorhill/uBlock/issues/3375
156
+ // Ignore instances of exception filter with negated hostnames,
157
+ // because there is no way to create an exception to an exception.
158
+
159
+ for ( const { hn, not, bad } of parser.getExtFilterDomainIterator() ) {
160
+ if ( bad ) { continue; }
161
+ let kind = 0;
162
+ if ( isException ) {
163
+ if ( not ) { continue; }
164
+ kind |= 1;
165
+ } else if ( not ) {
166
+ kind |= 1;
167
+ }
168
+ writer.push([ 32, hn, kind, normalized ]);
169
+ }
170
+ }
171
+
172
+ // writer: instance of CompiledListReader from static-filtering-io.js
173
+ fromCompiledContent(reader) {
174
+ reader.select('SCRIPTLET_FILTERS');
175
+
176
+ while ( reader.next() ) {
177
+ this.acceptedCount += 1;
178
+ const fingerprint = reader.fingerprint();
179
+ if ( this.duplicates.has(fingerprint) ) {
180
+ this.discardedCount += 1;
181
+ continue;
182
+ }
183
+ this.duplicates.add(fingerprint);
184
+ const args = reader.args();
185
+ if ( args.length < 4 ) { continue; }
186
+ this.scriptletDB.store(args[1], args[2], args[3]);
187
+ }
188
+ }
189
+
190
+ toSelfie() {
191
+ return this.scriptletDB.toSelfie();
192
+ }
193
+
194
+ fromSelfie(selfie) {
195
+ if ( selfie instanceof Object === false ) { return false; }
196
+ if ( selfie.version !== VERSION ) { return false; }
197
+ this.scriptletDB.fromSelfie(selfie);
198
+ return true;
199
+ }
200
+
201
+ retrieve(request, options = {}) {
202
+ if ( this.scriptletDB.size === 0 ) { return; }
203
+
204
+ $scriptlets.clear();
205
+ $exceptions.clear();
206
+
207
+ const { hostname } = request;
208
+
209
+ this.scriptletDB.retrieve(hostname, [ $scriptlets, $exceptions ]);
210
+ const entity = request.entity !== ''
211
+ ? `${hostname.slice(0, -request.domain.length)}${request.entity}`
212
+ : '*';
213
+ this.scriptletDB.retrieve(entity, [ $scriptlets, $exceptions ], 1);
214
+ if ( $scriptlets.size === 0 ) { return; }
215
+
216
+ // Wholly disable scriptlet injection?
217
+ if ( $exceptions.has('[]') ) {
218
+ return { filters: '#@#+js()' };
219
+ }
220
+
221
+ for ( const token of $exceptions ) {
222
+ if ( $scriptlets.has(token) ) {
223
+ $scriptlets.delete(token);
224
+ } else {
225
+ $exceptions.delete(token);
226
+ }
227
+ }
228
+
229
+ for ( const token of $scriptlets ) {
230
+ lookupScriptlet(token, $mainWorldMap, $isolatedWorldMap, options.debug);
231
+ }
232
+
233
+ const mainWorldCode = [];
234
+ for ( const js of $mainWorldMap.values() ) {
235
+ mainWorldCode.push(js);
236
+ }
237
+
238
+ const isolatedWorldCode = [];
239
+ for ( const js of $isolatedWorldMap.values() ) {
240
+ isolatedWorldCode.push(js);
241
+ }
242
+
243
+ const scriptletDetails = {
244
+ mainWorld: mainWorldCode.join('\n\n'),
245
+ isolatedWorld: isolatedWorldCode.join('\n\n'),
246
+ filters: [
247
+ ...Array.from($scriptlets).map(s => `##${decompile(s)}`),
248
+ ...Array.from($exceptions).map(s => `#@#${decompile(s)}`),
249
+ ].join('\n'),
250
+ };
251
+ $mainWorldMap.clear();
252
+ $isolatedWorldMap.clear();
253
+
254
+ if ( scriptletDetails.mainWorld === '' ) {
255
+ if ( scriptletDetails.isolatedWorld === '' ) {
256
+ return { filters: scriptletDetails.filters };
257
+ }
258
+ }
259
+
260
+ const scriptletGlobals = options.scriptletGlobals || [];
261
+
262
+ if ( options.debug ) {
263
+ scriptletGlobals.push([ 'canDebug', true ]);
264
+ }
265
+
266
+ return {
267
+ mainWorld: scriptletDetails.mainWorld === '' ? '' : [
268
+ '(function() {',
269
+ '// >>>> start of private namespace',
270
+ '',
271
+ options.debugScriptlets ? 'debugger;' : ';',
272
+ '',
273
+ // For use by scriptlets to share local data among themselves
274
+ `const scriptletGlobals = new Map(${JSON.stringify(scriptletGlobals, null, 2)});`,
275
+ '',
276
+ scriptletDetails.mainWorld,
277
+ '',
278
+ '// <<<< end of private namespace',
279
+ '})();',
280
+ ].join('\n'),
281
+ isolatedWorld: scriptletDetails.isolatedWorld === '' ? '' : [
282
+ 'function() {',
283
+ '// >>>> start of private namespace',
284
+ '',
285
+ options.debugScriptlets ? 'debugger;' : ';',
286
+ '',
287
+ // For use by scriptlets to share local data among themselves
288
+ `const scriptletGlobals = new Map(${JSON.stringify(scriptletGlobals, null, 2)});`,
289
+ '',
290
+ scriptletDetails.isolatedWorld,
291
+ '',
292
+ '// <<<< end of private namespace',
293
+ '}',
294
+ ].join('\n'),
295
+ filters: scriptletDetails.filters,
296
+ };
297
+ }
298
+ }
299
+
300
+ /******************************************************************************/