Dark theme: adapt unified profile iframe

When the CC dark theme is on, the unified profile iframe will now be
shown in a dark theme.

This is accomplished by rewriting its URL so it points to the dark theme
version of the profile (in TW dark mode is natively supported by Google).

Change-Id: I2fe74fc5a111bcdf4caa53b3b55a94394a7e1d81
diff --git a/docs/features.es.md b/docs/features.es.md
index 6f2dfbc..c7a9363 100644
--- a/docs/features.es.md
+++ b/docs/features.es.md
@@ -152,9 +152,6 @@
 
 Nótese que esto solo aplica a a la Consola de la Comunidad.
 
-Esto podría ser útil si usas el modo oscuro, ya que los nuevos perfiles
-unificados no se han adaptado todavía al tema oscuro.
-
 ## Punto indicador
 > **Opciones:** _Muestra si el autor del hilo ha participado en otros hilos_,
 _Muestra el número de preguntas y respuestas escritas por el autor del hilo
diff --git a/docs/features.md b/docs/features.md
index 8fa27ba..a2b2b33 100644
--- a/docs/features.md
+++ b/docs/features.md
@@ -145,9 +145,6 @@
 
 Note that this only applies to the Community Console.
 
-This might be useful if you're using the dark theme, since the new unified
-profiles haven't been yet adapted to the dark theme.
-
 ## Indicator dot
 > **Option names:** _Show whether the OP has participated in other threads_,
 _Show the number of questions and replies written by the OP within the last `n`
diff --git a/src/content_scripts/console_inject.js b/src/content_scripts/console_inject.js
index 2fe30d6..efc18c5 100644
--- a/src/content_scripts/console_inject.js
+++ b/src/content_scripts/console_inject.js
@@ -568,6 +568,30 @@
   },
 };
 
+function isDarkThemeOn() {
+  if (!options.ccdarktheme) return false;
+
+  if (options.ccdarktheme_mode == 'switch')
+    return options.ccdarktheme_switch_status;
+
+  return window.matchMedia &&
+      window.matchMedia('(prefers-color-scheme: dark)').matches;
+}
+
+var unifiedProfilesFix = {
+  checkIframe(iframe) {
+    var srcRegex = /support.*\.google\.com\/profile\//;
+    console.log(srcRegex.test(iframe.src ?? ''));
+    return srcRegex.test(iframe.src ?? '');
+  },
+  fixIframe(iframe) {
+    console.info('[unifiedProfilesFix] Fixing unified profiles iframe');
+    var url = new URL(iframe.src);
+    url.searchParams.set('dark', 1);
+    iframe.src = url.href;
+  },
+};
+
 function injectPreviousPostsLinks(nameElement) {
   var mainCardContent = getNParent(nameElement, 3);
   if (mainCardContent === null) {
@@ -630,6 +654,9 @@
 
   // Thread list (used for the autorefresh feature)
   'ec-thread-list',
+
+  // Unified profile iframe
+  'iframe',
 ];
 
 function handleCandidateNode(node) {
@@ -712,6 +739,12 @@
         node.tagName == 'EC-THREAD-LIST') {
       autoRefresh.setUp();
     }
+
+    // Redirect unified profile iframe to dark version if applicable
+    if (node.tagName == 'IFRAME' && isDarkThemeOn() &&
+        unifiedProfilesFix.checkIframe(node)) {
+      unifiedProfilesFix.fixIframe(node);
+    }
   }
 }