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

Switch to unified view

a/wvclient.js b/wvdetection.js
...
...
673
 * the library.  If this is what you want to do, use the GNU Lesser General
673
 * the library.  If this is what you want to do, use the GNU Lesser General
674
 * Public License instead of this License.  But first, please read
674
 * Public License instead of this License.  But first, please read
675
 * <https://www.gnu.org/licenses/why-not-lgpl.html>.
675
 * <https://www.gnu.org/licenses/why-not-lgpl.html>.
676
 */
676
 */
677
677
678
const consoleLog = window.console.log;
678
class WVDetection {
679
const newConsole = window.console;
679
680
newConsole.log = function(args) {
680
    constructor(frameID) {
681
        if (frameID === undefined || frameID === null|| typeof frameID  !== "string") {
682
            throw Error("The element id of the frame has to be a string!");
683
        }
684
        this.frame = document.getElementById(frameID);
685
        if (this.frame === null|| (!(this.frame instanceof HTMLIFrameElement))) {
686
            throw Error("Cannot find the frame");
687
        }
688
        this.urls = [];
689
        this.activeIndex = -1;
690
        this.isStarted = false;
691
        this.timerID = -1;
692
        const self = this;
693
        window.addEventListener("message", function(event) {self.onMessageFromFrame(event)}, false);
694
    }
695
696
    /**
697
     * Set the urls to use
698
     * @param urls Array of strings
699
     */
700
    setWebvisuUrls(urls) {
681
    if (parent !== undefined)
701
        if (urls === undefined || urls === null || (!Array.isArray(urls))) {
682
        parent.postMessage(args, "*");
702
            throw Error ("Argument has to be an array of strings!");
683
    consoleLog(args);
703
        }
684
};
704
        if (urls.length < 2) {
685
newConsole.warn = function(args) {
705
            throw Error("The array must at least contain two entries!");
706
        }
707
        if (this.isStarted) {
708
            throw Error("Cannot change urls while the check is running.");
709
        }
710
        urls.forEach((element) => {
711
            if (!element.startsWith("http://") && !element.startsWith("https://")) {
712
                throw Error ("Urls have to start with http:// or https://.");
713
            }
714
        });
715
        this.urls = urls;
716
    }
717
718
    /**
719
     * Start the webvisu detection with the specified wait timeout
720
     * @param timeout ms waiting for new messages (default 5000ms).
721
     */
722
    start(timeout) {
686
    if (parent !== undefined)
723
        if (timeout === undefined || timeout === null) {
687
        parent.postMessage(args, "*");
724
            timeout = 5000;
688
    consoleLog(args);
725
        }
689
};
726
        if (typeof timeout !== "number") {
690
newConsole.info = newConsole.log;
727
            throw Error("Timeout has to be a number!")
691
newConsole.error = function(args) {
728
        }
692
    if (parent !== undefined)
729
        if (!Array.isArray(this.urls) || this.urls.length < 2) {
693
        parent.postMessage(args, "*");
730
            throw Error("setWebvisuUrls has to be called!");
694
    consoleLog(args);
731
        }
695
};
732
696
console = newConsole;
733
        this.timeout = timeout;
734
        this.isStarted = true;
735
        const self = this;
736
        this.frame.addEventListener("load", function() {self.onLoad()});
737
        this.activeIndex = 0;
738
        this.frame.src = this.urls[this.activeIndex];
739
        console.info("Webvisu detection started!");
740
    }
741
    onLoad() {
742
        if (this.isStarted) {
743
            const self = this;
744
            this.timerID = setTimeout(function () {
745
                console.log("No message received after " + self.timeout + " ms, switching to different webvisu.");
746
                self.switchToDifferentUrl();
747
            }, this.timeout);
748
        }
749
    }
750
    switchToDifferentUrl() {
751
        this.activeIndex++;
752
        if (this.activeIndex >= this.urls.length) {
753
            this.activeIndex = 0;
754
        }
755
        this.frame.src = this.urls[this.activeIndex];
756
    }
757
    onMessageFromFrame(event) {
758
        const activeUrl = this.urls[this.activeIndex], self = this;
759
        if (activeUrl.startsWith(event.origin)) {
760
            if (this.timerID !== -1) {
761
                clearTimeout(this.timerID);
762
                this.timerID = -1;
763
                console.log("Received messages. Killing timer!");
764
            }
765
            var message = event.data;
766
            console.log(message);
767
            if (message === "--INFO--\"Still polling the registration of the visualization. Is the visu stopped?\"") {
768
                console.log("Webvisu stopped? Switching to different url!");
769
                setTimeout(function() {self.switchToDifferentUrl()}, 1000);
770
            }
771
            if (message === "Error while processing the visualization: Sending service aborted") {
772
                console.log("Download? Switching to different url!");
773
                setTimeout(function() {self.switchToDifferentUrl()}, 1000);
774
            }
775
            if (message === "--INFO--\"Triing to reconnect after error\"") {
776
                console.log("Error? Switching to different url!");
777
                setTimeout(function() {self.switchToDifferentUrl()}, 1000);
778
            }
779
        }
780
    }
781
}