Trimps Tweaks

various tweaks like Quicksave/Quickload for Trimps

当前为 2024-03-29 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Trimps Tweaks
// @namespace    https://github.com/Alistair1231/my-userscripts/
// @version      0.2.2
// @description  various tweaks like Quicksave/Quickload for Trimps
// @author       Alistair1231
// @match        https://trimps.github.io/
// @icon         https://icons.duckduckgo.com/ip2/github.io.ico
// @license MIT
// ==/UserScript==
// https://github.com/Alistair1231/my-userscripts/raw/master/trimps-tweaks.user.js
// https://greasyfork.org/en/scripts/491186-trimps-tweaks/

const getSave = () => window.save(true);
const setSave = (save) => window.load(save);

const copy = async (text) => await window.navigator.clipboard.writeText(text);
const paste = () => prompt('Paste save here');

const addHotkey = (key, action) => {
  document.addEventListener('keydown', (e) => {
    if (e.key === key) action(e);
  });
}

// show a toast bubble on the bottom right of the screen, green with white text on success, red with white text on failure
const showToast = (message, success = true) => {
  const toast = document.createElement('div');
  toast.style.cssText = `position: fixed;
    bottom: 7vh;
    right: 1vw;
    background-color: ${success ? '#0a3' : '#a30'};
    color: #e7e7e7;
    padding: 10px;
    border-radius: 10px;
    box-shadow: 2px 2px 5px black;
    z-index: 9999;
    opacity: 1;
    transition: opacity 1s;`;
  toast.textContent = message;
  document.body.appendChild(toast);

  setTimeout(() => {
    toast.style.opacity = '0';
    setTimeout(() => {
      toast.remove()
    }, 1000);
  }, 2000);
}


const addLabel = (selector, filter, value) => {
  const run = () => {
    const exportDiv = [...document.querySelectorAll(selector)].find(x => x.innerHTML === filter);
    if (exportDiv) {
      exportDiv.innerHTML += value;
      return true;
    }
    return false;
  }
  // try to run immediately, if it fails, wait for the settings to load
  if (run()) return;

  new Promise(resolve => {
    const observer = new MutationObserver(() => {
      const target = document.querySelector(selector);
      if (target) {
        observer.disconnect();
        resolve();
      }
    });
    observer.observe(document.body, {childList: true, subtree: true});
  }).then(run);
}


(async () => {
  'use strict';
  // export save to clipboard when pressing F8
  addHotkey('F8', (e) => {
    e.preventDefault();
    copy(getSave());
    showToast('Exported save to clipboard');
  });

  // import save from clipboard when pressing F9
  addHotkey('F9', async (e) => {
    e.preventDefault();
    console.log('Backup save:', getSave());
    setSave(paste());
  });

  // add (F8)/(F9) to the export/import buttons
  addLabel("#settingsTable div", "Export", " (F8)");
  addLabel("#settingsTable div", "Import", " (F9)");

})();