summaryrefslogtreecommitdiff
path: root/src/bg/deferWebTraffic.js
blob: fcfb0b9651fcabdab21ff7c4ad0cf0eb103ef31d (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
function deferWebTraffic(promiseToWaitFor, next) {
  debug("deferWebTraffic on %o", promiseToWaitFor);
  let seenTabs = new Set();
  function checkNavigation(nav) {
    if (nav.tabId !== browser.tabs.TAB_ID_NONE && nav.url.startsWith("http")) {
      let seen = seenTabs.has(nav.tabId);
      debug(`%s navigation %o`, seen ? "seen" : "unseen", nav);
      if (!seen) {
        reloadTab(nav.tabId);
      }
    }
  }
  browser.webNavigation.onCommitted.addListener(checkNavigation);
  function reloadTab(tabId) {
    seenTabs.add(tabId);
    try {
      browser.tabs.executeScript(tabId, {
        runAt: "document_start",
        code: "if (performance.now() < 60000) window.location.reload(false)"
      });
      debug("Reloading tab", tabId);
    } catch (e) {
      error(e, "Can't reload tab", tabId);
    }
  }

   async function waitFor(request) {
    let {type, documentUrl, url, tabId, frameId} = request;
    if (tabId === browser.tabs.TAB_ID_NONE) return;
    if (!seenTabs.has(tabId)) {
      if (type === "main_frame") {
        seenTabs.add(tabId);
      } else if (documentUrl) {
        if (frameId !== 0 && request.frameAncestors) {
          documentUrl = request.frameAncestors.pop().url;
        }
        reloadTab(tabId);
      }
    }
    debug("Deferring %s %s from %s", type, url, documentUrl);
    try {
      await promiseToWaitFor;
    } catch (e) {
      error(e);
    }
    debug("Green light to %s %s from %s", type, url, documentUrl);
  }

  function spyTabs(request) {
    debug("Spying request %o", request);
  }

  browser.webRequest.onHeadersReceived.addListener(spyTabs, {
    urls: ["<all_urls>"],
    types: ["main_frame"],
  }, ["blocking", "responseHeaders"]);
  browser.webRequest.onBeforeRequest.addListener(waitFor, {
    urls: ["<all_urls>"]
  }, ["blocking"]);

  (async () => {
    await promiseToWaitFor;
    browser.webNavigation.onCommitted.removeListener(checkNavigation);
    browser.webRequest.onBeforeRequest.removeListener(waitFor);
    browser.webRequest.onHeadersReceived.removeListener(spyTabs);
    if (next) next();
  })();
}