Backed out changeset 48baafc34055 (bug 1789166) for causing mochitests failures....
[gecko.git] / browser / extensions / screenshots / sitehelper.js
blobe25f5100705d49aca1791eed5cdd5dac0d77e47c
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 /* globals catcher, callBackground, content */
6 /** This is a content script added to all screenshots.firefox.com pages, and allows the site to
7     communicate with the add-on */
9 "use strict";
11 this.sitehelper = (function () {
12   catcher.registerHandler(errorObj => {
13     callBackground("reportError", errorObj);
14   });
16   const capabilities = {};
17   function registerListener(name, func) {
18     capabilities[name] = name;
19     document.addEventListener(name, func);
20   }
22   function sendCustomEvent(name, detail) {
23     if (typeof detail === "object") {
24       // Note sending an object can lead to security problems, while a string
25       // is safe to transfer:
26       detail = JSON.stringify(detail);
27     }
28     document.dispatchEvent(new CustomEvent(name, { detail }));
29   }
31   registerListener(
32     "delete-everything",
33     catcher.watchFunction(() => {
34       // FIXME: reset some data in the add-on
35     }, false)
36   );
38   registerListener(
39     "copy-to-clipboard",
40     catcher.watchFunction(event => {
41       catcher.watchPromise(callBackground("copyShotToClipboard", event.detail));
42     })
43   );
45   registerListener(
46     "show-notification",
47     catcher.watchFunction(event => {
48       catcher.watchPromise(callBackground("showNotification", event.detail));
49     })
50   );
52   // Depending on the script loading order, the site might get the addon-present event,
53   // but probably won't - instead the site will ask for that event after it has loaded
54   registerListener(
55     "request-addon-present",
56     catcher.watchFunction(() => {
57       sendCustomEvent("addon-present", capabilities);
58     })
59   );
61   sendCustomEvent("addon-present", capabilities);
62 })();
63 null;