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/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);
+    }
   }
 }