Don't tweet without alt text

Adds a conformation box before tweeting with media lacking alt text

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

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

(I already have a user script manager, let me install it!)

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.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name     Don't tweet without alt text
// @description Adds a conformation box before tweeting with media lacking alt text
// @author   stick
// @version  1
// @homepageURL https://github.com/sticks-stuff/Dont-tweet-without-alt-text
// @include  https://twitter.*/*
// @grant    none
// @require  https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js
// @require  https://greasyfork.org/scripts/31940-waitforkeyelements/code/waitForKeyElements.js
// @namespace https://greasyfork.org/users/710818
// ==/UserScript==

console.log("Don't tweet without alt text loaded!");

waitForKeyElements ('[data-testid="tweetButton"][data-focusable="true"]', tweetButtonLoaded);
waitForKeyElements ('[aria-label="Media"][role="group"]', imagesAdded);

function tweetButtonLoaded (tweetButton)
{
  console.log("hijacked tweet button");
  var tweetButtonReal = tweetButton[0];
  tweetButtonReal.dataset.testid = "hijacked";
  var clonedButton = $(tweetButtonReal).clone(true);
  var parentOfButton = $(tweetButtonReal).parent();
  console.log($(tweetButtonReal).data('events'));
  $(tweetButtonReal).hide();
  $(clonedButton).appendTo($(parentOfButton));
  $(clonedButton).click(function () {
    var attachmentsWithoutAlt = document.querySelectorAll('[aria-label="Media"][role="group"]').length;
    if(attachmentsWithoutAlt > 0) {
      var answer = window.confirm("You have " + attachmentsWithoutAlt + " attachments without alt text in this tweet. Are you sure you want to send?");
      if (answer) {
          $(clonedButton).remove();
          $(tweetButtonReal).click();
          $(tweetButtonReal).show();
      }
      else {
          console.log("cancelled");
      }
    } else {
      $(clonedButton).remove();
      $(tweetButtonReal).click();
      $(tweetButtonReal).show();
    }
  });
}