title: Redirect to new URL using onBeforeRequest and chrome.tabs.query (Chrome Extension)
body: <p>Good time.</p>
  
  <p>I have some interesting idea for which have to do changing URL after inputing pushing "enter" by user.</p>
  
  <p>My manifest file: </p>
  
  <pre><code> {
    "name": "The Pirate Bay_2",
    "description": "Redirect The Pirate Bay to a different host",
    "version": "1.0",
    "manifest_version": 2,
  
    "browser_action": {
          "default_title": "Saving Studio generator",
          "default_popup": "popup.html"
  
      },
      "background": {"scripts":["redirect.js"]},
      "content_scripts": [
     {
       "matches": ["http://*/*", "https://*/*"],
       "js": ["background.js"]
     }
     ],
    "permissions": 
    [ 
      "webRequest", 
      "https://www.amazon.com/", 
      "webRequestBlocking",
      "tabs",
      "activeTab"
      ]
  
  }
  </code></pre>
  
  <p>My redirect.js file:</p>
  
  <pre><code>var host = "https://2ch.hk/b/";
  
  chrome.tabs.query({
      'active': true, 'currentWindow': true
      }, function (tabs) {
      var url = tabs[0].url;
      host = host+url;
      console.log(url);
      });
  
  chrome.webRequest.onBeforeRequest.addListener(  
      function(details) {
  
          if (localStorage.check_box == "true"){
              console.log("start_1");
  
           return {redirectUrl: host};
          }
      },
      {
          urls: ["https://www.amazon.com/" ],
          types: ["main_frame", "sub_frame", "stylesheet", "script", "image", "object", "xmlhttprequest", "other"]
      },
  
      ["blocking"]
  );
  </code></pre>
  
  <p>Main is to how to <strong>take entered URL</strong>, change it by some regex pattern, <strong>return resulting URL</strong> to redirect to it. How it's possible to do? How to insert <em>chrome.tabs.query</em> inside <em>onBeforeRequest</em> or this needn't and there is another way?</p>
  
  <p>Big thanks</p>