Merge reused code into a common file

Change-Id: I264aaa67ae1103df2292e17d1eb46b3054799994
diff --git a/src/background.js b/src/background.js
index 6728e07..688aaa1 100644
--- a/src/background.js
+++ b/src/background.js
@@ -1,45 +1,9 @@
-function isEmpty(obj) {
-  return Object.keys(obj).length === 0;
-}
-
-const defaultOptions = {
-  'list': true,
-  'thread': true,
-  'threadall': false,
-  'fixedtoolbar': false,
-  'redirect': false,
-  'history': false,
-  'loaddrafts': false,
-  'batchduplicate': false,
-  'escalatethreads': false,
-  'movethreads': false,
-  'increasecontrast': false,
-  'stickysidebarheaders': false,
-  'profileindicator': false,
-};
-
-function cleanUpOptions() {
-  chrome.storage.sync.get(null, function(options) {
-    console.log('[cleanUpOptions] Previous options', options);
-    var ok = true;
-    for (const [opt, value] of Object.entries(defaultOptions)) {
-      if (!(opt in options)) {
-        ok = false;
-        options[opt] = value;
-      }
-    }
-
-    console.log('[cleanUpOptions] New options', options);
-
-    if (!ok) {
-      chrome.storage.sync.set(options);
-    }
-  });
-}
-
+// When the extension gets updated, set new options to their default value.
 chrome.runtime.onInstalled.addListener(function(details) {
   if (details.reason == 'install' || details.reason == 'update') {
-    cleanUpOptions();
+    chrome.storage.sync.get(null, function(options) {
+      cleanUpOptions(options);
+    });
   }
 });
 
diff --git a/src/common/common.js b/src/common/common.js
new file mode 100644
index 0000000..28c233a
--- /dev/null
+++ b/src/common/common.js
@@ -0,0 +1,44 @@
+const defaultOptions = {
+  'list': true,
+  'thread': true,
+  'threadall': false,
+  'fixedtoolbar': false,
+  'redirect': false,
+  'history': false,
+  'loaddrafts': false,
+  'batchduplicate': false,
+  'escalatethreads': false,
+  'movethreads': false,
+  'increasecontrast': false,
+  'stickysidebarheaders': false,
+  'profileindicator': false,
+};
+
+const deprecatedOptions = [
+  'escalatethreads',
+  'movethreads',
+  'batchduplicate',
+];
+
+function isEmpty(obj) {
+  return Object.keys(obj).length === 0;
+}
+
+function cleanUpOptions(options) {
+  console.log('[cleanUpOptions] Previous options', options);
+  var ok = true;
+  for (const [opt, value] of Object.entries(defaultOptions)) {
+    if (!(opt in options)) {
+      ok = false;
+      options[opt] = value;
+    }
+  }
+
+  console.log('[cleanUpOptions] New options', options);
+
+  if (!ok) {
+    chrome.storage.sync.set(options);
+  }
+
+  return options;
+}
diff --git a/src/common/content_scripts_injections.js b/src/common/content_scripts_injections.js
new file mode 100644
index 0000000..2067075
--- /dev/null
+++ b/src/common/content_scripts_injections.js
@@ -0,0 +1,16 @@
+function injectStylesheet(stylesheetName) {
+  var link = document.createElement('link');
+  link.setAttribute('rel', 'stylesheet');
+  link.setAttribute('href', stylesheetName);
+  document.head.appendChild(link);
+}
+
+function injectStyles(css) {
+  injectStylesheet('data:text/css;charset=UTF-8,' + encodeURIComponent(css));
+}
+
+function injectScript(scriptName) {
+  var script = document.createElement('script');
+  script.src = scriptName;
+  document.head.appendChild(script);
+}
diff --git a/src/content_scripts/console_inject.js b/src/content_scripts/console_inject.js
index 8d481e9..eda9960 100644
--- a/src/content_scripts/console_inject.js
+++ b/src/content_scripts/console_inject.js
@@ -62,23 +62,6 @@
   });
 };
 
-function injectStylesheet(stylesheetName) {
-  var link = document.createElement('link');
-  link.setAttribute('rel', 'stylesheet');
-  link.setAttribute('href', stylesheetName);
-  document.head.appendChild(link);
-}
-
-function injectStyles(css) {
-  injectStylesheet('data:text/css;charset=UTF-8,' + encodeURIComponent(css));
-}
-
-function injectScript(scriptName) {
-  var script = document.createElement('script');
-  script.src = scriptName;
-  document.head.appendChild(script);
-}
-
 var observerOptions = {
   childList: true,
   attributes: true,
diff --git a/src/content_scripts/thread_inject.js b/src/content_scripts/thread_inject.js
index 2e2554a..89ef2f6 100644
--- a/src/content_scripts/thread_inject.js
+++ b/src/content_scripts/thread_inject.js
@@ -1,22 +1,5 @@
 var intersectionObserver;
 
-function injectStylesheet(stylesheetName) {
-  var link = document.createElement('link');
-  link.setAttribute('rel', 'stylesheet');
-  link.setAttribute('href', stylesheetName);
-  document.head.appendChild(link);
-}
-
-function injectStyles(css) {
-  injectStylesheet('data:text/css;charset=UTF-8,' + encodeURIComponent(css));
-}
-
-function injectScript(scriptName) {
-  var script = document.createElement('script');
-  script.src = scriptName;
-  document.head.appendChild(script);
-}
-
 function intersectionCallback(entries, observer) {
   entries.forEach(entry => {
     if (entry.isIntersecting) {
diff --git a/src/options.html b/src/options.html
index 6dc2d46..cd325a0 100644
--- a/src/options.html
+++ b/src/options.html
@@ -23,6 +23,7 @@
     </p>
     <p class="actions"><button id="save" data-i18n="save"></button></p>
     <div id="save-indicator"></div>
+    <script src="common/common.js"></script>
     <script src="options.js"></script>
   </body>
 </html>
diff --git a/src/options.js b/src/options.js
index af9dede..64d1bad 100644
--- a/src/options.js
+++ b/src/options.js
@@ -1,47 +1,5 @@
-function isEmpty(obj) {
-  return Object.keys(obj).length === 0;
-}
-
-const defaultOptions = {
-  'list': true,
-  'thread': true,
-  'threadall': false,
-  'fixedtoolbar': false,
-  'redirect': false,
-  'history': false,
-  'loaddrafts': false,
-  'batchduplicate': false,
-  'escalatethreads': false,
-  'movethreads': false,
-  'increasecontrast': false,
-  'stickysidebarheaders': false,
-  'profileindicator': false,
-};
-
-const deprecatedOptions = [
-  'escalatethreads',
-  'movethreads',
-  'batchduplicate',
-];
-
 var savedSuccessfullyTimeout = null;
 
-function cleanUpOptions(options) {
-  var ok = true;
-  for (const [opt, value] of Object.entries(defaultOptions)) {
-    if (!opt in options) {
-      ok = false;
-      options[opt] = value;
-    }
-  }
-
-  if (!ok) {
-    chrome.storage.sync.set(options);
-  }
-
-  return options;
-}
-
 function save() {
   var options = defaultOptions;