(function () {
  const extensionName = 'SilverDog';
  const extensionStatus = {
    init: 'The extension is enabled.',
    disabled: 'The extension is currently disabled.',
    search: 'Looking for audio elements...',
    audio: 'A new audio element has been found.',
    error: 'Audio element could not be filtered.',
    filtered: 'Audio element has already been filtered.',
    filter: 'Ultrasound audio filter added.'
  };

  let connect = AudioNode.prototype.connect,
    filteredSources = [],
    filters = [];

  /**
   * Fire initial call.
   */
  chrome
    .runtime
    .sendMessage({ sync: 'init' });

  /**
   * Sign up for messaged from the background script.
   */
  chrome
    .runtime
    .onMessage
    .addListener(function (request, sender, sendResponse) {
      if (request.state) {
        if (request.sync === 'init') {
          log(extensionStatus['init']);
        }
        log(extensionStatus['search']);
        searchForAudioContent();
      } else {
        log(extensionStatus['disabled']);
      }
    });

  function log(status) {
    console.log(`${extensionName}: ${status}`);
  }

  function searchForAudioContent() {
    let audioElements = document.getElementsByTagName('audio');
    Array.from(audioElements).forEach(createAndConnectSource, audioElements);
    let videoElements = document.getElementsByTagName('video');
    Array.from(videoElements).forEach(createAndConnectSource, videoElements);
  }

  function createAndConnectSource(element, i) {
    if (!filteredSources.includes(this[i])) {
      let windowContext = new (window.AudioContext || window.webkitAudioContext);

      log(extensionStatus['audio']);
      console.log(this[i]);

      try {
        windowContext
          .createMediaElementSource(this[i])
          .connect(windowContext.destination);
      } catch(err) {
        log(extensionStatus['error']);
      }

      filteredSources.push(this[i]);
    } else {
      log(extensionStatus['filtered']);
    }
  }

  function createFilter(context) {
    let biquadFilter = context.createBiquadFilter();

    let storage = chrome.storage.local;
    storage.get(['type', 'freq', 'q', 'gain'], function (items) {
      biquadFilter.type = items.type || 'highshelf';
      biquadFilter.frequency.value = items.freq || '17999';
      biquadFilter.Q.value = items.q || '0';
      biquadFilter.gain.value = items.gain || '-70';
    });

    return biquadFilter;
  }

  AudioNode.prototype.connect = function () {
    let name = arguments[0].toString();

    if (name.indexOf('AudioDestinationNode') > -1) {
      let filter = createFilter(arguments[0].context);
      filters.push(filter);

      connect.apply(this, [filters[filters.length - 1], arguments[1], arguments[2]]);
      connect.apply(filter, [arguments[0], arguments[1], arguments[2]]);

      log(extensionStatus['filter']);
    } else {	
      connect.apply(this, arguments);
    }
  };
})();

@@ -466,16 +466,136 @@
 onnect,%0A
+    audioElements = document.getElementsByTagName('audio'),%0A    videoElements = document.getElementsByTagName('video'),%0A
     filt
@@ -846,25 +846,57 @@
 istener(
-function 
+handleRequest);%0A%0A  function handleRequest
 (request
@@ -921,26 +921,24 @@
 onse) %7B%0A    
-  
 if (request.
@@ -952,18 +952,16 @@
 %7B%0A      
- 
- 
 if (requ
@@ -987,26 +987,24 @@
 ) %7B%0A        
-  
 log(extensio
@@ -1027,22 +1027,19 @@
 ;%0A      
- 
- 
 %7D%0A
-  
+%0A
       lo
@@ -1078,288 +1078,84 @@
     
-  searchForAudioContent();%0A      %7D else %7B%0A        log(extensionStatus%5B'disabled'%5D);%0A      %7D%0A    %7D);%0A%0A  function log(status) %7B%0A    console.log(%60$%7BextensionName%7D: $%7Bstatus%7D%60);%0A  %7D%0A%0A  function searchForAudioContent() %7B%0A    let audioElements = document.getElementsByTagName('audio'
+Array.from(audioElements).forEach(createAndConnectSource, audioElements
 );%0A
+  
     
@@ -1157,36 +1157,36 @@
      Array.from(
-audi
+vide
 oElements).forEa
@@ -1204,36 +1204,36 @@
 dConnectSource, 
-audi
+vide
 oElements);%0A    
@@ -1232,147 +1232,144 @@
 s);%0A
+%0A
     
-let videoElements = document.getElementsByTagName('video'
+%7D else %7B%0A      log(extensionStatus%5B'disabled'%5D
 );%0A
+%0A
     
-Array.from(videoElements).forEach(createAndConnectSource, videoElements
+%7D%0A  %7D%0A%0A  function log(status) %7B%0A    console.log(%60$%7BextensionName%7D: $%7Bstatus%7D%60
 );%0A 

Make audio/video containers global