summaryrefslogtreecommitdiff
path: root/src/bg/RequestUtil.js
blob: 873f15318d6c7e89a1c0c453602f248898879276 (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
'use strict';
{
  let NULL = new Uint8Array();
  let xmlFeedOrImage = /^(?:(?:application|text)\/(?:(?:r(?:ss|df)|atom)\+)?xml(;|$))|image\//i;
  let brokenOnLoad = (async () => parseInt(await browser.runtime.getBrowserInfo().version) < 61);
  let pendingRequests = new Map();

  let cleanup = r => {
    pendingRequests.delete(r.requestId);
  };
  let filter = {
    urls: ["<all_urls>"],
    types:  ["main_frame", "sub_frame", "object"]
  };
  browser.webRequest.onCompleted.addListener(cleanup, filter);
  browser.webRequest.onErrorOccurred.addListener(cleanup, filter);

  let executeAll = async (scripts, where) => {
    let {url, tabId, frameId} = where;
    for (let details of scripts.values()) {
      details = Object.assign({
        runAt: "document_start",
        matchAboutBlank: true,
        frameId,
      }, details);
      try {
        await browser.tabs.executeScript(tabId, details);
        debug("Execute on start OK", url, details);
      } catch (e) {
        error(e, "Execute on start failed", url, details);
      }
    }
  };

  var RequestUtil = {

    getContentMetaData(request) {
      if (request.content) return request.content;
      let {responseHeaders} = request;
      let content = request.content = {};
      for (let h of responseHeaders) {
        if (/^\s*Content-(Type|Disposition)\s*$/i.test(h.name)) {
          content[h.name.split("-")[1].trim().toLowerCase()] = h.value;
        }
      }
      return content;
    },

    async executeOnStart(request, details) {
      let {requestId, tabId, frameId, statusCode} = request;
      if (statusCode >= 300 && statusCode < 400) return;
      let scripts = pendingRequests.get(requestId);
      let scriptKey = JSON.stringify(details);
      if (!scripts) {
        pendingRequests.set(requestId, scripts = new Map());
        scripts.set(scriptKey, details);
      } else {
        scripts.set(scriptKey, details);
        return;
      }

      let content = this.getContentMetaData(request);
      debug(request.url, content.type);
      if (xmlFeedOrImage.test(content.type) && !/\/svg\b/i.test(content.type)) return;
      let filter = browser.webRequest.filterResponseData(requestId);
      let buffer = [];

      let first = true;
      let runAndFlush = async () => {
        await executeAll(scripts, request);
        if (buffer.length) {
          debug("Flushing %s buffer chunks", buffer.length);
          for (let chunk of buffer) {
            filter.write(chunk);
          }
          filter.disconnect();
          buffer = null;
        }
      };

      if (brokenOnLoad) {
        filter.onstart = event => {
          filter.write(NULL);
        }
      }

      filter.ondata =  event => {
        if (first) {
          runAndFlush();
          first = false;
        }
        if (buffer) {
          buffer.push(event.data);
          return;
        }
        filter.write(event.data);
        filter.disconnect();
      };

    }
  }
}