CodeWhisperer auth

Auto-selects and pastes (if possible) the CodeWhisperer auth code

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name        CodeWhisperer auth
// @namespace   urn://https://www.georgegillams.co.uk/api/greasemonkey/codewhisperer-auth
// @include     *device.sso.us-east-1.amazonaws.com*
// @include     *awsapps.com/start*
// @exclude     none
// @version     1.1.0
// @description:en	Auto-selects and pastes (if possible) the CodeWhisperer auth code
// @grant    		none
// @description Auto-selects and pastes (if possible) the CodeWhisperer auth code
// @license MIT
// ==/UserScript==

function closeTab(targetElement) {
  const existingButton = document.getElementById(
    'script-added-auto-close-button',
  );
  if (existingButton) {
    existingButton.click();
    return;
  }

  const newElement = document.createElement('a');
  newElement.id = 'script-added-auto-close-button';
  newElement.href = "javascript:window.open('','_self').close();";
  newElement.innerText = 'Close';
  targetElement.appendChild(newElement);
  newElement.click();
}

function doTask() {
  const codeInputElement = document.getElementById('verification_code');
  const buttons = [...document.getElementsByTagName('BUTTON')];
  let pasted = false;
  if (codeInputElement) {
    codeInputElement.focus();
    try {
      // get clipboard content
      const clipboardText = window.clipboardData.getData('Text');
      codeInputElement.value = clipboardText;
      pasted = true;
    } catch (e) {
      console.log('Code could not be pasted');
    }
  }
  const [confirmAndContinueButton] = buttons.filter((b) => b.innerText === "Confirm and continue")
  confirmAndContinueButton?.click();
  const [nextButton] = buttons.filter((b) => b.innerText === 'Next');
  if (pasted || codeInputElement?.value?.length === 9) {
    nextButton?.click();
  }
  const [allowButton] = buttons.filter((b) => b.innerText === 'Allow');
  allowButton?.click();
  if (document.body.innerText.includes('You can close this window')) {
    closeTab(document.getElementsByClassName('awsui-signin-success')[0]);
  }
}

function worker() {
  try {
    doTask();
  } catch (e) {
    // eslint-disable-next-line no-console
    console.log(e);
  }
}

setInterval(worker, 200);