function main() {
  var topWindow = window.top;
  var isTop = window.top === window;

  // Check the top window for options.
  var options = null;
  try {
    var optionsEl = topWindow.document.querySelector('x-wtf-options');
    if (optionsEl) {
      // Options exist in the DOM, use those.
      options = JSON.parse(optionsEl.text);
    }
  } catch (e) {
    // Failed, likely because of cross-domain iframe issues.
  }

  // Grab options - if not found, not injected.
  if (!options) {
    options = fetchOptions(topWindow);
  }
  if (!options) {
    return;
  }

  // Add options to the document for other frames/etc to find.
  if (!optionsEl) {
    try {
      optionsEl = document.createElement('x-wtf-options');
      optionsEl.text = JSON.stringify(options);
      topWindow.document.documentElement.appendChild(optionsEl);
    } catch (e) {
      // Failed, likely because of cross-domain iframe issues.
    }
  }

  if (options['__instrumented__']) {
    // Instrumentation mode.
    if (isTop) {
      // Create a blob that is both the falafel and process scripts.
      // This is used to create the worker.
      var xhr = new XMLHttpRequest();
      xhr.open('GET', chrome.extension.getURL('third_party/falafel.js'), false);
      xhr.send();
      var falafelSource = xhr.responseText;
      xhr = new XMLHttpRequest();
      xhr.open('GET', chrome.extension.getURL('wtf-process.js'), false);
      xhr.send();
      var processSource = xhr.responseText;
      var blob = new Blob([
        falafelSource,
        processSource
      ], {
        type: 'application/javascript'
      });
      var blobUrl = URL.createObjectURL(blob);
      options['wtf.instrumentation.blob'] = blobUrl;

      injectScriptFile(chrome.extension.getURL('third_party/falafel.js'));
      injectScriptFile(chrome.extension.getURL('wtf-process.js'));
      injectScriptFile(chrome.extension.getURL('wtf-call-tracing.js'));
    }
    injectScriptFunction(function(options) {
      // If we are nested in an iframe, protect against cross-origin errors.
      try {
        window.top.wtfi.prepare(options, window);
      } catch (e) {
      }
    }, [
      options
    ]);
  } else {
    // Normal tracing mode.
    // Inject the tracing framework script and the prepare function.
    if (isTop) {
      var traceScriptUrl =
        chrome.extension.getURL('wtf_trace_web_js_compiled.js');
      injectScriptFunction(function(traceScriptUrl) {
        window.WTF_TRACE_SCRIPT_URL = traceScriptUrl;
      }, [traceScriptUrl]);
      injectScriptFile(traceScriptUrl);
    }
    injectScriptFunction(function(options) {
      // If we are nested in an iframe, protect against cross-origin errors.
      try {
        window.top.wtf.trace.prepare(options, window);
      } catch (e) {
      }
    }, [
      options
    ]);

    if (isTop) {
      // Inject addons, if required.
      var addons = injectAddons(options['wtf.addons'] || []);
      // Inject preparation code to start tracing with the desired options.
      injectScriptFunction(startTracing, [
        addons
      ]);
    }
  }

  if (isTop) {
    setupCommunications();
  }
}
Injects the extension, if needed.
This is called on startup in the content-script context.