Pokemon Showdown 备份助手

Pokemon Showdown 本地备份助手

当前为 2021-07-08 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Pokemon Showdown Backup Helper
  3. // @name:zh-CN Pokemon Showdown 备份助手
  4. // @namespace pokemon-showdown-backup-helper
  5. // @version 0.3
  6. // @description Pokemon Showdown Local Backup Helper
  7. // @description:zh-CN Pokemon Showdown 本地备份助手
  8. // @author Sabertaz
  9. // @license MIT License
  10. // @match http://play.pokemonshowdown.com/*
  11. // @match https://play.pokemonshowdown.com/*
  12. // @match http://psim.us/*
  13. // @match https://psim.us/*
  14. // @match http://legacy.psim.us/*
  15. // @match https://legacy.psim.us/*
  16. // @match http://china.psim.us/*
  17. // @match https://china.psim.us/*
  18. // @grant none
  19. // ==/UserScript==
  20.  
  21. (function() {
  22. 'use strict';
  23.  
  24. const buttonGap = 40;
  25. const psTeamsFilename = 'PSTeams.txt';
  26.  
  27. const psTeamsStorageKey = 'showdown_teams';
  28. let buttonIdx = 1;
  29.  
  30. const createButton = (text, func) => {
  31. const button = document.createElement('button');
  32. button.type = 'button';
  33. button.value = 'button';
  34. button.style.position = 'fixed';
  35. button.style.right = '40px';
  36. button.style.bottom = `${buttonIdx * buttonGap}px`;
  37. button.style.zIndex = 9999;
  38. button.textContent = text;
  39. button.classList.add('button');
  40. button.addEventListener('click', func);
  41. buttonIdx++;
  42. return button;
  43. }
  44.  
  45. const downloadTeam = () => {
  46. const teamData = localStorage.getItem(psTeamsStorageKey);
  47. const blob = new Blob([teamData], {type: 'text/csv'});
  48.  
  49. if (window.navigator.msSaveOrOpenBlob) {
  50. window.navigator.msSaveBlob(blob, psTeamsFilename);
  51. } else {
  52. const elem = window.document.createElement('a');
  53. elem.href = window.URL.createObjectURL(blob);
  54. elem.download = psTeamsFilename;
  55. document.body.appendChild(elem);
  56. elem.click();
  57. document.body.removeChild(elem);
  58. }
  59. };
  60.  
  61. const uploadTeam = () => {
  62. if (!window.FileReader) {
  63. alert('Your browser is not supported');
  64. return false;
  65. }
  66.  
  67. const uploader = document.createElement('input');
  68. uploader.type = 'file';
  69. uploader.style.display = 'none';
  70.  
  71. // listen for files
  72. uploader.addEventListener('change', () => {
  73. const files = uploader.files;
  74.  
  75. if (files.length) {
  76. const reader = new FileReader();
  77. reader.addEventListener('load', () => {
  78. uploader.parentNode.removeChild(uploader);
  79. localStorage.setItem(psTeamsStorageKey, reader.result);
  80. location.reload();
  81. })
  82. reader.readAsText(files[0]);
  83. }
  84. })
  85.  
  86. // trigger input
  87. document.body.appendChild(uploader);
  88. uploader.click();
  89. };
  90.  
  91.  
  92. const downloadButton = createButton('Download All Teams', downloadTeam);
  93. const uploadButton = createButton('Upload Local Teams', uploadTeam);
  94. document.body.appendChild(downloadButton);
  95. document.body.appendChild(uploadButton);
  96. })();