Skip to content

Commit db1bcf3

Browse files
committed
v0.25.0
1 parent 751d593 commit db1bcf3

File tree

3 files changed

+94
-50
lines changed

3 files changed

+94
-50
lines changed

dist/main.js

+92-48
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// @supportURL https://github.com/Xmader/musescore-downloader/issues
66
// @updateURL https://msdl.librescore.org/install.user.js
77
// @downloadURL https://msdl.librescore.org/install.user.js
8-
// @version 0.24.3
8+
// @version 0.25.0
99
// @description download sheet music from musescore.com for free, no login or Musescore Pro required | 免登录、免 Musescore Pro,免费下载 musescore.com 上的曲谱
1010
// @author Xmader
1111
// @icon https://librescore.org/img/icons/logo.svg
@@ -348,6 +348,9 @@
348348
// eslint-disable-next-line @typescript-eslint/no-var-requires
349349
const nodeFetch = require('node-fetch');
350350
return (input, init) => {
351+
if (typeof input === 'string' && !input.startsWith('http')) { // fix: Only absolute URLs are supported
352+
input = 'https://musescore.com' + input;
353+
}
351354
init = Object.assign({ headers: NODE_FETCH_HEADERS }, init);
352355
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
353356
return nodeFetch(input, init);
@@ -417,7 +420,7 @@
417420
targetEl.setAttribute(eventName, `this['${id}'](document.createElement('iframe'))`);
418421
});
419422
});
420-
const console$1 = (window || global).console; // Object.is(window.console, unsafeWindow.console) == false
423+
const console$1 = (typeof window !== 'undefined' ? window : global).console; // Object.is(window.console, unsafeWindow.console) == false
421424
const windowOpenAsync = (targetEl, ...args) => {
422425
return getSandboxWindowAsync(targetEl).then(w => w.open(...args));
423426
};
@@ -26575,13 +26578,15 @@ Please pipe the document into a Node stream.\
2657526578
}
2657626579
}
2657726580

26578-
/* eslint-disable no-extend-native */
2657926581
const TYPE_REG = /type=(img|mp3|midi)/;
2658026582
/**
2658126583
* I know this is super hacky.
2658226584
*/
2658326585
const magicHookConstr = (() => {
2658426586
const l = {};
26587+
if (detectNode) { // noop in CLI
26588+
return () => Promise.resolve('');
26589+
}
2658526590
try {
2658626591
const p = Object.getPrototypeOf(document.body);
2658726592
Object.setPrototypeOf(document.body, null);
@@ -26597,7 +26602,7 @@ Please pipe the document into a Node stream.\
2659726602
const token = (_a = init === null || init === void 0 ? void 0 : init.headers) === null || _a === void 0 ? void 0 : _a.Authorization;
2659826603
if (typeof url === 'string' && token) {
2659926604
const m = url.match(TYPE_REG);
26600-
console$1.debug(url, token, m);
26605+
console.debug(url, token, m);
2660126606
if (m) {
2660226607
const type = m[1];
2660326608
// eslint-disable-next-line no-unused-expressions
@@ -26613,7 +26618,7 @@ Please pipe the document into a Node stream.\
2661326618
Object.setPrototypeOf(document.body, p);
2661426619
}
2661526620
catch (err) {
26616-
console$1.error(err);
26621+
console.error(err);
2661726622
}
2661826623
return (type) => __awaiter(void 0, void 0, void 0, function* () {
2661926624
return new Promise((resolve) => {
@@ -26629,51 +26634,70 @@ Please pipe the document into a Node stream.\
2662926634
midi: magicHookConstr('midi'),
2663026635
mp3: magicHookConstr('mp3'),
2663126636
};
26637+
26638+
/* eslint-disable no-extend-native */
2663226639
const getApiUrl = (id, type, index) => {
2663326640
return `/api/jmuse?id=${id}&type=${type}&index=${index}&v2=1`;
2663426641
};
26642+
/**
26643+
* hard-coded auth tokens
26644+
*/
26645+
const useBuiltinAuth = (type) => {
26646+
switch (type) {
26647+
case 'img': return '8c022bdef45341074ce876ae57a48f64b86cdcf5';
26648+
case 'midi': return '38fb9efaae51b0c83b5bb5791a698b48292129e7';
26649+
case 'mp3': return '63794e5461e4cfa046edfbdddfccc1ac16daffd2';
26650+
}
26651+
};
2663526652
const getApiAuth = (type, index) => __awaiter(void 0, void 0, void 0, function* () {
26636-
var _a;
26653+
var _a, _b, _c;
26654+
if (detectNode) {
26655+
// we cannot intercept API requests in Node.js (as no requests are sent), so go straightforward to the hard-coded tokens
26656+
return useBuiltinAuth(type);
26657+
}
2663726658
const magic = magics[type];
2663826659
if (magic instanceof Promise) {
2663926660
// force to retrieve the MAGIC
26640-
switch (type) {
26641-
case 'midi': {
26642-
const el = document.querySelector('button[hasaccess]');
26643-
el.click();
26644-
break;
26645-
}
26646-
case 'mp3': {
26647-
const el = document.querySelector('button[title="Toggle Play"]');
26648-
el.click();
26649-
break;
26650-
}
26651-
case 'img': {
26652-
const imgE = document.querySelector('img[src*=score_]');
26653-
const nextE = (_a = imgE === null || imgE === void 0 ? void 0 : imgE.parentElement) === null || _a === void 0 ? void 0 : _a.nextElementSibling;
26654-
if (nextE)
26655-
nextE.scrollIntoView();
26656-
break;
26661+
try {
26662+
switch (type) {
26663+
case 'midi': {
26664+
const fsBtn = document.querySelector('button[title="Toggle Fullscreen"]');
26665+
const el = (_b = (_a = fsBtn.parentElement) === null || _a === void 0 ? void 0 : _a.parentElement) === null || _b === void 0 ? void 0 : _b.querySelector('button');
26666+
el.click();
26667+
break;
26668+
}
26669+
case 'mp3': {
26670+
const el = document.querySelector('button[title="Toggle Play"]');
26671+
el.click();
26672+
break;
26673+
}
26674+
case 'img': {
26675+
const imgE = document.querySelector('img[src*=score_]');
26676+
const nextE = (_c = imgE === null || imgE === void 0 ? void 0 : imgE.parentElement) === null || _c === void 0 ? void 0 : _c.nextElementSibling;
26677+
if (nextE)
26678+
nextE.scrollIntoView();
26679+
break;
26680+
}
2665726681
}
2665826682
}
26683+
catch (err) {
26684+
console.error(err);
26685+
return useBuiltinAuth(type);
26686+
}
2665926687
}
2666026688
try {
2666126689
return yield useTimeout(magic, 5 * 1000 /* 5s */);
2666226690
}
26663-
catch (_b) {
26664-
console$1.error(type, 'token timeout');
26665-
switch (type) {
26666-
// try hard-coded tokens
26667-
case 'img': return '8c022bdef45341074ce876ae57a48f64b86cdcf5';
26668-
case 'midi': return '38fb9efaae51b0c83b5bb5791a698b48292129e7';
26669-
case 'mp3': return '63794e5461e4cfa046edfbdddfccc1ac16daffd2';
26670-
}
26691+
catch (_d) {
26692+
console.error(type, 'token timeout');
26693+
// try hard-coded tokens
26694+
return useBuiltinAuth(type);
2667126695
}
2667226696
});
26673-
const getFileUrl = (id, type, index = 0) => __awaiter(void 0, void 0, void 0, function* () {
26697+
const getFileUrl = (id, type, index = 0, _fetch = getFetch()) => __awaiter(void 0, void 0, void 0, function* () {
2667426698
const url = getApiUrl(id, type, index);
2667526699
const auth = yield getApiAuth(type);
26676-
const r = yield fetch(url, {
26700+
const r = yield _fetch(url, {
2667726701
headers: {
2667826702
Authorization: auth,
2667926703
},
@@ -27273,15 +27297,19 @@ Please pipe the document into a Node stream.\
2727327297
BtnAction.openUrl = BtnAction.download;
2727427298
BtnAction.mscoreWindow = (scoreinfo, fn) => {
2727527299
return (btnName, btn, setText) => __awaiter(this, void 0, void 0, function* () {
27300+
// save btn event for later use
2727627301
const _onclick = btn.onclick;
27302+
// clear btn event
2727727303
btn.onclick = null;
27304+
// set btn text to "PROCESSING"
2727827305
setText(i18n('PROCESSING')());
27306+
// open a new tab
2727927307
const w = yield windowOpenAsync(btn, '');
27308+
// add texts to the new tab
2728027309
const txt = document.createTextNode(i18n('PROCESSING')());
2728127310
w.document.body.append(txt);
27282-
// set page hooks
27283-
// eslint-disable-next-line prefer-const
27284-
let score;
27311+
// set page hooks that the new tab also closes as the og tab closes
27312+
let score; // eslint-disable-line prefer-const
2728527313
const destroy = () => {
2728627314
score && score.destroy();
2728727315
w.close();
@@ -27290,13 +27318,38 @@ Please pipe the document into a Node stream.\
2729027318
w.addEventListener('beforeunload', () => {
2729127319
score && score.destroy();
2729227320
window.removeEventListener('unload', destroy);
27321+
// reset btn text
2729327322
setText(btnName);
27323+
// reinstate btn event
2729427324
btn.onclick = _onclick;
2729527325
});
27296-
score = yield loadMscore(scoreinfo, w);
27297-
fn(w, score, txt);
27326+
try {
27327+
// fetch mscz & process using mscore
27328+
score = yield loadMscore(scoreinfo, w);
27329+
fn(w, score, txt);
27330+
}
27331+
catch (err) {
27332+
console$1.error(err);
27333+
// close the new tab & show error popup
27334+
w.close();
27335+
BtnAction.errorPopup()(btnName, btn, setText);
27336+
}
2729827337
});
2729927338
};
27339+
BtnAction.errorPopup = () => {
27340+
return (btnName, btn, setText) => {
27341+
setText(i18n('BTN_ERROR')());
27342+
// ask user to send Discord message
27343+
alert('❌Download Failed!\n\n' +
27344+
'Send your URL to the #dataset-patcher channel ' +
27345+
'in the LibreScore Community Discord server:\n' + DISCORD_URL);
27346+
// open Discord on 'OK'
27347+
const a = document.createElement('a');
27348+
a.href = DISCORD_URL;
27349+
a.target = '_blank';
27350+
a.dispatchEvent(new MouseEvent('click'));
27351+
};
27352+
};
2730027353
BtnAction.process = (fn, fallback, timeout = 10 * 60 * 1000 /* 10min */) => {
2730127354
return (name, btn, setText) => __awaiter(this, void 0, void 0, function* () {
2730227355
const _onclick = btn.onclick;
@@ -27314,16 +27367,7 @@ Please pipe the document into a Node stream.\
2731427367
setText(name);
2731527368
}
2731627369
else {
27317-
setText(i18n('BTN_ERROR')());
27318-
// ask user to send Discord message
27319-
alert('❌Download Failed!\n\n' +
27320-
'Send your URL to the #dataset-patcher channel ' +
27321-
'in the LibreScore Community Discord server:\n' + DISCORD_URL);
27322-
// open Discord on 'OK'
27323-
const a = document.createElement('a');
27324-
a.href = DISCORD_URL;
27325-
a.target = '_blank';
27326-
a.dispatchEvent(new MouseEvent('click'));
27370+
BtnAction.errorPopup()(name, btn, setText);
2732727371
}
2732827372
}
2732927373
btn.onclick = _onclick;

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "musescore-downloader",
3-
"version": "0.24.3",
3+
"version": "0.25.0",
44
"description": "download sheet music from musescore.com for free, no login or Musescore Pro required | 免登录、免 Musescore Pro,免费下载 musescore.com 上的曲谱",
55
"main": "dist/main.js",
66
"bin": "dist/cli.js",

0 commit comments

Comments
 (0)