import processJavaScriptCode from "../src/compilation/processJavaScriptCode"

var tabsToProcess = [];

chrome.browserAction.onClicked.addListener(function (tab) {
    console.log("clicked on tab", tab)
    tabsToProcess.push(tab.id)


});

var initialHTMLForNextLoadedPage = "";

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab){
  console.log("onUpdated", arguments)
   if (changeInfo.status !== "loading") {
    console.log("not loadign ")
    return
  }
   if (tabsToProcess.indexOf(tabId) === -1){
      console.log("not injecting", info.tabId, tabsToProcess)
      return
    }

  console.log("running")
  chrome.tabs.insertCSS(tab.id, {
    file: "fromjs.css"
  })

  chrome.tabs.executeScript(tab.id, {
    code: `
      var script = document.createElement("script");
      var initialPageHtml = \`${initialHTMLForNextLoadedPage}\`.replace(/\`/g, "\\\`")
      script.innerHTML = "window.fromJSInitialPageHtml = \`" + initialPageHtml + "\`"
      document.documentElement.appendChild(script)
    `,
    runAt: "document_start"
  }, function(){
    console.log("ran fromJSinitialPageHtml", arguments)
  })

  chrome.tabs.executeScript(tab.id, {
        "file": "contentScript.js",
        runAt: "document_start"
    }, function () {
        console.log("Script Executed .. ");
    });
})

var sourceMaps = {}
chrome.webRequest.onBeforeRequest.addListener(
  function(info){
      console.log("Intercepted: " + info.url, info);

      if (tabsToProcess.indexOf(info.tabId) === -1){
        console.log("skiipping", info.tabId, tabsToProcess)
        return
      }
      if (info.url.slice(0, "chrome-extension://".length ) === "chrome-extension://") {
        return
      }
      if (info.url.slice(info.url.length - ".js.map".length) === ".js.map") {
        return {
          redirectUrl: "data:," + encodeURI(sourceMaps[info.url])
        }
      }
      if (info.type === "main_frame") {
        var xhr = new XMLHttpRequest()
        xhr.open('GET', info.url, false);
        xhr.send(null);
        initialHTMLForNextLoadedPage = xhr.responseText

      }
      if (info.url.slice(info.url.length - ".js".length) === ".js") {
          var xhr = new XMLHttpRequest()
          xhr.open('GET', info.url, false);
          xhr.send(null);

        //   var code = "console.log('sth')"
        var res = processJavaScriptCode(xhr.responseText, {filename: info.url})
        var code = res.code
        code += "\n//# sourceURL=" + info.url
        code += "\n//# sourceMappingURL=" + info.url + ".map"
        sourceMaps[info.url + ".map"] = JSON.stringify(res.map)
        var url = "data:," + encodeURI(code)
        return {redirectUrl: url}
      }
  }, {urls: ["<all_urls>"]}, ["blocking"]);

@@ -96,16 +96,98 @@
  = %5B%5D;%0A%0A
+function isEnabledInTab(tabId)%7B%0A    return tabsToProcess.indexOf(tabId) !== -1%0A%7D%0A%0A
 chrome.b
@@ -285,35 +285,355 @@
     
-tabsToProcess.push(tab.id)%0A
+if (isEnabledInTab(tab.id)) %7B%0A      tabsToProcess = tabsToProcess.filter(function(tabId)%7B%0A          return tabId !== tab.id%0A      %7D)%0A    %7D else %7B%0A      tabsToProcess.push(tab.id)%0A    %7D%0A%0A    var text = %22%22%0A    if (isEnabledInTab(tab.id)) %7B%0A      text = %22ON%22%0A    %7D%0A    chrome.browserAction.setBadgeText(%7B%0A      text: text,%0A      tabId: tab.id%0A    %7D);
 %0A%0A%7D)
@@ -881,43 +881,30 @@
 if (
-tabsToProcess.indexOf(tabId) === -1
+!isEnabledInTab(tabId)
 )%7B%0A 
@@ -933,29 +933,24 @@
 injecting%22, 
-info.
 tabId, tabsT

Allow toggle fromJS using browser action button.