Diff of /wvdetection.js [c2b811] .. [1fa7c0]  Maximize  Restore

Switch to side-by-side view

--- a/wvclient.js
+++ b/wvdetection.js
@@ -675,22 +675,107 @@
  * <https://www.gnu.org/licenses/why-not-lgpl.html>.
  */
 
-const consoleLog = window.console.log;
-const newConsole = window.console;
-newConsole.log = function(args) {
-    if (parent !== undefined)
-        parent.postMessage(args, "*");
-    consoleLog(args);
-};
-newConsole.warn = function(args) {
-    if (parent !== undefined)
-        parent.postMessage(args, "*");
-    consoleLog(args);
-};
-newConsole.info = newConsole.log;
-newConsole.error = function(args) {
-    if (parent !== undefined)
-        parent.postMessage(args, "*");
-    consoleLog(args);
-};
-console = newConsole;
+class WVDetection {
+
+    constructor(frameID) {
+        if (frameID === undefined || frameID === null|| typeof frameID  !== "string") {
+            throw Error("The element id of the frame has to be a string!");
+        }
+        this.frame = document.getElementById(frameID);
+        if (this.frame === null|| (!(this.frame instanceof HTMLIFrameElement))) {
+            throw Error("Cannot find the frame");
+        }
+        this.urls = [];
+        this.activeIndex = -1;
+        this.isStarted = false;
+        this.timerID = -1;
+        const self = this;
+        window.addEventListener("message", function(event) {self.onMessageFromFrame(event)}, false);
+    }
+
+    /**
+     * Set the urls to use
+     * @param urls Array of strings
+     */
+    setWebvisuUrls(urls) {
+        if (urls === undefined || urls === null || (!Array.isArray(urls))) {
+            throw Error ("Argument has to be an array of strings!");
+        }
+        if (urls.length < 2) {
+            throw Error("The array must at least contain two entries!");
+        }
+        if (this.isStarted) {
+            throw Error("Cannot change urls while the check is running.");
+        }
+        urls.forEach((element) => {
+            if (!element.startsWith("http://") && !element.startsWith("https://")) {
+                throw Error ("Urls have to start with http:// or https://.");
+            }
+        });
+        this.urls = urls;
+    }
+
+    /**
+     * Start the webvisu detection with the specified wait timeout
+     * @param timeout ms waiting for new messages (default 5000ms).
+     */
+    start(timeout) {
+        if (timeout === undefined || timeout === null) {
+            timeout = 5000;
+        }
+        if (typeof timeout !== "number") {
+            throw Error("Timeout has to be a number!")
+        }
+        if (!Array.isArray(this.urls) || this.urls.length < 2) {
+            throw Error("setWebvisuUrls has to be called!");
+        }
+
+        this.timeout = timeout;
+        this.isStarted = true;
+        const self = this;
+        this.frame.addEventListener("load", function() {self.onLoad()});
+        this.activeIndex = 0;
+        this.frame.src = this.urls[this.activeIndex];
+        console.info("Webvisu detection started!");
+    }
+    onLoad() {
+        if (this.isStarted) {
+            const self = this;
+            this.timerID = setTimeout(function () {
+                console.log("No message received after " + self.timeout + " ms, switching to different webvisu.");
+                self.switchToDifferentUrl();
+            }, this.timeout);
+        }
+    }
+    switchToDifferentUrl() {
+        this.activeIndex++;
+        if (this.activeIndex >= this.urls.length) {
+            this.activeIndex = 0;
+        }
+        this.frame.src = this.urls[this.activeIndex];
+    }
+    onMessageFromFrame(event) {
+        const activeUrl = this.urls[this.activeIndex], self = this;
+        if (activeUrl.startsWith(event.origin)) {
+            if (this.timerID !== -1) {
+                clearTimeout(this.timerID);
+                this.timerID = -1;
+                console.log("Received messages. Killing timer!");
+            }
+            var message = event.data;
+            console.log(message);
+            if (message === "--INFO--\"Still polling the registration of the visualization. Is the visu stopped?\"") {
+                console.log("Webvisu stopped? Switching to different url!");
+                setTimeout(function() {self.switchToDifferentUrl()}, 1000);
+            }
+            if (message === "Error while processing the visualization: Sending service aborted") {
+                console.log("Download? Switching to different url!");
+                setTimeout(function() {self.switchToDifferentUrl()}, 1000);
+            }
+            if (message === "--INFO--\"Triing to reconnect after error\"") {
+                console.log("Error? Switching to different url!");
+                setTimeout(function() {self.switchToDifferentUrl()}, 1000);
+            }
+        }
+    }
+}