afr / get-radio-datek

// ==UserScript==
// @name         get-radio-datek
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  try to take over the world!
// @author       afr
// @match        https://onedigit.telkom.co.id/radio
// @icon         https://www.google.com/s2/favicons?sz=64&domain=co.id
// @grant        GM_xmlhttpRequest
// @updateURL    https://openuserjs.org/meta/afr/get-radio-datek.meta.js
// @downloadURL  https://openuserjs.org/install/afr/get-radio-datek.user.js
// @copyright    2024
// @license      MIT
// ==/UserScript==

(function () {
  'use strict';

  // Your code here...

  $(document).ready(() => {

    function getRadioDatek() {
      $("div.modal-footer").append(`<button id="bulls" type="button">test</button>`);
      const btn = $("#bulls");
      btn.on("click", () => {

        const radio_datek = {
          NE: {},
          FE: {}
        };

        const NE_input = $("div#update-form table tr:contains('Ne') td input");
        const NE_textarea = $("div#update-form table tr:contains('Ne') td textarea");

        const FE_input = $("div#update-form table tr:contains('Fe') td input");
        const FE_textarea = $("div#update-form table tr:contains('Fe') td textarea");

        NE_input.each((i, val) => {
          radio_datek.NE[`${val.id.replace(/TRadio_/i, "")}`] = val.value;
        });
        radio_datek.NE[`${NE_textarea[0].id.replace(/TRadio_/i, "")}`] = NE_textarea.val();

        FE_input.each((i, val) => {
          radio_datek.FE[`${val.id.replace(/TRadio_/i, "")}`] = val.value;
        });
        radio_datek.FE[`${FE_textarea[0].id.replace(/TRadio_/i, "")}`] = FE_textarea.val();

        console.log(radio_datek);

        const localURL = "http://localhost:3000/radio-datek";
        const prodURL = "https://autofill-2u8b.onrender.com/radio-datek";

        const details = {
          method: "POST",
          url: prodURL,
          headers: {
            "Content-Type": "application/json"
          },
          data: JSON.stringify(radio_datek),
          onload: res => {
            console.log(res.response);
          }
        };

        GM_xmlhttpRequest(details);

      });
    };

    waitForKeyElements("div#update-form table", getRadioDatek);

  });

  /*--- 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.
    src: https://gist.github.com/BrockA/2625891#file-waitforkeyelements-js
*/
  function waitForKeyElements(
    selectorTxt,
    /* Required: The jQuery selector string that
                        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;
  }
})();