afr / insera_image

// ==UserScript==
// @name         insera_image
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description  try to take over the world!
// @author       afr
// @match        https://oss-incident.telkom.co.id/jw/web/userview/ticketIncidentService/ticketIncidentService/*
// @updateURL    https://openuserjs.org/meta/afr/insera_image.meta.js
// @downloadURL  https://openuserjs.org/install/afr/insera_image.user.js
// @copyright    2024
// @license      MIT
// ==/UserScript==

(function () {
  'use strict';

  // Your code here...
  // waitForKeyEements
  /*--- waitForKeyElements():  A utility function, for Greasemonkey scripts,
  that detects and handles AJAXed content.

  Usage example:

      waitForKeyElements (
          "div.comments"
          , commentCallbackFunction
      );

      //--- Page-specific function to do what we want when the node is found.
      function commentCallbackFunction (jNode) {
          jNode.text ("This comment changed by waitForKeyElements().");
      }

  IMPORTANT: This function requires your script to have loaded jQuery.
*/
  function waitForKeyElements(
    selectorTxt,
    /* Required: The jQuery selector string thatur
                 specifies the desired element(s).
             */
    actionFunction,
    /* Required: The code to run when elements are
                    found. It is passed a jNode to the matched
                    element.
                */
    bWaitOnce,
    /* Optional: If false, will continue to scan for
               new elements even after the first match is
               found.
           */
    iframeSelector
    /* Optional: If set, identifies the iframe to
                   search.
               */
  ) {
    var targetNodes, btargetsFound;

    if (typeof iframeSelector == "undefined") targetNodes = $(selectorTxt);
    else targetNodes = $(iframeSelector).contents().find(selectorTxt);

    if (targetNodes && targetNodes.length > 0) {
      btargetsFound = true;
      /*--- Found target node(s).  Go through each and act if they
          are new.
      */
      targetNodes.each(function () {
        var jThis = $(this);
        var alreadyFound = jThis.data("alreadyFound") || false;

        if (!alreadyFound) {
          //--- Call the payload function.
          var cancelFound = actionFunction(jThis);
          if (cancelFound) btargetsFound = false;
          else jThis.data("alreadyFound", true);
        }
      });
    }
    else {
      btargetsFound = false;
    }

    //--- Get the timer-control variable for this selector.
    var controlObj = waitForKeyElements.controlObj || {};
    var controlKey = selectorTxt.replace(/[^\w]/g, "_");
    var timeControl = controlObj[controlKey];

    //--- Now set or clear the timer as appropriate.
    if (btargetsFound && bWaitOnce && timeControl) {
      //--- The only condition where we need to clear the timer.
      clearInterval(timeControl);
      delete controlObj[controlKey];
    }
    else {
      //--- Set a timer, if needed.
      if (!timeControl) {
        timeControl = setInterval(function () {
          waitForKeyElements(
            selectorTxt,
            actionFunction,
            bWaitOnce,
            iframeSelector
          );
        }, 300);
        controlObj[controlKey] = timeControl;
      }
    }
    waitForKeyElements.controlObj = controlObj;
  }

  // $(document).ready(() => {
  //     $("head").append('<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">');
  //     $("body").append('<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>');
  // });

  function sendImage(jNode) {
    console.log("FOUND!!");
    const header = $("h2#header-title-ticket").html();
    jNode.on("load", () => {
      console.log("iframe loaded");
      const details = $('textarea[id*="_details"]').val(); //jNode.contents().find("input#summary").val();
      const fd = new FormData();
      fd.append("header", header);
      fd.append("details", details);
      const getMyBlobs = new Promise((resolve, reject) => {
        jNode.contents().find("img").each(async (i, val) => {
          const url = val.src;
          if (/.gif/g.test(url)) {
            console.log("ada gif coy");
            return;
          }
          else {
            try {
              const res = await fetch(url);
              const blob = await res.blob();
              fd.append("evidence", blob);
            }
            catch (error) {
              console.log(error);
              reject(error);
            }
          }
        });
        resolve(fd);
      });
      getMyBlobs.then((val, err) => {
        if (val) {
          console.log(val);
          jNode.contents().find("a[onclick='copyImage()']").after(`<button style='margin-left: 10px;' id='bulls' type='button'>Send</button>`);
          const btn = jNode.contents().find("#bulls");
          btn.on("click", () => {
            const localurl = "http://localhost:3000/send-me-img";
            const produrl = "https://autofill-2u8b.onrender.com/send-me-img";
            const url = "https://autofill.faizruzain.site/send-me-img";
            console.log(fd);
            //ajax
            $.ajax(url, {
              method: "POST",
              data: fd,
              processData: false,
              contentType: false,
              complete: (res) => {
                btn.after(`<p>${res.responseJSON.message}</p>`);
              }
            });
          });
        }
        else {
          console.log(err);
        }
      });
    });
  };

  waitForKeyElements("iframe[id*='_work_logs']", sendImage);

})();