1 | (function (global, factory) {
|
2 | if (typeof define === "function" && define.amd) {
|
3 | define("webextension-polyfill", ["module"], factory);
|
4 | } else if (typeof exports !== "undefined") {
|
5 | factory(module);
|
6 | } else {
|
7 | var mod = {
|
8 | exports: {}
|
9 | };
|
10 | factory(mod);
|
11 | global.browser = mod.exports;
|
12 | }
|
13 | })(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (module) {
|
14 | /* webextension-polyfill - v0.9.0 - Fri Mar 25 2022 17:00:23 */
|
15 |
|
16 | /* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
|
17 |
|
18 | /* vim: set sts=2 sw=2 et tw=80: */
|
19 |
|
20 | /* This Source Code Form is subject to the terms of the Mozilla Public
|
21 | * License, v. 2.0. If a copy of the MPL was not distributed with this
|
22 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
23 | ;
|
24 |
|
25 | if (typeof globalThis != "object" || typeof chrome != "object" || !chrome || !chrome.runtime || !chrome.runtime.id) {
|
26 | throw new Error("This script should only be loaded in a browser extension.");
|
27 | }
|
28 |
|
29 | if (typeof globalThis.browser === "undefined" || Object.getPrototypeOf(globalThis.browser) !== Object.prototype) {
|
30 | const CHROME_SEND_MESSAGE_CALLBACK_NO_RESPONSE_MESSAGE = "The message port closed before a response was received.";
|
31 | const SEND_RESPONSE_DEPRECATION_WARNING = "Returning a Promise is the preferred way to send a reply from an onMessage/onMessageExternal listener, as the sendResponse will be removed from the specs (See https://developer.mozilla.org/docs/Mozilla/Add-ons/WebExtensions/API/runtime/onMessage)"; // Wrapping the bulk of this polyfill in a one-time-use function is a minor
|
32 | // optimization for Firefox. Since Spidermonkey does not fully parse the
|
33 | // contents of a function until the first time it's called, and since it will
|
34 | // never actually need to be called, this allows the polyfill to be included
|
35 | // in Firefox nearly for free.
|
36 |
|
37 | const wrapAPIs = extensionAPIs => {
|
38 | // NOTE: apiMetadata is associated to the content of the api-metadata.json file
|
39 | // at build time by replacing the following "include" with the content of the
|
40 | // JSON file.
|
41 | const apiMetadata = {
|
42 | "alarms": {
|
43 | "clear": {
|
44 | "minArgs": 0,
|
45 | "maxArgs": 1
|
46 | },
|
47 | "clearAll": {
|
48 | "minArgs": 0,
|
49 | "maxArgs": 0
|
50 | },
|
51 | "get": {
|
52 | "minArgs": 0,
|
53 | "maxArgs": 1
|
54 | },
|
55 | "getAll": {
|
56 | "minArgs": 0,
|
57 | "maxArgs": 0
|
58 | }
|
59 | },
|
60 | "bookmarks": {
|
61 | "create": {
|
62 | "minArgs": 1,
|
63 | "maxArgs": 1
|
64 | },
|
65 | "get": {
|
66 | "minArgs": 1,
|
67 | "maxArgs": 1
|
68 | },
|
69 | "getChildren": {
|
70 | "minArgs": 1,
|
71 | "maxArgs": 1
|
72 | },
|
73 | "getRecent": {
|
74 | "minArgs": 1,
|
75 | "maxArgs": 1
|
76 | },
|
77 | "getSubTree": {
|
78 | "minArgs": 1,
|
79 | "maxArgs": 1
|
80 | },
|
81 | "getTree": {
|
82 | "minArgs": 0,
|
83 | "maxArgs": 0
|
84 | },
|
85 | "move": {
|
86 | "minArgs": 2,
|
87 | "maxArgs": 2
|
88 | },
|
89 | "remove": {
|
90 | "minArgs": 1,
|
91 | "maxArgs": 1
|
92 | },
|
93 | "removeTree": {
|
94 | "minArgs": 1,
|
95 | "maxArgs": 1
|
96 | },
|
97 | "search": {
|
98 | "minArgs": 1,
|
99 | "maxArgs": 1
|
100 | },
|
101 | "update": {
|
102 | "minArgs": 2,
|
103 | "maxArgs": 2
|
104 | }
|
105 | },
|
106 | "browserAction": {
|
107 | "disable": {
|
108 | "minArgs": 0,
|
109 | "maxArgs": 1,
|
110 | "fallbackToNoCallback": true
|
111 | },
|
112 | "enable": {
|
113 | "minArgs": 0,
|
114 | "maxArgs": 1,
|
115 | "fallbackToNoCallback": true
|
116 | },
|
117 | "getBadgeBackgroundColor": {
|
118 | "minArgs": 1,
|
119 | "maxArgs": 1
|
120 | },
|
121 | "getBadgeText": {
|
122 | "minArgs": 1,
|
123 | "maxArgs": 1
|
124 | },
|
125 | "getPopup": {
|
126 | "minArgs": 1,
|
127 | "maxArgs": 1
|
128 | },
|
129 | "getTitle": {
|
130 | "minArgs": 1,
|
131 | "maxArgs": 1
|
132 | },
|
133 | "openPopup": {
|
134 | "minArgs": 0,
|
135 | "maxArgs": 0
|
136 | },
|
137 | "setBadgeBackgroundColor": {
|
138 | "minArgs": 1,
|
139 | "maxArgs": 1,
|
140 | "fallbackToNoCallback": true
|
141 | },
|
142 | "setBadgeText": {
|
143 | "minArgs": 1,
|
144 | "maxArgs": 1,
|
145 | "fallbackToNoCallback": true
|
146 | },
|
147 | "setIcon": {
|
148 | "minArgs": 1,
|
149 | "maxArgs": 1
|
150 | },
|
151 | "setPopup": {
|
152 | "minArgs": 1,
|
153 | "maxArgs": 1,
|
154 | "fallbackToNoCallback": true
|
155 | },
|
156 | "setTitle": {
|
157 | "minArgs": 1,
|
158 | "maxArgs": 1,
|
159 | "fallbackToNoCallback": true
|
160 | }
|
161 | },
|
162 | "browsingData": {
|
163 | "remove": {
|
164 | "minArgs": 2,
|
165 | "maxArgs": 2
|
166 | },
|
167 | "removeCache": {
|
168 | "minArgs": 1,
|
169 | "maxArgs": 1
|
170 | },
|
171 | "removeCookies": {
|
172 | "minArgs": 1,
|
173 | "maxArgs": 1
|
174 | },
|
175 | "removeDownloads": {
|
176 | "minArgs": 1,
|
177 | "maxArgs": 1
|
178 | },
|
179 | "removeFormData": {
|
180 | "minArgs": 1,
|
181 | "maxArgs": 1
|
182 | },
|
183 | "removeHistory": {
|
184 | "minArgs": 1,
|
185 | "maxArgs": 1
|
186 | },
|
187 | "removeLocalStorage": {
|
188 | "minArgs": 1,
|
189 | "maxArgs": 1
|
190 | },
|
191 | "removePasswords": {
|
192 | "minArgs": 1,
|
193 | "maxArgs": 1
|
194 | },
|
195 | "removePluginData": {
|
196 | "minArgs": 1,
|
197 | "maxArgs": 1
|
198 | },
|
199 | "settings": {
|
200 | "minArgs": 0,
|
201 | "maxArgs": 0
|
202 | }
|
203 | },
|
204 | "commands": {
|
205 | "getAll": {
|
206 | "minArgs": 0,
|
207 | "maxArgs": 0
|
208 | }
|
209 | },
|
210 | "contextMenus": {
|
211 | "remove": {
|
212 | "minArgs": 1,
|
213 | "maxArgs": 1
|
214 | },
|
215 | "removeAll": {
|
216 | "minArgs": 0,
|
217 | "maxArgs": 0
|
218 | },
|
219 | "update": {
|
220 | "minArgs": 2,
|
221 | "maxArgs": 2
|
222 | }
|
223 | },
|
224 | "cookies": {
|
225 | "get": {
|
226 | "minArgs": 1,
|
227 | "maxArgs": 1
|
228 | },
|
229 | "getAll": {
|
230 | "minArgs": 1,
|
231 | "maxArgs": 1
|
232 | },
|
233 | "getAllCookieStores": {
|
234 | "minArgs": 0,
|
235 | "maxArgs": 0
|
236 | },
|
237 | "remove": {
|
238 | "minArgs": 1,
|
239 | "maxArgs": 1
|
240 | },
|
241 | "set": {
|
242 | "minArgs": 1,
|
243 | "maxArgs": 1
|
244 | }
|
245 | },
|
246 | "devtools": {
|
247 | "inspectedWindow": {
|
248 | "eval": {
|
249 | "minArgs": 1,
|
250 | "maxArgs": 2,
|
251 | "singleCallbackArg": false
|
252 | }
|
253 | },
|
254 | "panels": {
|
255 | "create": {
|
256 | "minArgs": 3,
|
257 | "maxArgs": 3,
|
258 | "singleCallbackArg": true
|
259 | },
|
260 | "elements": {
|
261 | "createSidebarPane": {
|
262 | "minArgs": 1,
|
263 | "maxArgs": 1
|
264 | }
|
265 | }
|
266 | }
|
267 | },
|
268 | "downloads": {
|
269 | "cancel": {
|
270 | "minArgs": 1,
|
271 | "maxArgs": 1
|
272 | },
|
273 | "download": {
|
274 | "minArgs": 1,
|
275 | "maxArgs": 1
|
276 | },
|
277 | "erase": {
|
278 | "minArgs": 1,
|
279 | "maxArgs": 1
|
280 | },
|
281 | "getFileIcon": {
|
282 | "minArgs": 1,
|
283 | "maxArgs": 2
|
284 | },
|
285 | "open": {
|
286 | "minArgs": 1,
|
287 | "maxArgs": 1,
|
288 | "fallbackToNoCallback": true
|
289 | },
|
290 | "pause": {
|
291 | "minArgs": 1,
|
292 | "maxArgs": 1
|
293 | },
|
294 | "removeFile": {
|
295 | "minArgs": 1,
|
296 | "maxArgs": 1
|
297 | },
|
298 | "resume": {
|
299 | "minArgs": 1,
|
300 | "maxArgs": 1
|
301 | },
|
302 | "search": {
|
303 | "minArgs": 1,
|
304 | "maxArgs": 1
|
305 | },
|
306 | "show": {
|
307 | "minArgs": 1,
|
308 | "maxArgs": 1,
|
309 | "fallbackToNoCallback": true
|
310 | }
|
311 | },
|
312 | "extension": {
|
313 | "isAllowedFileSchemeAccess": {
|
314 | "minArgs": 0,
|
315 | "maxArgs": 0
|
316 | },
|
317 | "isAllowedIncognitoAccess": {
|
318 | "minArgs": 0,
|
319 | "maxArgs": 0
|
320 | }
|
321 | },
|
322 | "history": {
|
323 | "addUrl": {
|
324 | "minArgs": 1,
|
325 | "maxArgs": 1
|
326 | },
|
327 | "deleteAll": {
|
328 | "minArgs": 0,
|
329 | "maxArgs": 0
|
330 | },
|
331 | "deleteRange": {
|
332 | "minArgs": 1,
|
333 | "maxArgs": 1
|
334 | },
|
335 | "deleteUrl": {
|
336 | "minArgs": 1,
|
337 | "maxArgs": 1
|
338 | },
|
339 | "getVisits": {
|
340 | "minArgs": 1,
|
341 | "maxArgs": 1
|
342 | },
|
343 | "search": {
|
344 | "minArgs": 1,
|
345 | "maxArgs": 1
|
346 | }
|
347 | },
|
348 | "i18n": {
|
349 | "detectLanguage": {
|
350 | "minArgs": 1,
|
351 | "maxArgs": 1
|
352 | },
|
353 | "getAcceptLanguages": {
|
354 | "minArgs": 0,
|
355 | "maxArgs": 0
|
356 | }
|
357 | },
|
358 | "identity": {
|
359 | "launchWebAuthFlow": {
|
360 | "minArgs": 1,
|
361 | "maxArgs": 1
|
362 | }
|
363 | },
|
364 | "idle": {
|
365 | "queryState": {
|
366 | "minArgs": 1,
|
367 | "maxArgs": 1
|
368 | }
|
369 | },
|
370 | "management": {
|
371 | "get": {
|
372 | "minArgs": 1,
|
373 | "maxArgs": 1
|
374 | },
|
375 | "getAll": {
|
376 | "minArgs": 0,
|
377 | "maxArgs": 0
|
378 | },
|
379 | "getSelf": {
|
380 | "minArgs": 0,
|
381 | "maxArgs": 0
|
382 | },
|
383 | "setEnabled": {
|
384 | "minArgs": 2,
|
385 | "maxArgs": 2
|
386 | },
|
387 | "uninstallSelf": {
|
388 | "minArgs": 0,
|
389 | "maxArgs": 1
|
390 | }
|
391 | },
|
392 | "notifications": {
|
393 | "clear": {
|
394 | "minArgs": 1,
|
395 | "maxArgs": 1
|
396 | },
|
397 | "create": {
|
398 | "minArgs": 1,
|
399 | "maxArgs": 2
|
400 | },
|
401 | "getAll": {
|
402 | "minArgs": 0,
|
403 | "maxArgs": 0
|
404 | },
|
405 | "getPermissionLevel": {
|
406 | "minArgs": 0,
|
407 | "maxArgs": 0
|
408 | },
|
409 | "update": {
|
410 | "minArgs": 2,
|
411 | "maxArgs": 2
|
412 | }
|
413 | },
|
414 | "pageAction": {
|
415 | "getPopup": {
|
416 | "minArgs": 1,
|
417 | "maxArgs": 1
|
418 | },
|
419 | "getTitle": {
|
420 | "minArgs": 1,
|
421 | "maxArgs": 1
|
422 | },
|
423 | "hide": {
|
424 | "minArgs": 1,
|
425 | "maxArgs": 1,
|
426 | "fallbackToNoCallback": true
|
427 | },
|
428 | "setIcon": {
|
429 | "minArgs": 1,
|
430 | "maxArgs": 1
|
431 | },
|
432 | "setPopup": {
|
433 | "minArgs": 1,
|
434 | "maxArgs": 1,
|
435 | "fallbackToNoCallback": true
|
436 | },
|
437 | "setTitle": {
|
438 | "minArgs": 1,
|
439 | "maxArgs": 1,
|
440 | "fallbackToNoCallback": true
|
441 | },
|
442 | "show": {
|
443 | "minArgs": 1,
|
444 | "maxArgs": 1,
|
445 | "fallbackToNoCallback": true
|
446 | }
|
447 | },
|
448 | "permissions": {
|
449 | "contains": {
|
450 | "minArgs": 1,
|
451 | "maxArgs": 1
|
452 | },
|
453 | "getAll": {
|
454 | "minArgs": 0,
|
455 | "maxArgs": 0
|
456 | },
|
457 | "remove": {
|
458 | "minArgs": 1,
|
459 | "maxArgs": 1
|
460 | },
|
461 | "request": {
|
462 | "minArgs": 1,
|
463 | "maxArgs": 1
|
464 | }
|
465 | },
|
466 | "runtime": {
|
467 | "getBackgroundPage": {
|
468 | "minArgs": 0,
|
469 | "maxArgs": 0
|
470 | },
|
471 | "getPlatformInfo": {
|
472 | "minArgs": 0,
|
473 | "maxArgs": 0
|
474 | },
|
475 | "openOptionsPage": {
|
476 | "minArgs": 0,
|
477 | "maxArgs": 0
|
478 | },
|
479 | "requestUpdateCheck": {
|
480 | "minArgs": 0,
|
481 | "maxArgs": 0
|
482 | },
|
483 | "sendMessage": {
|
484 | "minArgs": 1,
|
485 | "maxArgs": 3
|
486 | },
|
487 | "sendNativeMessage": {
|
488 | "minArgs": 2,
|
489 | "maxArgs": 2
|
490 | },
|
491 | "setUninstallURL": {
|
492 | "minArgs": 1,
|
493 | "maxArgs": 1
|
494 | }
|
495 | },
|
496 | "sessions": {
|
497 | "getDevices": {
|
498 | "minArgs": 0,
|
499 | "maxArgs": 1
|
500 | },
|
501 | "getRecentlyClosed": {
|
502 | "minArgs": 0,
|
503 | "maxArgs": 1
|
504 | },
|
505 | "restore": {
|
506 | "minArgs": 0,
|
507 | "maxArgs": 1
|
508 | }
|
509 | },
|
510 | "storage": {
|
511 | "local": {
|
512 | "clear": {
|
513 | "minArgs": 0,
|
514 | "maxArgs": 0
|
515 | },
|
516 | "get": {
|
517 | "minArgs": 0,
|
518 | "maxArgs": 1
|
519 | },
|
520 | "getBytesInUse": {
|
521 | "minArgs": 0,
|
522 | "maxArgs": 1
|
523 | },
|
524 | "remove": {
|
525 | "minArgs": 1,
|
526 | "maxArgs": 1
|
527 | },
|
528 | "set": {
|
529 | "minArgs": 1,
|
530 | "maxArgs": 1
|
531 | }
|
532 | },
|
533 | "managed": {
|
534 | "get": {
|
535 | "minArgs": 0,
|
536 | "maxArgs": 1
|
537 | },
|
538 | "getBytesInUse": {
|
539 | "minArgs": 0,
|
540 | "maxArgs": 1
|
541 | }
|
542 | },
|
543 | "sync": {
|
544 | "clear": {
|
545 | "minArgs": 0,
|
546 | "maxArgs": 0
|
547 | },
|
548 | "get": {
|
549 | "minArgs": 0,
|
550 | "maxArgs": 1
|
551 | },
|
552 | "getBytesInUse": {
|
553 | "minArgs": 0,
|
554 | "maxArgs": 1
|
555 | },
|
556 | "remove": {
|
557 | "minArgs": 1,
|
558 | "maxArgs": 1
|
559 | },
|
560 | "set": {
|
561 | "minArgs": 1,
|
562 | "maxArgs": 1
|
563 | }
|
564 | }
|
565 | },
|
566 | "tabs": {
|
567 | "captureVisibleTab": {
|
568 | "minArgs": 0,
|
569 | "maxArgs": 2
|
570 | },
|
571 | "create": {
|
572 | "minArgs": 1,
|
573 | "maxArgs": 1
|
574 | },
|
575 | "detectLanguage": {
|
576 | "minArgs": 0,
|
577 | "maxArgs": 1
|
578 | },
|
579 | "discard": {
|
580 | "minArgs": 0,
|
581 | "maxArgs": 1
|
582 | },
|
583 | "duplicate": {
|
584 | "minArgs": 1,
|
585 | "maxArgs": 1
|
586 | },
|
587 | "executeScript": {
|
588 | "minArgs": 1,
|
589 | "maxArgs": 2
|
590 | },
|
591 | "get": {
|
592 | "minArgs": 1,
|
593 | "maxArgs": 1
|
594 | },
|
595 | "getCurrent": {
|
596 | "minArgs": 0,
|
597 | "maxArgs": 0
|
598 | },
|
599 | "getZoom": {
|
600 | "minArgs": 0,
|
601 | "maxArgs": 1
|
602 | },
|
603 | "getZoomSettings": {
|
604 | "minArgs": 0,
|
605 | "maxArgs": 1
|
606 | },
|
607 | "goBack": {
|
608 | "minArgs": 0,
|
609 | "maxArgs": 1
|
610 | },
|
611 | "goForward": {
|
612 | "minArgs": 0,
|
613 | "maxArgs": 1
|
614 | },
|
615 | "highlight": {
|
616 | "minArgs": 1,
|
617 | "maxArgs": 1
|
618 | },
|
619 | "insertCSS": {
|
620 | "minArgs": 1,
|
621 | "maxArgs": 2
|
622 | },
|
623 | "move": {
|
624 | "minArgs": 2,
|
625 | "maxArgs": 2
|
626 | },
|
627 | "query": {
|
628 | "minArgs": 1,
|
629 | "maxArgs": 1
|
630 | },
|
631 | "reload": {
|
632 | "minArgs": 0,
|
633 | "maxArgs": 2
|
634 | },
|
635 | "remove": {
|
636 | "minArgs": 1,
|
637 | "maxArgs": 1
|
638 | },
|
639 | "removeCSS": {
|
640 | "minArgs": 1,
|
641 | "maxArgs": 2
|
642 | },
|
643 | "sendMessage": {
|
644 | "minArgs": 2,
|
645 | "maxArgs": 3
|
646 | },
|
647 | "setZoom": {
|
648 | "minArgs": 1,
|
649 | "maxArgs": 2
|
650 | },
|
651 | "setZoomSettings": {
|
652 | "minArgs": 1,
|
653 | "maxArgs": 2
|
654 | },
|
655 | "update": {
|
656 | "minArgs": 1,
|
657 | "maxArgs": 2
|
658 | }
|
659 | },
|
660 | "topSites": {
|
661 | "get": {
|
662 | "minArgs": 0,
|
663 | "maxArgs": 0
|
664 | }
|
665 | },
|
666 | "webNavigation": {
|
667 | "getAllFrames": {
|
668 | "minArgs": 1,
|
669 | "maxArgs": 1
|
670 | },
|
671 | "getFrame": {
|
672 | "minArgs": 1,
|
673 | "maxArgs": 1
|
674 | }
|
675 | },
|
676 | "webRequest": {
|
677 | "handlerBehaviorChanged": {
|
678 | "minArgs": 0,
|
679 | "maxArgs": 0
|
680 | }
|
681 | },
|
682 | "windows": {
|
683 | "create": {
|
684 | "minArgs": 0,
|
685 | "maxArgs": 1
|
686 | },
|
687 | "get": {
|
688 | "minArgs": 1,
|
689 | "maxArgs": 2
|
690 | },
|
691 | "getAll": {
|
692 | "minArgs": 0,
|
693 | "maxArgs": 1
|
694 | },
|
695 | "getCurrent": {
|
696 | "minArgs": 0,
|
697 | "maxArgs": 1
|
698 | },
|
699 | "getLastFocused": {
|
700 | "minArgs": 0,
|
701 | "maxArgs": 1
|
702 | },
|
703 | "remove": {
|
704 | "minArgs": 1,
|
705 | "maxArgs": 1
|
706 | },
|
707 | "update": {
|
708 | "minArgs": 2,
|
709 | "maxArgs": 2
|
710 | }
|
711 | }
|
712 | };
|
713 |
|
714 | if (Object.keys(apiMetadata).length === 0) {
|
715 | throw new Error("api-metadata.json has not been included in browser-polyfill");
|
716 | }
|
717 | /**
|
718 | * A WeakMap subclass which creates and stores a value for any key which does
|
719 | * not exist when accessed, but behaves exactly as an ordinary WeakMap
|
720 | * otherwise.
|
721 | *
|
722 | * @param {function} createItem
|
723 | * A function which will be called in order to create the value for any
|
724 | * key which does not exist, the first time it is accessed. The
|
725 | * function receives, as its only argument, the key being created.
|
726 | */
|
727 |
|
728 |
|
729 | class DefaultWeakMap extends WeakMap {
|
730 | constructor(createItem, items = undefined) {
|
731 | super(items);
|
732 | this.createItem = createItem;
|
733 | }
|
734 |
|
735 | get(key) {
|
736 | if (!this.has(key)) {
|
737 | this.set(key, this.createItem(key));
|
738 | }
|
739 |
|
740 | return super.get(key);
|
741 | }
|
742 |
|
743 | }
|
744 | /**
|
745 | * Returns true if the given object is an object with a `then` method, and can
|
746 | * therefore be assumed to behave as a Promise.
|
747 | *
|
748 | * @param {*} value The value to test.
|
749 | * @returns {boolean} True if the value is thenable.
|
750 | */
|
751 |
|
752 |
|
753 | const isThenable = value => {
|
754 | return value && typeof value === "object" && typeof value.then === "function";
|
755 | };
|
756 | /**
|
757 | * Creates and returns a function which, when called, will resolve or reject
|
758 | * the given promise based on how it is called:
|
759 | *
|
760 | * - If, when called, `chrome.runtime.lastError` contains a non-null object,
|
761 | * the promise is rejected with that value.
|
762 | * - If the function is called with exactly one argument, the promise is
|
763 | * resolved to that value.
|
764 | * - Otherwise, the promise is resolved to an array containing all of the
|
765 | * function's arguments.
|
766 | *
|
767 | * @param {object} promise
|
768 | * An object containing the resolution and rejection functions of a
|
769 | * promise.
|
770 | * @param {function} promise.resolve
|
771 | * The promise's resolution function.
|
772 | * @param {function} promise.reject
|
773 | * The promise's rejection function.
|
774 | * @param {object} metadata
|
775 | * Metadata about the wrapped method which has created the callback.
|
776 | * @param {boolean} metadata.singleCallbackArg
|
777 | * Whether or not the promise is resolved with only the first
|
778 | * argument of the callback, alternatively an array of all the
|
779 | * callback arguments is resolved. By default, if the callback
|
780 | * function is invoked with only a single argument, that will be
|
781 | * resolved to the promise, while all arguments will be resolved as
|
782 | * an array if multiple are given.
|
783 | *
|
784 | * @returns {function}
|
785 | * The generated callback function.
|
786 | */
|
787 |
|
788 |
|
789 | const makeCallback = (promise, metadata) => {
|
790 | return (...callbackArgs) => {
|
791 | if (extensionAPIs.runtime.lastError) {
|
792 | promise.reject(new Error(extensionAPIs.runtime.lastError.message));
|
793 | } else if (metadata.singleCallbackArg || callbackArgs.length <= 1 && metadata.singleCallbackArg !== false) {
|
794 | promise.resolve(callbackArgs[0]);
|
795 | } else {
|
796 | promise.resolve(callbackArgs);
|
797 | }
|
798 | };
|
799 | };
|
800 |
|
801 | const pluralizeArguments = numArgs => numArgs == 1 ? "argument" : "arguments";
|
802 | /**
|
803 | * Creates a wrapper function for a method with the given name and metadata.
|
804 | *
|
805 | * @param {string} name
|
806 | * The name of the method which is being wrapped.
|
807 | * @param {object} metadata
|
808 | * Metadata about the method being wrapped.
|
809 | * @param {integer} metadata.minArgs
|
810 | * The minimum number of arguments which must be passed to the
|
811 | * function. If called with fewer than this number of arguments, the
|
812 | * wrapper will raise an exception.
|
813 | * @param {integer} metadata.maxArgs
|
814 | * The maximum number of arguments which may be passed to the
|
815 | * function. If called with more than this number of arguments, the
|
816 | * wrapper will raise an exception.
|
817 | * @param {boolean} metadata.singleCallbackArg
|
818 | * Whether or not the promise is resolved with only the first
|
819 | * argument of the callback, alternatively an array of all the
|
820 | * callback arguments is resolved. By default, if the callback
|
821 | * function is invoked with only a single argument, that will be
|
822 | * resolved to the promise, while all arguments will be resolved as
|
823 | * an array if multiple are given.
|
824 | *
|
825 | * @returns {function(object, ...*)}
|
826 | * The generated wrapper function.
|
827 | */
|
828 |
|
829 |
|
830 | const wrapAsyncFunction = (name, metadata) => {
|
831 | return function asyncFunctionWrapper(target, ...args) {
|
832 | if (args.length < metadata.minArgs) {
|
833 | throw new Error(`Expected at least ${metadata.minArgs} ${pluralizeArguments(metadata.minArgs)} for ${name}(), got ${args.length}`);
|
834 | }
|
835 |
|
836 | if (args.length > metadata.maxArgs) {
|
837 | throw new Error(`Expected at most ${metadata.maxArgs} ${pluralizeArguments(metadata.maxArgs)} for ${name}(), got ${args.length}`);
|
838 | }
|
839 |
|
840 | return new Promise((resolve, reject) => {
|
841 | if (metadata.fallbackToNoCallback) {
|
842 | // This API method has currently no callback on Chrome, but it return a promise on Firefox,
|
843 | // and so the polyfill will try to call it with a callback first, and it will fallback
|
844 | // to not passing the callback if the first call fails.
|
845 | try {
|
846 | target[name](...args, makeCallback({
|
847 | resolve,
|
848 | reject
|
849 | }, metadata));
|
850 | } catch (cbError) {
|
851 | console.warn(`${name} API method doesn't seem to support the callback parameter, ` + "falling back to call it without a callback: ", cbError);
|
852 | target[name](...args); // Update the API method metadata, so that the next API calls will not try to
|
853 | // use the unsupported callback anymore.
|
854 |
|
855 | metadata.fallbackToNoCallback = false;
|
856 | metadata.noCallback = true;
|
857 | resolve();
|
858 | }
|
859 | } else if (metadata.noCallback) {
|
860 | target[name](...args);
|
861 | resolve();
|
862 | } else {
|
863 | target[name](...args, makeCallback({
|
864 | resolve,
|
865 | reject
|
866 | }, metadata));
|
867 | }
|
868 | });
|
869 | };
|
870 | };
|
871 | /**
|
872 | * Wraps an existing method of the target object, so that calls to it are
|
873 | * intercepted by the given wrapper function. The wrapper function receives,
|
874 | * as its first argument, the original `target` object, followed by each of
|
875 | * the arguments passed to the original method.
|
876 | *
|
877 | * @param {object} target
|
878 | * The original target object that the wrapped method belongs to.
|
879 | * @param {function} method
|
880 | * The method being wrapped. This is used as the target of the Proxy
|
881 | * object which is created to wrap the method.
|
882 | * @param {function} wrapper
|
883 | * The wrapper function which is called in place of a direct invocation
|
884 | * of the wrapped method.
|
885 | *
|
886 | * @returns {Proxy<function>}
|
887 | * A Proxy object for the given method, which invokes the given wrapper
|
888 | * method in its place.
|
889 | */
|
890 |
|
891 |
|
892 | const wrapMethod = (target, method, wrapper) => {
|
893 | return new Proxy(method, {
|
894 | apply(targetMethod, thisObj, args) {
|
895 | return wrapper.call(thisObj, target, ...args);
|
896 | }
|
897 |
|
898 | });
|
899 | };
|
900 |
|
901 | let hasOwnProperty = Function.call.bind(Object.prototype.hasOwnProperty);
|
902 | /**
|
903 | * Wraps an object in a Proxy which intercepts and wraps certain methods
|
904 | * based on the given `wrappers` and `metadata` objects.
|
905 | *
|
906 | * @param {object} target
|
907 | * The target object to wrap.
|
908 | *
|
909 | * @param {object} [wrappers = {}]
|
910 | * An object tree containing wrapper functions for special cases. Any
|
911 | * function present in this object tree is called in place of the
|
912 | * method in the same location in the `target` object tree. These
|
913 | * wrapper methods are invoked as described in {@see wrapMethod}.
|
914 | *
|
915 | * @param {object} [metadata = {}]
|
916 | * An object tree containing metadata used to automatically generate
|
917 | * Promise-based wrapper functions for asynchronous. Any function in
|
918 | * the `target` object tree which has a corresponding metadata object
|
919 | * in the same location in the `metadata` tree is replaced with an
|
920 | * automatically-generated wrapper function, as described in
|
921 | * {@see wrapAsyncFunction}
|
922 | *
|
923 | * @returns {Proxy<object>}
|
924 | */
|
925 |
|
926 | const wrapObject = (target, wrappers = {}, metadata = {}) => {
|
927 | let cache = Object.create(null);
|
928 | let handlers = {
|
929 | has(proxyTarget, prop) {
|
930 | return prop in target || prop in cache;
|
931 | },
|
932 |
|
933 | get(proxyTarget, prop, receiver) {
|
934 | if (prop in cache) {
|
935 | return cache[prop];
|
936 | }
|
937 |
|
938 | if (!(prop in target)) {
|
939 | return undefined;
|
940 | }
|
941 |
|
942 | let value = target[prop];
|
943 |
|
944 | if (typeof value === "function") {
|
945 | // This is a method on the underlying object. Check if we need to do
|
946 | // any wrapping.
|
947 | if (typeof wrappers[prop] === "function") {
|
948 | // We have a special-case wrapper for this method.
|
949 | value = wrapMethod(target, target[prop], wrappers[prop]);
|
950 | } else if (hasOwnProperty(metadata, prop)) {
|
951 | // This is an async method that we have metadata for. Create a
|
952 | // Promise wrapper for it.
|
953 | let wrapper = wrapAsyncFunction(prop, metadata[prop]);
|
954 | value = wrapMethod(target, target[prop], wrapper);
|
955 | } else {
|
956 | // This is a method that we don't know or care about. Return the
|
957 | // original method, bound to the underlying object.
|
958 | value = value.bind(target);
|
959 | }
|
960 | } else if (typeof value === "object" && value !== null && (hasOwnProperty(wrappers, prop) || hasOwnProperty(metadata, prop))) {
|
961 | // This is an object that we need to do some wrapping for the children
|
962 | // of. Create a sub-object wrapper for it with the appropriate child
|
963 | // metadata.
|
964 | value = wrapObject(value, wrappers[prop], metadata[prop]);
|
965 | } else if (hasOwnProperty(metadata, "*")) {
|
966 | // Wrap all properties in * namespace.
|
967 | value = wrapObject(value, wrappers[prop], metadata["*"]);
|
968 | } else {
|
969 | // We don't need to do any wrapping for this property,
|
970 | // so just forward all access to the underlying object.
|
971 | Object.defineProperty(cache, prop, {
|
972 | configurable: true,
|
973 | enumerable: true,
|
974 |
|
975 | get() {
|
976 | return target[prop];
|
977 | },
|
978 |
|
979 | set(value) {
|
980 | target[prop] = value;
|
981 | }
|
982 |
|
983 | });
|
984 | return value;
|
985 | }
|
986 |
|
987 | cache[prop] = value;
|
988 | return value;
|
989 | },
|
990 |
|
991 | set(proxyTarget, prop, value, receiver) {
|
992 | if (prop in cache) {
|
993 | cache[prop] = value;
|
994 | } else {
|
995 | target[prop] = value;
|
996 | }
|
997 |
|
998 | return true;
|
999 | },
|
1000 |
|
1001 | defineProperty(proxyTarget, prop, desc) {
|
1002 | return Reflect.defineProperty(cache, prop, desc);
|
1003 | },
|
1004 |
|
1005 | deleteProperty(proxyTarget, prop) {
|
1006 | return Reflect.deleteProperty(cache, prop);
|
1007 | }
|
1008 |
|
1009 | }; // Per contract of the Proxy API, the "get" proxy handler must return the
|
1010 | // original value of the target if that value is declared read-only and
|
1011 | // non-configurable. For this reason, we create an object with the
|
1012 | // prototype set to `target` instead of using `target` directly.
|
1013 | // Otherwise we cannot return a custom object for APIs that
|
1014 | // are declared read-only and non-configurable, such as `chrome.devtools`.
|
1015 | //
|
1016 | // The proxy handlers themselves will still use the original `target`
|
1017 | // instead of the `proxyTarget`, so that the methods and properties are
|
1018 | // dereferenced via the original targets.
|
1019 |
|
1020 | let proxyTarget = Object.create(target);
|
1021 | return new Proxy(proxyTarget, handlers);
|
1022 | };
|
1023 | /**
|
1024 | * Creates a set of wrapper functions for an event object, which handles
|
1025 | * wrapping of listener functions that those messages are passed.
|
1026 | *
|
1027 | * A single wrapper is created for each listener function, and stored in a
|
1028 | * map. Subsequent calls to `addListener`, `hasListener`, or `removeListener`
|
1029 | * retrieve the original wrapper, so that attempts to remove a
|
1030 | * previously-added listener work as expected.
|
1031 | *
|
1032 | * @param {DefaultWeakMap<function, function>} wrapperMap
|
1033 | * A DefaultWeakMap object which will create the appropriate wrapper
|
1034 | * for a given listener function when one does not exist, and retrieve
|
1035 | * an existing one when it does.
|
1036 | *
|
1037 | * @returns {object}
|
1038 | */
|
1039 |
|
1040 |
|
1041 | const wrapEvent = wrapperMap => ({
|
1042 | addListener(target, listener, ...args) {
|
1043 | target.addListener(wrapperMap.get(listener), ...args);
|
1044 | },
|
1045 |
|
1046 | hasListener(target, listener) {
|
1047 | return target.hasListener(wrapperMap.get(listener));
|
1048 | },
|
1049 |
|
1050 | removeListener(target, listener) {
|
1051 | target.removeListener(wrapperMap.get(listener));
|
1052 | }
|
1053 |
|
1054 | });
|
1055 |
|
1056 | const onRequestFinishedWrappers = new DefaultWeakMap(listener => {
|
1057 | if (typeof listener !== "function") {
|
1058 | return listener;
|
1059 | }
|
1060 | /**
|
1061 | * Wraps an onRequestFinished listener function so that it will return a
|
1062 | * `getContent()` property which returns a `Promise` rather than using a
|
1063 | * callback API.
|
1064 | *
|
1065 | * @param {object} req
|
1066 | * The HAR entry object representing the network request.
|
1067 | */
|
1068 |
|
1069 |
|
1070 | return function onRequestFinished(req) {
|
1071 | const wrappedReq = wrapObject(req, {}
|
1072 | /* wrappers */
|
1073 | , {
|
1074 | getContent: {
|
1075 | minArgs: 0,
|
1076 | maxArgs: 0
|
1077 | }
|
1078 | });
|
1079 | listener(wrappedReq);
|
1080 | };
|
1081 | }); // Keep track if the deprecation warning has been logged at least once.
|
1082 |
|
1083 | let loggedSendResponseDeprecationWarning = false;
|
1084 | const onMessageWrappers = new DefaultWeakMap(listener => {
|
1085 | if (typeof listener !== "function") {
|
1086 | return listener;
|
1087 | }
|
1088 | /**
|
1089 | * Wraps a message listener function so that it may send responses based on
|
1090 | * its return value, rather than by returning a sentinel value and calling a
|
1091 | * callback. If the listener function returns a Promise, the response is
|
1092 | * sent when the promise either resolves or rejects.
|
1093 | *
|
1094 | * @param {*} message
|
1095 | * The message sent by the other end of the channel.
|
1096 | * @param {object} sender
|
1097 | * Details about the sender of the message.
|
1098 | * @param {function(*)} sendResponse
|
1099 | * A callback which, when called with an arbitrary argument, sends
|
1100 | * that value as a response.
|
1101 | * @returns {boolean}
|
1102 | * True if the wrapped listener returned a Promise, which will later
|
1103 | * yield a response. False otherwise.
|
1104 | */
|
1105 |
|
1106 |
|
1107 | return function onMessage(message, sender, sendResponse) {
|
1108 | let didCallSendResponse = false;
|
1109 | let wrappedSendResponse;
|
1110 | let sendResponsePromise = new Promise(resolve => {
|
1111 | wrappedSendResponse = function (response) {
|
1112 | if (!loggedSendResponseDeprecationWarning) {
|
1113 | console.warn(SEND_RESPONSE_DEPRECATION_WARNING, new Error().stack);
|
1114 | loggedSendResponseDeprecationWarning = true;
|
1115 | }
|
1116 |
|
1117 | didCallSendResponse = true;
|
1118 | resolve(response);
|
1119 | };
|
1120 | });
|
1121 | let result;
|
1122 |
|
1123 | try {
|
1124 | result = listener(message, sender, wrappedSendResponse);
|
1125 | } catch (err) {
|
1126 | result = Promise.reject(err);
|
1127 | }
|
1128 |
|
1129 | const isResultThenable = result !== true && isThenable(result); // If the listener didn't returned true or a Promise, or called
|
1130 | // wrappedSendResponse synchronously, we can exit earlier
|
1131 | // because there will be no response sent from this listener.
|
1132 |
|
1133 | if (result !== true && !isResultThenable && !didCallSendResponse) {
|
1134 | return false;
|
1135 | } // A small helper to send the message if the promise resolves
|
1136 | // and an error if the promise rejects (a wrapped sendMessage has
|
1137 | // to translate the message into a resolved promise or a rejected
|
1138 | // promise).
|
1139 |
|
1140 |
|
1141 | const sendPromisedResult = promise => {
|
1142 | promise.then(msg => {
|
1143 | // send the message value.
|
1144 | sendResponse(msg);
|
1145 | }, error => {
|
1146 | // Send a JSON representation of the error if the rejected value
|
1147 | // is an instance of error, or the object itself otherwise.
|
1148 | let message;
|
1149 |
|
1150 | if (error && (error instanceof Error || typeof error.message === "string")) {
|
1151 | message = error.message;
|
1152 | } else {
|
1153 | message = "An unexpected error occurred";
|
1154 | }
|
1155 |
|
1156 | sendResponse({
|
1157 | __mozWebExtensionPolyfillReject__: true,
|
1158 | message
|
1159 | });
|
1160 | }).catch(err => {
|
1161 | // Print an error on the console if unable to send the response.
|
1162 | console.error("Failed to send onMessage rejected reply", err);
|
1163 | });
|
1164 | }; // If the listener returned a Promise, send the resolved value as a
|
1165 | // result, otherwise wait the promise related to the wrappedSendResponse
|
1166 | // callback to resolve and send it as a response.
|
1167 |
|
1168 |
|
1169 | if (isResultThenable) {
|
1170 | sendPromisedResult(result);
|
1171 | } else {
|
1172 | sendPromisedResult(sendResponsePromise);
|
1173 | } // Let Chrome know that the listener is replying.
|
1174 |
|
1175 |
|
1176 | return true;
|
1177 | };
|
1178 | });
|
1179 |
|
1180 | const wrappedSendMessageCallback = ({
|
1181 | reject,
|
1182 | resolve
|
1183 | }, reply) => {
|
1184 | if (extensionAPIs.runtime.lastError) {
|
1185 | // Detect when none of the listeners replied to the sendMessage call and resolve
|
1186 | // the promise to undefined as in Firefox.
|
1187 | // See https://github.com/mozilla/webextension-polyfill/issues/130
|
1188 | if (extensionAPIs.runtime.lastError.message === CHROME_SEND_MESSAGE_CALLBACK_NO_RESPONSE_MESSAGE) {
|
1189 | resolve();
|
1190 | } else {
|
1191 | reject(new Error(extensionAPIs.runtime.lastError.message));
|
1192 | }
|
1193 | } else if (reply && reply.__mozWebExtensionPolyfillReject__) {
|
1194 | // Convert back the JSON representation of the error into
|
1195 | // an Error instance.
|
1196 | reject(new Error(reply.message));
|
1197 | } else {
|
1198 | resolve(reply);
|
1199 | }
|
1200 | };
|
1201 |
|
1202 | const wrappedSendMessage = (name, metadata, apiNamespaceObj, ...args) => {
|
1203 | if (args.length < metadata.minArgs) {
|
1204 | throw new Error(`Expected at least ${metadata.minArgs} ${pluralizeArguments(metadata.minArgs)} for ${name}(), got ${args.length}`);
|
1205 | }
|
1206 |
|
1207 | if (args.length > metadata.maxArgs) {
|
1208 | throw new Error(`Expected at most ${metadata.maxArgs} ${pluralizeArguments(metadata.maxArgs)} for ${name}(), got ${args.length}`);
|
1209 | }
|
1210 |
|
1211 | return new Promise((resolve, reject) => {
|
1212 | const wrappedCb = wrappedSendMessageCallback.bind(null, {
|
1213 | resolve,
|
1214 | reject
|
1215 | });
|
1216 | args.push(wrappedCb);
|
1217 | apiNamespaceObj.sendMessage(...args);
|
1218 | });
|
1219 | };
|
1220 |
|
1221 | const staticWrappers = {
|
1222 | devtools: {
|
1223 | network: {
|
1224 | onRequestFinished: wrapEvent(onRequestFinishedWrappers)
|
1225 | }
|
1226 | },
|
1227 | runtime: {
|
1228 | onMessage: wrapEvent(onMessageWrappers),
|
1229 | onMessageExternal: wrapEvent(onMessageWrappers),
|
1230 | sendMessage: wrappedSendMessage.bind(null, "sendMessage", {
|
1231 | minArgs: 1,
|
1232 | maxArgs: 3
|
1233 | })
|
1234 | },
|
1235 | tabs: {
|
1236 | sendMessage: wrappedSendMessage.bind(null, "sendMessage", {
|
1237 | minArgs: 2,
|
1238 | maxArgs: 3
|
1239 | })
|
1240 | }
|
1241 | };
|
1242 | const settingMetadata = {
|
1243 | clear: {
|
1244 | minArgs: 1,
|
1245 | maxArgs: 1
|
1246 | },
|
1247 | get: {
|
1248 | minArgs: 1,
|
1249 | maxArgs: 1
|
1250 | },
|
1251 | set: {
|
1252 | minArgs: 1,
|
1253 | maxArgs: 1
|
1254 | }
|
1255 | };
|
1256 | apiMetadata.privacy = {
|
1257 | network: {
|
1258 | "*": settingMetadata
|
1259 | },
|
1260 | services: {
|
1261 | "*": settingMetadata
|
1262 | },
|
1263 | websites: {
|
1264 | "*": settingMetadata
|
1265 | }
|
1266 | };
|
1267 | return wrapObject(extensionAPIs, staticWrappers, apiMetadata);
|
1268 | }; // The build process adds a UMD wrapper around this file, which makes the
|
1269 | // `module` variable available.
|
1270 |
|
1271 |
|
1272 | module.exports = wrapAPIs(chrome);
|
1273 | } else {
|
1274 | module.exports = globalThis.browser;
|
1275 | }
|
1276 | });
|
1277 | //# sourceMappingURL=browser-polyfill.js.map
|