// ==UserScript==
// @name        Google Meet Helper Improved
// @namespace   https://04d.co/
// @match       https://meet.google.com/*
// @grant       none
// @version     1.3
// @author      04dco
// @description Reload if necesarry, disable video, auto mute and auto join in that order. Also switches account (defaults to primary). Original by Vanshaj Girotra.
// @run-at      document-start
// ==/UserScript==

// This script may have severe race condition issues.
// Play with the delay to make it work well on your machine.

// Settings
const DISABLE_VIDEO = true;
const DISABLE_AUDIO = true;
const AUTO_JOIN = true;
const AUTO_RELOAD = true;
const DELAY = 2000;

// if your work email is not the first account (authuser = 0) change the authuser below
const ACCOUNT_SWITCH = {
 enable: false,
 authuser: 0
}

// -----BEGIN USERSCRIPT-----

const getButtonList = () => {
 const node_list = document.getElementsByTagName('div');
 const button_list = [];
 for (let i = 0; i < node_list.length; i = i + 1) {
   if (node_list[i].getAttribute('role') === 'button')
     button_list.push(node_list[i]);
 }
 return button_list;
}


const init_screen_main = () => {
 const button_list = getButtonList();
 const button_map = {
   video: null,
   audio: null,
   join: null
 }
 button_list.forEach(button => {
   const aria_label = button.getAttribute('aria-label')
   if (button.innerText === 'Join now')
     button_map.join = button;
   else if (aria_label && ~aria_label.toLowerCase().indexOf('+ d'))
     button_map.audio = button;
   else if (aria_label && ~aria_label.toLowerCase().indexOf('+ e'))
     button_map.video = button;
 })


 if (DISABLE_VIDEO)
   button_map.video.click()

 if (DISABLE_AUDIO)
   button_map.audio.click()

 if (AUTO_JOIN && button_map.audio && button_map.video)   // join iff audio and video buttons have been clicked
   button_map.join && button_map.join.click();

};


if (AUTO_RELOAD)
{
setTimeout(function()
{
       if ((document.documentElement.textContent).indexOf('Join') > -1)
       {
       console.log ("Found join button.");
       }

       else
       {
       location.reload();
       }
}, DELAY);
}


const checkLoad = () => {
 const divs = document.getElementsByTagName('div')
 let loaded = true
 for (let i=0;i<divs.length; i+=1) {
   if (divs[i].getAttribute('data-loadingmessage') === 'Loading...') { // :/
     loaded = false
   }
 }
 return loaded
}



const checkButtonLoad = () => {
 let clear_interval = false
 const interval_check = setInterval(() => {
   const button_list = getButtonList()
   if (checkLoad()) {
     //  hackerman
       clearInterval(interval_check)
     setTimeout(() => init_screen_main(),500)

   }
 }, 100)
}

const main = () => {
 window.removeEventListener('load', main);
 const params = new URLSearchParams(location.search);
 const authuser = params.get('authuser') || '0'
 if (ACCOUNT_SWITCH.enable && authuser != ACCOUNT_SWITCH.authuser) {
   params.set('authuser', ACCOUNT_SWITCH.authuser)
   window.location.search = params.toString()
 } else {
   checkButtonLoad()
 }
}

window.addEventListener('load', main);
// -----END USERSCRIPT-----