summaryrefslogtreecommitdiff
path: root/data/extensions/jid1-KtlZuoiikVfFew@jetpack/node_modules/pathfinder/lib/scriptish/userscript-manager.js
blob: f1eec70e5b7d9ad94d03c7b84554c526d95037da (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
'use strict';

var { Services } = require("../chrome/services");
var obs = require("sdk/deprecated/observer-service");

var sandboxFactory = require("./userscript-sandbox");

var userscripts = [];

// TODO: register obs only when there is a userscript
obs.add("content-document-global-created", docReady);
obs.add("chrome-document-global-created", docReady);

function docReady(safeWin, data) {
  let href = (safeWin.location.href
      || (safeWin.frameElement && safeWin.frameElement.src)) || "";

  safeWin.addEventListener("load", function() {
    userscripts.forEach(function(script) {
      // check that the userscript should be run on this page
      if (!script.matchesURL(href))
        return;

      sandboxFactory.evalInSandbox(
          script._source,
          sandboxFactory.createSandbox(safeWin, script, href),
          script.jsversion);
    });
  }, true);
}

exports.register = function(aScript) {
  unregister(aScript);
  userscripts.push(aScript);
};

var unregister = exports.unregister = function unregister(aScript) {
  for (var i = userscripts.length - 1; ~i; i--) {
    if (userscripts[i] == aScript) {
      userscripts.splice(i, 1);
      break;
    }
  }
};