// Global variables only exist for the life of the page, so they get reset
// each time the page is unloaded.
const attachedTabs = {};
let scriptInjected = false;

// Called when the user clicks on the browser action.
chrome.browserAction.onClicked.addListener((tab) => {
  const tabId = tab.id;
  if (!scriptInjected) {
    chrome.tabs.executeScript(tabId, { file: 'src/bg/injecthtml.js' }, (response) => {
      scriptInjected = response;
      chrome.browserAction.setIcon({ tabId, path: 'icons/continue.png' });
      chrome.browserAction.setTitle({ tabId, title: 'Enable collapsing' });
      chrome.tabs.insertCSS(tabId, { file: 'css/toggle.css' });
      chrome.tabs.executeScript(tabId, { file: 'src/bg/toggle_collapse.js' });
    });
  } else if (!attachedTabs[tabId]) {
    attachedTabs[tabId] = 'collapsed';
    chrome.browserAction.setIcon({ tabId, path: 'icons/pause.png' });
    chrome.browserAction.setTitle({ tabId, title: 'Pause collapsing' });
    chrome.tabs.sendMessage(tabId, { collapse: true });
  } else if (attachedTabs[tabId]) {
    delete attachedTabs[tabId];
    chrome.browserAction.setIcon({ tabId, path: 'icons/continue.png' });
    chrome.browserAction.setTitle({ tabId, title: 'Enable collapsing' });
    chrome.tabs.sendMessage(tabId, { collapse: false });
  }
});

// Global variables only exist for the life of the page, so they get reset
// each time the page is unloaded.
const activeTabs = {};
const injected = {};

// Called when the user clicks on the browser action.
chrome.browserAction.onClicked.addListener((tab) => {
  const tabId = tab.id;
  if (!injected[tabId]) {
    chrome.tabs.executeScript(tabId, { file: 'src/bg/injecthtml.js' }, (response) => {
      injected[tabId] = response;
      chrome.browserAction.setIcon({ tabId, path: 'icons/continue.png' });
      chrome.browserAction.setTitle({ tabId, title: 'Enable collapsing' });
      chrome.tabs.insertCSS(tabId, { file: 'css/toggle.css' });
      chrome.tabs.executeScript(tabId, { file: 'src/bg/toggle_collapse.js' });
    });
  } else if (!activeTabs[tabId]) {
    activeTabs[tabId] = true;
    chrome.browserAction.setIcon({ tabId, path: 'icons/pause.png' });
    chrome.browserAction.setTitle({ tabId, title: 'Pause collapsing' });
    chrome.tabs.sendMessage(tabId, { collapse: true });
  } else if (activeTabs[tabId]) {
    activeTabs[tabId] = false;
    chrome.browserAction.setIcon({ tabId, path: 'icons/continue.png' });
    chrome.browserAction.setTitle({ tabId, title: 'Enable collapsing' });
    chrome.tabs.sendMessage(tabId, { collapse: false });
  }
});

Fix issue of not working for multipe tabs
Fix issue of not working for multipe tabs

Rename for clarity:
scriptInjected -> injected
attachedTabs -> activeTabs

Change injected type for proper work with multiple tabs.
Injected property is still a boolean.
Change activeTabs[tabId] toggling to boolean for clarity.
