Musescore Downloader

download pdf or print any sheets!

目前为 2022-10-19 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Musescore Downloader
  3. // @version 1.2.3
  4. // @description download pdf or print any sheets!
  5. // @author Charlie
  6. // @match https://musescore.com/*
  7. // @namespace https://greasyfork.org/users/890174
  8. // @grant GM_addStyle
  9. // ==/UserScript==
  10.  
  11. setTimeout(() => {
  12. "use strict"
  13. GM_addStyle(`
  14. .SUG3q > .hei_M:nth-child(3) > .AFyen:nth-child(2),
  15. #jmuse-scroller-component div:not(.BmIOX),
  16. .YaB2I > section :nth-last-child(n+2),
  17. aside > div:nth-last-child(2),
  18. aside > div:nth-child(2),
  19. aside > div:nth-child(3),
  20. header > *:not(nav),
  21. .hFMfR > .AFyen,
  22. footer,
  23. ._9aj2,
  24. .p6izg,
  25. .TnQwe,
  26. .U8wvj,
  27. .jxGLy,
  28. .YRlfn,
  29. .YMprF,
  30. .MXIPY {
  31. display: none !important;
  32. }
  33. .dXt6a {
  34. background-color: #e1effe !important;
  35. padding: .3em .8em !important;
  36. border-left: 5px solid #1a4f9f !important;
  37. font-size: 14px !important;
  38. }
  39. .ZvxB2, .AJXCt {
  40. background-color: #e1effe7c !important;
  41. padding: .6em .8em !important;
  42. border-radius: 4px !important;
  43. outline: 1.5px solid #1a4f9f !important;
  44. flex-direction: column !important;
  45. }
  46. aside a {
  47. padding: 0 .3em !important;
  48. transition: background-color ease-out .15s !important;
  49. }
  50. aside a:hover {
  51. background-color: #45f !important;
  52. color: #f3f3ff !important;
  53. }
  54. aside > div:nth-child(5) {
  55. border: none !important;
  56. }
  57. aside > div:nth-child(4),
  58. aside > div:nth-child(5) {
  59. padding: 12px 20px !important;
  60. }
  61. aside > div:nth-child(4) > div {
  62. display: flex !important;
  63. place-items: center !important;
  64. flex-direction: column !important;
  65. }
  66. aside > div:nth-child(4) > div > .Pl3iC {
  67. margin-right: 16px !important;
  68. }
  69. aside > div:nth-child(4) > div.PI5Hd.g1QZl.CgHMj > section:nth-child(2) {
  70. width: 100% !important;
  71. }
  72. .oQdm2 {
  73. max-width: unset !important;
  74. }
  75. aside table {
  76. background-color: #e1effe7c !important;
  77. padding: .3em .6em !important;
  78. border-radius: 3px !important;
  79. outline: 1.5px solid #1a4f9f !important;
  80. }
  81. aside table tr {
  82. padding-bottom: 5px !important;
  83. }
  84. .a0naR,
  85. .Au_pg,
  86. .Al3lQ {
  87. height: 32px !important;
  88. width: 32px !important;
  89. color: transparent !important;
  90. border-radius: 5px !important;
  91. }
  92. .hyzNL {
  93. max-height: unset !important;
  94. }
  95. .dXt6a section {
  96. margin: 0 !important;
  97. }`)
  98.  
  99. const ZH = navigator.language == "zh-CN"
  100.  
  101. async function download() {
  102. const Window = window.open()
  103. const id = location.pathname.match(/\/[^\/]*$/)[0].slice(1)
  104. const length = document.getElementsByClassName("BmIOX").length
  105. Window.document.write(`
  106. <!DOCTYPE html>
  107. <head>
  108. <title>${document.getElementsByTagName('h1')[0].innerText}</title>
  109. <style>
  110. body {
  111. margin: 0;
  112. display: flex;
  113. min-height: 100vh;
  114. flex-direction: column;
  115. place-items: center;
  116. justify-content: center;
  117. color: rgb(49, 63, 78);
  118. background-color: rgb(237, 242, 247);
  119. transform-origin: top;
  120. transition: background-color ease .3s;
  121. will-change: background-color;
  122. overflow-x: scroll;
  123. }
  124.  
  125. svg {
  126. width: 100vw;
  127. height: auto;
  128. display: block;
  129. margin: auto;
  130. }
  131.  
  132. .card {
  133. display: flex;
  134. text-align: center;
  135. place-items: center;
  136. font-size: 1.2em;
  137. width: ${ ZH ? 360 : 480 }px;
  138. box-shadow: 10px 10px 8px rgba(0, 0, 0, 0.07);
  139. padding: 1.5em 2em;
  140. background-color: rgb(255, 255, 255);
  141. border-radius: 8px;
  142. transition: all ease-out .3s;
  143. overflow: hidden;
  144. white-space: nowrap;
  145. }
  146.  
  147. body > * {
  148. animation: 1.5s intro;
  149. }
  150.  
  151. @keyframes intro {
  152. 0%, 20% { opacity: 0; }
  153. 100% { opacity: 1; }
  154. }
  155.  
  156. .card-icon {
  157. display: flex;
  158. flex-direction: column;
  159. place-items: center;
  160. margin-right: 3em;
  161. font-weight: bold;
  162. }
  163.  
  164. .card-text {
  165. flex: 1;
  166. }
  167.  
  168. b {
  169. color: rgb(49, 140, 252);
  170. }
  171.  
  172. .spinner, .spinner * { box-sizing: border-box; }
  173. .spinner {
  174. height: 40px;
  175. width: 40px;
  176. top: calc( -10px * 2 / 3);
  177. margin-left: calc(10px / 3);
  178. margin-bottom: calc(10px / 3);
  179. }
  180. .spinner .sq {
  181. height: 10px;
  182. width: 10px;
  183. top: calc( -10px * 2 / 3);
  184. margin-right: calc(10px / 3);
  185. margin-top: calc(10px / 3);
  186. background: rgb(49, 140, 252);
  187. float: left;
  188. position: relative;
  189. opacity: 0;
  190. animation: spinner 6s infinite;
  191. }
  192. ${Array(9).fill().map((_, i) => `.spinner .sq:nth-child(${i+1}) { animation-delay: calc(300ms * ${8-i}); }`).join("\n")}
  193. .spinner .clear { clear: both; }
  194. @keyframes spinner {
  195. 0% { opacity: 0; }
  196. 5% { opacity: 1; top: 0; }
  197. 50.9% { opacity: 1; top: 0; }
  198. 55.9% { opacity: 0; top: inherit; }
  199. }
  200.  
  201. @media print {
  202. @page {
  203. margin: 0;
  204. }
  205. button {
  206. display: none;
  207. }
  208. svg {
  209. width: 21cm;
  210. height: 29.7cm;
  211. }
  212. }
  213.  
  214. .btn-group {
  215. position: fixed;
  216. left: 32px;
  217. top: 24px;
  218. }
  219. button {
  220. font-size: 1.4em;
  221. font-weight: bold;
  222. box-shadow: 1px 2px 8px rgba(0, 0, 0, 0.1);
  223. padding: .4em ${ ZH ? 1.5 : 0.8 }em;
  224. margin-right: 1.2em;
  225. background-color: rgb(255, 255, 255);
  226. color: rgb(49, 63, 78);
  227. border-radius: 4px;
  228. cursor: pointer;
  229. outline: 1.5px solid rgb(49, 63, 78);
  230. border: none;
  231. transition: all ease-out .2s;
  232. }
  233. button:hover {
  234. background-color: rgb(235, 235, 235);
  235. }
  236. button:active {
  237. outline-color: black;
  238. transform: scale(0.97);
  239. }
  240.  
  241. .hint {
  242. margin: 1em 0;
  243. color: #a7b2be;
  244. }
  245. </style>
  246. </head>
  247. <body>
  248. <div class="card">
  249. <div class="card-icon">
  250. <div class="spinner">
  251. <div class="sq"></div>
  252. <div class="sq"></div>
  253. <div class="sq"></div>
  254. <div class="sq clear"></div>
  255. <div class="sq"></div>
  256. <div class="sq"></div>
  257. <div class="sq clear"></div>
  258. <div class="sq"></div>
  259. <div class="sq"></div>
  260. </div>
  261. <div class="card-icon-text">${ ZH ? "下载中" : "Downloading" }</div>
  262. </div>
  263. <div class="card-text">
  264. ${ ZH ? `已下载 <b id="download-status">0</b> 页,共 <b>${length}</b> 页`
  265. : `<b id="download-status">0</b> page(s) loaded, <b>${length}</b> total` }
  266. </div>
  267. </div>
  268. <span class="hint">${ ZH ? "点击左上方的 <b>打印</b> 按钮,选择 <b>另存为PDF</b> 即可下载乐谱。" : "To download the sheet, click <b>PRINT</b> button on the top left then select <b>Export PDF</b>." }</span>
  269. </body>`)
  270. Window.onbeforeunload = e => {
  271. e.preventDefault()
  272. e.returnValue = "QwQ"
  273. return "awa"
  274. }
  275.  
  276. let data = Array(length).fill(""), cnt = 0
  277.  
  278. async function getData() {
  279. return Promise.all(
  280. data.filter(e => e.length == 0).map((_, i) => new Promise(async (res, rej) => {
  281. let url = await fetch(`https://musescore.com/api/jmuse?id=${id}&type=img&v2=1&index=${i}`, {
  282. headers: { authorization: "8c022bdef45341074ce876ae57a48f64b86cdcf5" }
  283. }).then(e => e.json()).then(e => e.info.url).catch(rej)
  284. data[i] = await fetch(url).then(e => e.text()).catch(rej)
  285. console.log(i, data[i])
  286. Window.document.getElementById("download-status").innerText = ++cnt
  287. res()
  288. }))
  289. ).catch((err) => console.error(err))
  290. }
  291.  
  292. setTimeout(() => {
  293. Window.document.getElementsByClassName("hint")[0].innerText = ZH ? "如果长时间没反应就不要傻等着了,现在或者等会再试试吧。" : "If the process stoped, you can try again now or later."
  294. }, 8 * 1000)
  295.  
  296. await getData()
  297.  
  298. setTimeout(async () => {
  299. Window.document.getElementsByClassName("card-icon-text")[0].innerText = ZH ? "解析中" : "Decoding"
  300. if(cnt != length) {
  301. Window.document.getElementsByClassName("card-text")[0].innerText = ZH ? "下载失败,请稍后再试。" : "Download failed, try again later."
  302. return
  303. }
  304. setTimeout(() => {
  305. Window.document.body.style.background = "white"
  306. Window.document.body.innerHTML = data.join("") + `<div class="btn-group"><button onclick="print()">${ ZH ? "打印" : "PRINT" }</button>`
  307. const svgs = [...Window.document.getElementsByTagName("svg")]
  308. svgs.forEach((e) => e.setAttribute("viewBox", `0 0 ${e.width.baseVal.value} ${e.height.baseVal.value}`))
  309. Window.scrollTo(0, 0)
  310. }, 400)
  311. }, 400)
  312. }
  313.  
  314. const btns = [...document.getElementsByTagName("button")]
  315. btns.filter(el => {
  316. const val = el.attributes.getNamedItem("name")?.value
  317. return val == "download" || val == "print"
  318. }).forEach(el => {
  319. const type = el.attributes.getNamedItem("name").value
  320. const fakeEl = el.cloneNode(true)
  321. fakeEl.style.border = "2px #0dbc79 solid"
  322. if(type == "download") fakeEl.style.background = "#0dbc79"
  323. fakeEl.onclick = download
  324. el.parentNode.replaceChild(fakeEl, el)
  325. })
  326. }, 500)