Greasy Fork镜像 is available in English.

lidarts Rest Score Input

adding restscore functionalty to lidarts

当前为 2023-07-27 提交的版本,查看 最新版本

// ==UserScript==
// @name           lidarts Rest Score Input
// @name:de        lidarts Rest-Betrag Eingabe
// @version        0.3
// @description    adding restscore functionalty to lidarts
// @description:de fügt in lidarts die Rest-Betrag Eingabe hinzu 
// @author         AlexisDot
// @license        MIT
// @match          https://lidarts.org/game/*
// @namespace      https://greasyfork.dpdns.org/en/users/913506-alexisdot
// ==/UserScript==
/*jshint esversion: 6 */

(function () {
  'use strict';

  let scoreSelfElement = null;
  let scoreInputForm = document.querySelector('#score_input');
  let scoreInput = scoreInputForm.querySelector('#score_value');
  let isNotCricket = document.querySelector('#cricket_scoreboard')
    ? false
    : true;
  let isLocalGame = false;
  let isComputerGame = false;

  async function initRestScoreInput() {
    let url = `https://lidarts.org/api${window.location.pathname}`;
    const response = await fetch(url, {
      method: 'GET',
    });
    const responseJson = await response.json();
    window['myJson'] = responseJson;
    isLocalGame = responseJson.p1_name == responseJson.p2_name;
    isComputerGame = responseJson.p2_name == null;

    if (scoreInput && isNotCricket) {
      document.head.insertAdjacentHTML(
        'beforeend',
        /*html*/ `
    <style>
      .p1_turn_name_card .card-body.select-highlight, .p2_turn_name_card .card-body.select-highlight{
          background: red;
          animation: mymove 3s infinite;
          cursor: pointer;
        }

        @keyframes mymove {
          from {background-color: inherit;}
          50% {background-color: rgb(227,172,17);}
          to {background-color: inherit;}
        }
    </style>`
      );

      if (!isLocalGame && !isComputerGame) {
        document.querySelector('.container').insertAdjacentHTML(
          'afterbegin',
          /*html*/ `
        <div id="player-select-notice">
        <h2 class="text-light text-center">
        Please click on your name!
        </h2>
        <br>
        <h4 class="text-light text-center">
        Reload if you selected the wrong player
        </h4>
        <br>
        <h4 class="text-light text-center">
        How to use: Enter rest score, click rest OR press 'r' or '/', ..., enjoy.
        </h4>
        </div>`
        );

        document
          .querySelector('.p1_turn_name_card .card-body')
          .classList.add('select-highlight');
        document
          .querySelector('.p2_turn_name_card .card-body')
          .classList.add('select-highlight');

        document.querySelectorAll('.select-highlight').forEach((el) =>
          el.addEventListener('click', (e) => {
            scoreSelfElement = e.currentTarget.parentElement
              .closest('.card-body')
              .querySelector('[id$=_score]');

            document
              .querySelectorAll('.select-highlight')
              .forEach((el) => el.classList.remove('select-highlight'));

            document.querySelector('#player-select-notice').remove();
          })
        );
      }

      if (isComputerGame) {
        scoreSelfElement = document.querySelector('#p1_score');
      }

      let dummyDiv = document.createElement('div');
      dummyDiv.innerHTML = /*html*/ `
    <button type="button" class="mt-3 btn btn-lg btn-outline-info btn-block" id="rest-score-input">Rest</button>
  `;

      let restScoreInput = dummyDiv
        .querySelector('#rest-score-input')
        .cloneNode(true);

      scoreInputForm.insertAdjacentElement('afterend', restScoreInput);

      function restScoreSubmit() {
        let restScore = scoreInput.value;
        let scoreSelf = isLocalGame
          ? document.querySelector('.player_turn.border-light [id$=_score]')
              .innerText
          : scoreSelfElement.innerText;
        let currentScore = parseInt(scoreSelf);
        let thrownScore = currentScore - restScore;

        scoreInput.value = thrownScore;
        scoreInput.focus();

        $(scoreInputForm).submit();
      }

      restScoreInput.addEventListener('click', (e) => {
        e.preventDefault();
        restScoreSubmit();
      });

      document.addEventListener('keydown', (e) => {
        if (e.key === '/' || e.key === 'r') {
          e.preventDefault();
          restScoreSubmit();
        }
      });
    }
  }

  initRestScoreInput();
})();
长期地址
遇到问题?请前往 GitHub 提 Issues,或加Q群1031348184

赞助商

Fishcpy

广告

Rainyun

一年攒够 12 元

云驰互联

云驰互联