// ==UserScript==
// @name         CargoShorts
// @namespace    http://github.com/Fryguy/cargo_shorts
// @version      0.2
// @description  Auto-click through BlueJeans screens
// @author       Jason Frey
// @match        https://bluejeans.com/*
// @run-at       document-end
// @grant        GM_xmlhttpRequest
// @connect      localhost
// ==/UserScript==

(function() {
    'use strict';

    function isInitialPage() {
        return !isSubsequentPage();
    }

    function isSubsequentPage() {
        return location.pathname == "/xd-local-storage-iframe/";
    }

    function guestLoginInput() {
        return window.top.document.querySelector(".guestView.panel > div:nth-child(2) > input");
    }

    function guestLoginButton() {
        return window.top.document.querySelector(".guestView.panel > button");
    }

    function computerAudioButton() {
        return window.top.document.querySelector(".primaryConnectionsDialog[style*=\"visible\"] .joinComputer");
    }

    function joinMeetingButton() {
        return window.top.document.querySelector("#modalContentMain > div.options.dynamicTooltipEl > div.decisionsHolder.connectionSwitching > div > div.enterHolder > button");
    }

    function waitForEntrypoint() {
        if (isInitialPage()) {
            if (guestLoginInput()) {
                doLogin();
            } else {
                setTimeout(waitForEntrypoint, 1000);
            }
        } else {
            if (computerAudioButton()) {
                doAudioOptions();
                setTimeout(waitForJoinMeetingButton, 1000);
            } else {
                setTimeout(waitForEntrypoint, 1000);
            }
        }
    }

    function waitForJoinMeetingButton() {
        if (joinMeetingButton()) {
            doJoinMeeting();
        } else {
            setTimeout(waitForJoinMeetingButton, 1000);
        }
    }

    function doLogin() {
        GM_xmlhttpRequest({
            method: "GET",
            headers: {
                Accept: "application/json",
            },
            url: "http://localhost:3000/configuration.json",
            onload: function(response) {
                var json = JSON.parse(response.responseText);
                guestLoginInput().value = json.name;
                guestLoginButton().click();
            }
        });
    }

    function doAudioOptions() {
        computerAudioButton().click();
    }

    function doJoinMeeting() {
        joinMeetingButton().click();
    }

    waitForEntrypoint();
})();

@@ -115,16 +115,18 @@
      0.2
+.1
 %0A// @des
@@ -306,16 +306,74 @@
 Request%0A
+// @grant        GM_getValue%0A// @grant        GM_setValue%0A
 // @conn
@@ -1953,24 +1953,367 @@
 gin() %7B%0A    
+    guestLoginInput().value = configuration().name;%0A        guestLoginButton().click();%0A    %7D%0A%0A    function doAudioOptions() %7B%0A        computerAudioButton().click();%0A    %7D%0A%0A    function doJoinMeeting() %7B%0A        joinMeetingButton().click();%0A    %7D%0A%0A    function configuration() %7B%0A        return JSON.parse(GM_getValue(%22configuration%22));%0A    %7D%0A%0A
     GM_xmlht
@@ -2332,20 +2332,16 @@
         
-    
 method: 
@@ -2355,20 +2355,16 @@
         
-    
 headers:
@@ -2378,20 +2378,16 @@
         
-   
- 
 Accept: 
@@ -2418,19 +2418,11 @@
     
-    %7D,%0A    
+%7D,%0A
     
@@ -2482,20 +2482,16 @@
         
-  
-  
 onload: 
@@ -2527,34 +2527,37 @@
     
-    var json = JSON.parse(
+GM_setValue(%22configuration%22, 
 resp
@@ -2592,283 +2592,8 @@
     
-    guestLoginInput().value = json.name;%0A                guestLoginButton().click();%0A            %7D%0A        %7D);%0A    %7D%0A%0A    function doAudioOptions() %7B%0A        computerAudioButton().click();%0A    %7D%0A%0A    function doJoinMeeting() %7B%0A        joinMeetingButton().click();%0A    %7D%0A%0A    
 wait
@@ -2606,16 +2606,34 @@
 rypoint(
+);%0A        %7D%0A    %7D
 );%0A%7D)();

Move fetching of configuration to before the entrypoint