Rhythm Plus Export/Import maps

Rhythm+ export/import

当前为 2024-07-14 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Rhythm Plus Export/Import maps
  3. // @namespace http://tampermonkey.net/
  4. // @version 2024-07-14
  5. // @description Rhythm+ export/import
  6. // @author qiinnv
  7. // @match https://rhythm-plus.com/editor/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=rhythm-plus.com
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. const id = window.location.href.split("/")[4]
  16. const postUrl = 'https://api.rhythm-plus.com/api/v1/sheet/update?sheetId=' + id
  17. const getUrl = 'https://api.rhythm-plus.com/api/v1/sheet/get?sheetId=' + id
  18.  
  19. let mapDataImport = {
  20. "id": id,
  21. "mapping": null,
  22. "visibility":"private",
  23. "length":0,
  24. "noteCount":0
  25. }
  26.  
  27.  
  28. let mapDataImportBool = false
  29.  
  30. let mapDataExport
  31.  
  32. const originalFetch = window.fetch
  33.  
  34. //create custom fetch
  35. window.fetch = function(url, options) {
  36. console.log("a")
  37. if (url === postUrl && options.method === "post" && mapDataImportBool) {
  38. const Header = new Headers(options.headers)
  39. return fetch(url, {
  40. method: "POST",
  41. headers: Header,
  42. body: JSON.stringify(mapDataImport)
  43. })
  44.  
  45. }
  46. else if (url === getUrl && options.method === "get") {
  47. originalFetch(url, options).then(response => {
  48. return response.json().then(json => {
  49. mapDataExport = json
  50. return json
  51. })
  52. })
  53. }
  54.  
  55. return originalFetch(url, options)
  56. }
  57.  
  58. function waitToolbar(callback) {
  59. var element = document.getElementsByClassName("toolbar")[0]
  60. if (element) {
  61. callback(element)
  62. }
  63. else {
  64. setTimeout(function() {
  65. waitToolbar(callback)
  66. }, 500)
  67. }
  68. }
  69.  
  70. waitToolbar(function(toolbar) {
  71. let exportDiv = document.createElement("div")
  72. exportDiv.textContent = "Export"
  73. exportDiv.className = "exportDiv"
  74.  
  75. let importDiv = document.createElement("div")
  76. importDiv.textContent = "Import"
  77. importDiv.className = "importDiv"
  78.  
  79.  
  80. let styles = window.getComputedStyle(toolbar.children[2])
  81. for (let prop of styles) {
  82. importDiv.style.setProperty(prop, styles.getPropertyValue(prop))
  83. exportDiv.style.setProperty(prop, styles.getPropertyValue(prop))
  84. }
  85.  
  86. toolbar.appendChild(importDiv)
  87. toolbar.appendChild(exportDiv)
  88.  
  89. exportDiv.addEventListener('click', function() {
  90.  
  91. let file = new Blob([mapDataExport.mapping], {type: "text/plain"})
  92. let a = document.createElement("a"), url = URL.createObjectURL(file)
  93. a.href = url
  94. a.download = "map.ryp"
  95. document.body.appendChild(a)
  96. a.click()
  97. document.body.removeChild(a)
  98. window.URL.revokeObjectURL(url)
  99. })
  100.  
  101.  
  102. importDiv.addEventListener("click", function() {
  103. let fileInput = document.createElement("input")
  104. fileInput.type = "file"
  105. fileInput.accept = ".ryp"
  106.  
  107. fileInput.onchange = function(event) {
  108. var file = event.target.files[0]
  109. if (file) {
  110. let fileContent = new FileReader()
  111. fileContent.onload = function(fileContent) {
  112. mapDataImport.mapping = fileContent.target.result
  113. mapDataImportBool = true
  114. document.body.removeChild(fileInput)
  115. alert("Loaded map! Press save and refresh the page to view.")
  116. }
  117. fileContent.readAsText(file)
  118. }
  119. }
  120. document.body.appendChild(fileInput)
  121. fileInput.click()
  122.  
  123. })
  124. })
  125.  
  126. })()