Improve sort detection

Before, we relied on the startup data object to detect whether the sort
options were compatible with this feature or not, which gave a wrong
veredict if those options were changed without reloading the page.

This change detects those options much more reliably, as it now parses
the last ViewForum request body to search for those settings.

Bug: twpowertools:5, twpowertools:22
Change-Id: Ic00200f51b611197c0158f9497597af47bee9581
diff --git a/src/contentScripts/communityConsole/autoRefresh.js b/src/contentScripts/communityConsole/autoRefresh.js
index 41777ca..4f40fee 100644
--- a/src/contentScripts/communityConsole/autoRefresh.js
+++ b/src/contentScripts/communityConsole/autoRefresh.js
@@ -5,6 +5,7 @@
 
 var authuser = getAuthUser();
 
+const threadListRequestEvent = 'TWPT_ViewForumRequest';
 const intervalMs = 3 * 60 * 1000;  // 3 minutes
 const firstCallDelayMs = 3 * 1000;  // 3 seconds
 
@@ -15,9 +16,13 @@
     this.lastTimestamp = null;
     this.filter = null;
     this.path = null;
+    this.requestId = null;
+    this.requestOrderOptions = null;
     this.snackbar = null;
     this.interval = null;
     this.firstCallTimeout = null;
+
+    this.setUpHandlers();
   }
 
   getStartupData() {
@@ -26,11 +31,14 @@
   }
 
   isOrderedByTimestampDescending() {
-    var startup = this.getStartupData();
+    // This means we didn't intercept the request.
+    if (!this.requestOrderOptions)
+      return false;
+
     // Returns orderOptions.by == TIMESTAMP && orderOptions.desc == true
     return (
-        startup?.[1]?.[1]?.[3]?.[14]?.[1] == 1 &&
-        startup?.[1]?.[1]?.[3]?.[14]?.[2] == true);
+        this.requestOrderOptions?.[1] == 1 &&
+        this.requestOrderOptions?.[2] == true);
   }
 
   getCustomFilter(path) {
@@ -224,8 +232,25 @@
                 'Couldn\'t get last timestamp (while setting up): ', err));
   }
 
+  setUpHandlers() {
+    window.addEventListener(
+        threadListRequestEvent, e => this.handleListRequest(e));
+  }
+
+  handleListRequest(e) {
+    console.debug('autorefresh_list: handling ViewForum request');
+    if (this.requestId === null || e.detail.id > this.requestId) {
+      this.requestId = e.detail.id;
+      this.requestOrderOptions = e.detail.body?.['2']?.['2'];
+    }
+  }
+
   setUp() {
-    if (!this.isOrderedByTimestampDescending()) return;
+    if (!this.isOrderedByTimestampDescending()) {
+      console.debug(
+          'autorefresh_list: refused to start up because the order is not by timestamp descending.');
+      return;
+    }
 
     this.unregister();