summaryrefslogtreecommitdiff
path: root/src/content/media.js
blob: ead6e05543f7ecf00e9e16acf8d3b3b22719d55f (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
if (correctFrame()) {
  debug("Media Hook (blocked %s)", !!window.mediaBlocker, document.URL, document.documentElement && document.documentElement.innerHTML);
  (() => {
    let unpatched = new Map();
    function patch(obj, methodName, replacement) {
       let methods = unpatched.get(obj) || {};
       methods[methodName] = obj[methodName];
       exportFunction(replacement, obj, {defineAs: methodName});
       unpatched.set(obj, methods);
    }
    let urlMap = new WeakMap();
    patch(window.URL, "createObjectURL",  function(o, ...args) {
      let url = unpatched.get(window.URL).createObjectURL.call(this, o, ...args);
      if (o instanceof MediaSource) {
        let urls = urlMap.get(o);
        if (!urls) urlMap.set(o, urls = new Set());
        urls.add(url);
      }
      return url;
    });

    patch(window.MediaSource.prototype, "addSourceBuffer", function(mime, ...args) {
      let ms = this;
      let urls = urlMap.get(ms);

      let request = {
        id: "noscript-media",
        type: "media",
        url: document.URL,
        documentUrl: document.URL,
        embeddingDocument: true,
      };
      seen.record({policyType: "media", request, allowed: false});
      notifyPage();

      if (window.mediaBlocker) {
        (async () => {
          let me = Array.from(document.querySelectorAll("video,audio"))
            .find(e => e.srcObject === ms || urls && urls.has(e.src));

          if (!me) return;
          let exposedMime = `${mime} (MSE)`;

          try {
            let ph = PlaceHolder.create("media", request);
            ph.replace(me);
            PlaceHolder.listen();
          } catch (e) {
            error(e);
          }
        })();
        throw new Error(`${exposedMime} blocked by NoScript`);
      }

      return unpatched.get(window.MediaSource.prototype).addSourceBuffer.call(ms, mime, ...args);
    });

  })();
  document.URL;
}