title: Chrome webRequest API: onBeforeSendHeaders event will no change headers
body: <p>I created an extension, that sends different User-Agent header, if a kill switch is not enabled. Kill switch is a button, that, once clicked, changes 'ks' in chrome.storage.sync value from 0 to 1 and from 1 to 0. In the background.js, in onBeforeSendHeaders listener, I check the chrome storage for 'ks' or kill switch value. If its a 1, then the extensions should change the user agent to default. If its a 0, then user agent gets changed to the custom agent. The thing is - without that check, the code works fine, but you cant stop the listener from changing the user agent. When the check is added, it only sends the default, even tho 'ks' is 0.</p>
  <p><strong>manifest.json</strong></p>
  <pre><code>{
      &quot;manifest_version&quot;: 2,
      &quot;name&quot;: &quot;UserAgent changer&quot;,
      &quot;description&quot;: &quot;changes user agent&quot;,
      &quot;version&quot;: &quot;1.0.1&quot;,
      &quot;permissions&quot;: [&quot;tabs&quot;,&quot;webRequest&quot;,&quot;webRequestBlocking&quot;, &quot;*://*/*&quot;, &quot;storage&quot;],
      &quot;background&quot;:{
          &quot;scripts&quot;:[&quot;background.js&quot;],
          &quot;persistent&quot;: true
      },
      &quot;browser_action&quot;: {
          &quot;default_popup&quot;: &quot;popup.html&quot;
      }
  }
  </code></pre>
  <p><strong>background.js</strong></p>
  <pre><code>chrome.webRequest.onBeforeSendHeaders.addListener(function(details){
      var headers = details.requestHeaders,
      blockingResponse = {};
      for( var i = 0, l = headers.length; i &lt; l; ++i ) {
        if( headers[i].name == 'User-Agent' ) {
          chrome.storage.sync.get('ks', function(result){
              if (result.ks == 0){
                  headers[i].value = 'Custom agent';
              }else{
                  headers.value = &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36&quot;;
              }
          });
          break;
        }
      }
    
      blockingResponse.requestHeaders = headers;
      return blockingResponse;
    },
    {urls: [ &quot;&lt;all_urls&gt;&quot; ]},['requestHeaders','blocking']);
  
  chrome.runtime.onInstalled.addListener(function() {
      // Set kill switch to True by default
      chrome.storage.sync.set({ks: 1});
  });
  </code></pre>
  <p><strong>popup.html</strong></p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang=&quot;en&quot;&gt;
      &lt;head&gt;
          &lt;title&gt;Popup&lt;/title&gt;
          &lt;style&gt;
              button{
                  width: 100px;
              }
          &lt;/style&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;button id=&quot;toggleAgent&quot;&gt;Turn on agent&lt;/button&gt;
          &lt;script src=&quot;popup.js&quot;&gt;&lt;/script&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p><strong>popup.js</strong></p>
  <pre><code>console.log(&quot;loaded&quot;);
  btn = document.getElementById(&quot;toggleAgent&quot;);
  chrome.storage.sync.get('ks', function(result){
      if (result.ks == 1){
          btn.innerText = &quot;Turn on agent&quot;;
      }else{
          btn.innerText = &quot;Turn off agent&quot;;
      }
  });
  btn.onclick = function(){
      chrome.storage.sync.get('ks', function(result){
          console.log(result);
  
          if (result.ks == 1){
              btn.innerText = &quot;Turn off agent&quot;;
              chrome.storage.sync.set({ks: 0});
          }else{
              btn.innerText = &quot;Turn on agent&quot;;
              chrome.storage.sync.set({ks: 1});
          }
      });
  };
  </code></pre>