function syncDefaultOptions() {
  chrome.storage.sync.getBytesInUse(null, (bytes) => {
    if (bytes === 0) {
      chrome.storage.sync.set({
        lowerRegexLimit: 13,
        upperRegexLimit: 255,
        autoLoad: false,
        autoPlay: false,
      });
    }
  });
}

const loadedTabs = {};
const injected = {};

function load(tabId) {
  chrome.tabs.executeScript(tabId, { file: 'src/bg/bracketless.js' }, (response) => {
    injected[tabId] = response;
    chrome.browserAction.setIcon({ tabId, path: 'icons/play.png' });
    chrome.browserAction.setTitle({ tabId, title: 'Collapse brackets' });
    chrome.tabs.insertCSS(tabId, { file: 'css/toggle.css' });
    chrome.tabs.executeScript(tabId, { file: 'src/bg/toggle_collapse.js' });
  });
}

function doAction(tabId, action) {
  const state = action === 'play' ?
    { message: 'Pause collapsing', collapse: true, icon: 'pause' } :
    { message: 'Collapse brackets', collapse: false, icon: 'play' };

  loadedTabs[tabId] = state.collapse;
  chrome.browserAction.setIcon({ tabId, path: `icons/${state.icon}.png` });
  chrome.browserAction.setTitle({ tabId, title: state.message });
  chrome.tabs.sendMessage(tabId, { collapse: state.collapse });
}

function setUpContextMenus() {
  chrome.contextMenus.create({
    id: 'bracketless',
    title: 'Bracketless action',
  });
}

function listenerAction(tabId) {
  if (!injected[tabId]) {
    load(tabId);
  } else if (!loadedTabs[tabId]) {
    doAction(tabId, 'play');
  } else if (loadedTabs[tabId]) {
    doAction(tabId, 'pause');
  }
}

// When the extension gets installed, set up the context menus
chrome.runtime.onInstalled.addListener(() => {
  syncDefaultOptions();
  setUpContextMenus();
  chrome.browserAction.onClicked.addListener(tab => listenerAction(tab.id));
  chrome.contextMenus.onClicked.addListener((_, tab) => listenerAction(tab.id));
});

// activate play pause update?
// chrome.contextMenus.update();

// optional functionality
function autoAction() {
  chrome.permissions.contains({
    permissions: ['tabs'],
    origins: ['http://*/', 'https://*/'],
  }, (result) => {
    if (result) {
      chrome.storage.sync.get(null, (options) => {
        if (options.autoLoad) {
          chrome.tabs.query({ active: true }, tabs => load(tabs[0].id));
        }
        if (options.autoPlay) {
          chrome.tabs.query({ active: true }, tabs => doAction(tabs[0].id, 'play'));
        }
      });
    }
  });
}

// !BROKEN! preload script if user granted tabs permission from options page
document.addEventListener('DOMContentLoaded', autoAction);

@@ -1952,16 +1952,19 @@
 onality%0A
+// 
 function
@@ -1975,24 +1975,27 @@
 oAction() %7B%0A
+// 
   chrome.per
@@ -2014,16 +2014,19 @@
 tains(%7B%0A
+// 
     perm
@@ -2044,16 +2044,19 @@
 tabs'%5D,%0A
+// 
     orig
@@ -2089,16 +2089,19 @@
 //*/'%5D,%0A
+// 
   %7D, (re
@@ -2107,28 +2107,31 @@
 esult) =%3E %7B%0A
+//
     
+ 
 if (result) 
@@ -2128,30 +2128,33 @@
  (result) %7B%0A
+//
       
+ 
 chrome.stora
@@ -2182,24 +2182,27 @@
 tions) =%3E %7B%0A
+// 
         if (
@@ -2217,24 +2217,26 @@
 autoLoad) %7B%0A
+//
           ch
@@ -2225,32 +2225,33 @@
 ) %7B%0A//          
+ 
 chrome.tabs.quer
@@ -2293,24 +2293,27 @@
 bs%5B0%5D.id));%0A
+// 
         %7D%0A  
@@ -2306,24 +2306,27 @@
 /         %7D%0A
+// 
         if (
@@ -2341,24 +2341,27 @@
 autoPlay) %7B%0A
+// 
           ch
@@ -2433,16 +2433,19 @@
 lay'));%0A
+// 
         
@@ -2446,16 +2446,19 @@
       %7D%0A
+// 
       %7D)
@@ -2459,28 +2459,37 @@
     %7D);%0A
+// 
     %7D%0A
+// 
   %7D);%0A
+// 
 %7D%0A%0A// !B
@@ -2560,16 +2560,19 @@
 ns page%0A
+// 
 document

Disable autoload and autoplay functionality