summaryrefslogtreecommitdiffstats
path: root/browser/extensions/screenshots/background/selectorLoader.js
blob: 4749f73b301f82cefb6f265f5a7520f1da4ae2c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

/* globals browser, catcher, communication, log, main */

"use strict";

// eslint-disable-next-line no-var
var global = this;

this.selectorLoader = (function () {
  const exports = {};

  // These modules are loaded in order, first standardScripts and then selectorScripts
  // The order is important due to dependencies
  const standardScripts = [
    "log.js",
    "catcher.js",
    "assertIsTrusted.js",
    "assertIsBlankDocument.js",
    "blobConverters.js",
    "background/selectorLoader.js",
    "selector/callBackground.js",
    "selector/util.js",
  ];

  const selectorScripts = [
    "clipboard.js",
    "build/selection.js",
    "build/shot.js",
    "randomString.js",
    "domainFromUrl.js",
    "build/inlineSelectionCss.js",
    "selector/documentMetadata.js",
    "selector/ui.js",
    "selector/shooter.js",
    "selector/uicontrol.js",
  ];

  exports.unloadIfLoaded = function (tabId) {
    return browser.tabs
      .executeScript(tabId, {
        code: "this.selectorLoader && this.selectorLoader.unloadModules()",
        runAt: "document_start",
      })
      .then(result => {
        return result && result[0];
      });
  };

  exports.testIfLoaded = function (tabId) {
    if (loadingTabs.has(tabId)) {
      return true;
    }
    return browser.tabs
      .executeScript(tabId, {
        code: "!!this.selectorLoader",
        runAt: "document_start",
      })
      .then(result => {
        return result && result[0];
      });
  };

  const loadingTabs = new Set();

  exports.loadModules = function (tabId) {
    loadingTabs.add(tabId);
    catcher.watchPromise(
      executeModules(tabId, standardScripts.concat(selectorScripts)).then(
        () => {
          loadingTabs.delete(tabId);
        }
      )
    );
  };

  function executeModules(tabId, scripts) {
    let lastPromise = Promise.resolve(null);
    scripts.forEach(file => {
      lastPromise = lastPromise.then(() => {
        return browser.tabs
          .executeScript(tabId, {
            file,
            runAt: "document_start",
          })
          .catch(error => {
            log.error("error in script:", file, error);
            error.scriptName = file;
            throw error;
          });
      });
    });
    return lastPromise.then(
      () => {
        log.debug("finished loading scripts:", scripts.join(" "));
      },
      error => {
        exports.unloadIfLoaded(tabId);
        catcher.unhandled(error);
        throw error;
      }
    );
  }

  exports.unloadModules = function () {
    const watchFunction = catcher.watchFunction;
    const allScripts = standardScripts.concat(selectorScripts);
    const moduleNames = allScripts.map(filename =>
      filename.replace(/^.*\//, "").replace(/\.js$/, "")
    );
    moduleNames.reverse();
    for (const moduleName of moduleNames) {
      const moduleObj = global[moduleName];
      if (moduleObj && moduleObj.unload) {
        try {
          watchFunction(moduleObj.unload)();
        } catch (e) {
          // ignore (watchFunction handles it)
        }
      }
      delete global[moduleName];
    }
    return true;
  };

  exports.toggle = function (tabId) {
    return exports.unloadIfLoaded(tabId).then(wasLoaded => {
      if (!wasLoaded) {
        exports.loadModules(tabId);
      }
      return !wasLoaded;
    });
  };

  return exports;
})();
null;