function scanPageScripts(targetWindow, doc) {
  // In Firefox we could use this:
  // https://developer.mozilla.org/en-US/docs/Web/API/element.onbeforescriptexecute
  // doc.addEventListener('beforescriptexecute', function(e) {
  //   console.log('before', e);
  // }, true);

  // It's possible to get in the crazy situation where we actually have access
  // to the <script> *before it's done loading*. In that case, we are kind of
  // hosed.

  var observer = new MutationObserver(function(mutations) {
    mutations.forEach(function(mutation) {
      for (var n = 0; n < mutation.addedNodes.length; n++) {
        var node = mutation.addedNodes[n];
        if (node instanceof HTMLScriptElement && !node['__wtfi__'] &&
            !node.getAttribute('__wtfi__')) {
          var processedScript = processScript(node, true);
          try {
            node.parentNode.replaceChild(processedScript, node);
          } catch (e) {
          }
        }
      }
    });
  });
  var target = doc.documentElement;
  observer.observe(target, {
    childList: true,
    subtree: true
  });

  // Kill the observer when the document loads so that we don't keep listening
  // for all DOM mutations. The other hooks should catch everything else.
  doc.addEventListener('DOMContentLoaded', function() {
    observer.disconnect();
  }, false);
}
Scans the current page DOM for <script> elements and swaps them out.
@param {!Window} targetWindow Window.
@param {!Document} doc Target document object.