suika8melon / フリマウォッチ 自己アラート名ON

// ==UserScript==
// @name         フリマウォッチ 自己アラート名ON
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://www.furimawatch.net/pc2/*
// @match        https://www.furimawatch.net/pc4/*
// @include      https://www.furimawatch.net/*
// @grant        none
// @license MIT
// @copyright 2021, suika8melon (https://openuserjs.org/users/suika8melon)
// ==/UserScript==
(function() {
  "use strict";

  const MAX_RETRY_COUNT_FIND_DIFF_CONTAINER = 10;

  function setFormValues() {
    document
      .querySelectorAll('input[ng-model^="checkModel.status"]')
      .forEach((el) => {
        if (el.checked) {
          el.click();
        }
      });
    const newChecked = document.querySelectorAll(
      'input[ng-model^="checkModel.status"]'
    )[0].checked;
    if (!newChecked) {
      document.querySelectorAll(
        'input[ng-model^="checkModel.status"]'
      )[0].click();
    }
    // checkbox
    document.querySelectorAll('input[type="checkbox"]')[12].click();

    function setPmin(price) {
      document.querySelector("#inputPmin").value = price;
      document.querySelector('#inputPmin').dispatchEvent(new KeyboardEvent( "input",{key: price }))
    }

    if (document.querySelector('#inputPmin').value?.length == 0) {
      setPmin(1001);
    }

    function calclateMinPrice(source) {
      const finded = source.match(/^[0-9]+\/([0-9]+)/);
      if (finded) {
        setPmin(Math.floor(finded[1] * 0.4,0));
      }
    }

    if (!document.querySelector('#inputName').value) {
      calclateMinPrice(document.querySelector('#inputName').value);
    }

    navigator.clipboard.readText().then(clipText => {
      calclateMinPrice(clipText);
    });

    if (!document.querySelector('#inputKwes').value) {
      document.querySelector("#inputKwes").value = "トラベル トライアル 試供品 割 回";
      document.querySelector('#inputKwes').dispatchEvent(new KeyboardEvent( "input"))
    }
  }

  window.addEventListener("click", (evt) => {
    var retry_counter = 0;
    var hit = false;

    /**
     * diff-container要素を取得
     */
    function findTargetElement() {
      retry_counter++;
      var diff_container_elements = document.querySelector("#inputKws");
      if (diff_container_elements?.size > 0 && !hit) {
        setTimeout(setFormValues, 1000);
        hit = true;
      }
    }

    if (
      evt.srcElement.attributes["ng-click"]?.value.match(
        /gotoEditAlert|createButtonClicked|editButtonClicked\(watch\)/
      )
    ) {
      retry_counter = 0;
      hit = false;
      let set_interval_id = setInterval(findTargetElement, 1000);

      console.log("新しいアラートの作成");
      setTimeout(() => {
        clearInterval(set_interval_id);
      }, 4000);
    }
  });

  document.addEventListener("DOMContentLoaded", () => {
    //  document.querySelector('.btn.btn-primary.ng-pristine.ng-valid.ng-scope.ng-empty.ng-touched')
  });
})();