Fix: sometimes the translation window didn't open

This issue occured because since Webpack was introduced, the
|translator_tab| variable wasn't part of the |window| object, but
instead was inside the scope of the Webpack program, and thus the
options page couldn't reset the |translator_tab| variable when the
|uniquetab| option changed.

Fixed: translateselectedtext:14
Change-Id: I2d56fa7e8d16bc8d33d3d5d7dfe6dfec0d668927
diff --git a/src/background.js b/src/background.js
index e72574e..0befddf 100644
--- a/src/background.js
+++ b/src/background.js
@@ -1,7 +1,8 @@
 import {isoLangs} from './common/consts.js';
 import Options from './common/options.js';
 
-var array_elements = [], translator_tab = null, translator_window = null;
+window.contextMenuLangs = [];
+window.translator_tab = null;
 
 function getTranslationUrl(lang, text) {
   var params = new URLSearchParams({
@@ -16,11 +17,11 @@
 function translationClick(info, tab) {
   Options.getOptions()
       .then(options => {
-        var url = getTranslationUrl(
-            array_elements[info.menuItemId]['langCode'], info.selectionText);
-        var settings_tab = {url};
-        if (translator_tab && options.uniqueTab == 'yep') {
-          chrome.tabs.update(translator_tab, settings_tab, tab => {
+        let url = getTranslationUrl(
+            window.contextMenuLangs[info.menuItemId], info.selectionText);
+        let settings_tab = {url};
+        if (window.translator_tab && options.uniqueTab == 'yep') {
+          chrome.tabs.update(window.translator_tab, settings_tab, tab => {
             chrome.tabs.highlight(
                 {
                   windowId: tab.windowId,
@@ -33,33 +34,16 @@
                 });
           });
         } else if (options.uniqueTab == 'popup') {
-          chrome.windows.create(
-              {
-                type: 'popup',
-                url,
-                width: 1000,
-                height: 382,
-              },
-              function(tab) {
-                translator_window = tab.windowId;
-                translator_tab = tab.id;
-                chrome.windows.onRemoved.addListener(function(windowId) {
-                  if (windowId == translator_window) {
-                    translator_window = null;
-                    translator_tab = null;
-                  }
-                });
-              });
+          chrome.windows.create({
+            type: 'popup',
+            url,
+            width: 1000,
+            height: 382,
+          });
         } else {
           chrome.tabs.create(settings_tab, function(tab) {
-            translator_window = tab.windowId;
-            translator_tab = tab.id;
-            chrome.tabs.onRemoved.addListener(function(tabId, removeInfo) {
-              if (tabId == translator_tab) {
-                translator_window = null;
-                translator_tab = null;
-              }
-            });
+            let translator_window = tab.windowId;
+            window.translator_tab = tab.id;
           });
         }
       })
@@ -102,8 +86,7 @@
       'parentId': parentEl,
       'contexts': ['selection']
     });
-    array_elements[id] = new Array();
-    array_elements[id]['langCode'] = language;
+    window.contextMenuLangs[id] = language;
   }
 
   if (!isSingleEntry) {
@@ -173,6 +156,13 @@
   }
 });
 
+chrome.tabs.onRemoved.addListener((tabId, removeInfo) => {
+  if (tabId == window.translator_tab) {
+    translator_window = null;
+    window.translator_tab = null;
+  }
+});
+
 chrome.browserAction.onClicked.addListener(() => {
   chrome.runtime.openOptionsPage();
 });
diff --git a/src/options/elements/options-editor/options-editor.js b/src/options/elements/options-editor/options-editor.js
index b7eeb52..f6500ab 100644
--- a/src/options/elements/options-editor/options-editor.js
+++ b/src/options/elements/options-editor/options-editor.js
@@ -59,9 +59,7 @@
   changeTabOption(value) {
     chrome.storage.sync.set({uniquetab: value}, function() {
       var background = chrome.extension.getBackgroundPage();
-
-      background.translator_tab = false;
-      background.translator_window = false;
+      background.translator_tab = null;
     });
   }
 }