5
5
// @supportURL https://github.com/Xmader/musescore-downloader/issues
6
6
// @updateURL https://msdl.librescore.org/install.user.js
7
7
// @downloadURL https://msdl.librescore.org/install.user.js
8
- // @version 0.25 .0
8
+ // @version 0.26 .0
9
9
// @description download sheet music from musescore.com for free, no login or Musescore Pro required | 免登录、免 Musescore Pro,免费下载 musescore.com 上的曲谱
10
10
// @author Xmader
11
11
// @icon https://librescore.org/img/icons/logo.svg
358
358
}
359
359
};
360
360
const fetchData = (url, init) => __awaiter(void 0, void 0, void 0, function* () {
361
- const r = yield fetch(url, init);
361
+ const _fetch = getFetch();
362
+ const r = yield _fetch(url, init);
362
363
const data = yield r.arrayBuffer();
363
364
return new Uint8Array(data);
364
365
});
366
+ const fetchBuffer = (url, init) => __awaiter(void 0, void 0, void 0, function* () {
367
+ const d = yield fetchData(url, init);
368
+ return Buffer.from(d.buffer);
369
+ });
365
370
const assertRes = (r) => {
366
371
if (!r.ok)
367
372
throw new Error(`${r.url} ${r.status} ${r.statusText}`);
449
454
};
450
455
451
456
const PDFWorker = function () {
452
- (function () {
457
+ var worker = (function (exports ) {
453
458
454
459
function __awaiter(thisArg, _arguments, P, generator) {
455
460
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
@@ -26444,28 +26449,47 @@ Please pipe the document into a Node stream.\
26444
26449
});
26445
26450
26446
26451
/// <reference lib="webworker" />
26447
- const readData = (blob, type) => {
26448
- return new Promise((resolve, reject) => {
26449
- const reader = new FileReader();
26450
- reader.onload = () => {
26451
- const result = reader.result;
26452
- resolve(result);
26453
- };
26454
- reader.onerror = reject;
26452
+ const readData = (data, type) => {
26453
+ if (!(data instanceof Uint8Array)) { // blob
26454
+ return new Promise((resolve, reject) => {
26455
+ const reader = new FileReader();
26456
+ reader.onload = () => {
26457
+ const result = reader.result;
26458
+ resolve(result);
26459
+ };
26460
+ reader.onerror = reject;
26461
+ if (type === 'dataUrl') {
26462
+ reader.readAsDataURL(data);
26463
+ }
26464
+ else {
26465
+ reader.readAsText(data);
26466
+ }
26467
+ });
26468
+ }
26469
+ else { // buffer
26455
26470
if (type === 'dataUrl') {
26456
- reader.readAsDataURL(blob );
26471
+ return 'data:image/png;base64,' + data.toString('base64' );
26457
26472
}
26458
26473
else {
26459
- reader.readAsText(blob );
26474
+ return data.toString('utf-8' );
26460
26475
}
26461
- });
26476
+ }
26462
26477
};
26478
+ /**
26479
+ * @platform browser
26480
+ */
26463
26481
const fetchBlob = (imgUrl) => __awaiter(void 0, void 0, void 0, function* () {
26464
26482
const r = yield fetch(imgUrl, {
26465
26483
cache: 'no-cache',
26466
26484
});
26467
26485
return r.blob();
26468
26486
});
26487
+ /**
26488
+ * @example
26489
+ * import { PDFWorker } from '../dist/cache/worker'
26490
+ * const { generatePDF } = PDFWorker()
26491
+ * const pdfData = await generatePDF(...)
26492
+ */
26469
26493
const generatePDF = (imgBlobs, imgType, width, height) => __awaiter(void 0, void 0, void 0, function* () {
26470
26494
// @ts-ignore
26471
26495
const pdf = new PDFDocument({
@@ -26498,20 +26522,35 @@ Please pipe the document into a Node stream.\
26498
26522
const buf = yield pdf.getBuffer();
26499
26523
return buf.buffer;
26500
26524
});
26501
- onmessage = (e) => __awaiter(void 0, void 0, void 0, function* () {
26502
- const [imgUrls, imgType, width, height,] = e.data;
26503
- const imgBlobs = yield Promise.all(imgUrls.map(url => fetchBlob(url)));
26504
- const pdfBuf = yield generatePDF(imgBlobs, imgType, width, height);
26505
- postMessage(pdfBuf, [pdfBuf]);
26506
- });
26525
+ /**
26526
+ * @platform browser (web worker)
26527
+ */
26528
+ if (typeof onmessage !== 'undefined') {
26529
+ onmessage = (e) => __awaiter(void 0, void 0, void 0, function* () {
26530
+ const [imgUrls, imgType, width, height,] = e.data;
26531
+ const imgBlobs = yield Promise.all(imgUrls.map(url => fetchBlob(url)));
26532
+ const pdfBuf = yield generatePDF(imgBlobs, imgType, width, height);
26533
+ postMessage(pdfBuf, [pdfBuf]);
26534
+ });
26535
+ }
26507
26536
26508
- }());
26537
+ exports.generatePDF = generatePDF;
26538
+
26539
+ return exports;
26540
+
26541
+ }({}));
26542
+ return worker
26509
26543
};
26510
26544
26511
26545
const scriptUrlFromFunction = (fn) => {
26512
26546
const blob = new Blob(['(' + fn.toString() + ')()'], { type: 'application/javascript' });
26513
26547
return window.URL.createObjectURL(blob);
26514
26548
};
26549
+ // Node.js fix
26550
+ if (typeof Worker === 'undefined') {
26551
+ globalThis.Worker = class {
26552
+ }; // noop shim
26553
+ }
26515
26554
class PDFWorkerHelper extends Worker {
26516
26555
constructor() {
26517
26556
const url = scriptUrlFromFunction(PDFWorker);
@@ -26706,20 +26745,19 @@ Please pipe the document into a Node stream.\
26706
26745
return info.url;
26707
26746
});
26708
26747
26709
- let pdfBlob;
26710
- const _downloadPDF = (imgURLs, imgType, name = '') => __awaiter(void 0, void 0, void 0, function* () {
26711
- if (pdfBlob) {
26712
- return FileSaver_min.saveAs(pdfBlob, `${name}.pdf`);
26713
- }
26714
- const cachedImg = document.querySelector('img[src*=score_]');
26715
- const { naturalWidth: width, naturalHeight: height } = cachedImg;
26748
+ const _exportPDFBrowser = (imgURLs, imgType, dimensions) => __awaiter(void 0, void 0, void 0, function* () {
26716
26749
const worker = new PDFWorkerHelper();
26717
- const pdfArrayBuffer = yield worker.generatePDF(imgURLs, imgType, width, height);
26750
+ const pdfArrayBuffer = yield worker.generatePDF(imgURLs, imgType, dimensions. width, dimensions. height);
26718
26751
worker.terminate();
26719
- pdfBlob = new Blob([pdfArrayBuffer]);
26720
- FileSaver_min.saveAs(pdfBlob, `${name}.pdf`);
26752
+ return pdfArrayBuffer;
26721
26753
});
26722
- const downloadPDF = (scoreinfo, sheet) => __awaiter(void 0, void 0, void 0, function* () {
26754
+ const _exportPDFNode = (imgURLs, imgType, dimensions) => __awaiter(void 0, void 0, void 0, function* () {
26755
+ const imgBufs = yield Promise.all(imgURLs.map(url => fetchBuffer(url)));
26756
+ const { generatePDF } = PDFWorker();
26757
+ const pdfArrayBuffer = yield generatePDF(imgBufs, imgType, dimensions.width, dimensions.height);
26758
+ return pdfArrayBuffer;
26759
+ });
26760
+ const exportPDF = (scoreinfo, sheet) => __awaiter(void 0, void 0, void 0, function* () {
26723
26761
const imgType = sheet.imgType;
26724
26762
const pageCount = sheet.pageCount;
26725
26763
const rs = Array.from({ length: pageCount }).map((_, i) => {
@@ -26731,7 +26769,23 @@ Please pipe the document into a Node stream.\
26731
26769
}
26732
26770
});
26733
26771
const sheetImgURLs = yield Promise.all(rs);
26734
- return _downloadPDF(sheetImgURLs, imgType, scoreinfo.fileName);
26772
+ const args = [sheetImgURLs, imgType, sheet.dimensions];
26773
+ if (!detectNode) {
26774
+ return _exportPDFBrowser(...args);
26775
+ }
26776
+ else {
26777
+ return _exportPDFNode(...args);
26778
+ }
26779
+ });
26780
+ let pdfBlob;
26781
+ const downloadPDF = (scoreinfo, sheet, saveAs) => __awaiter(void 0, void 0, void 0, function* () {
26782
+ const name = scoreinfo.fileName;
26783
+ if (pdfBlob) {
26784
+ return saveAs(pdfBlob, `${name}.pdf`);
26785
+ }
26786
+ const pdfArrayBuffer = yield exportPDF(scoreinfo, sheet);
26787
+ pdfBlob = new Blob([pdfArrayBuffer]);
26788
+ saveAs(pdfBlob, `${name}.pdf`);
26735
26789
});
26736
26790
26737
26791
const MSCZ_BUF_SYM = Symbol('msczBufferP');
@@ -27449,11 +27503,14 @@ Please pipe the document into a Node stream.\
27449
27503
}
27450
27504
get thumbnailUrl() {
27451
27505
var _a;
27452
- // url to the image of the first page
27453
27506
const el = this.document.querySelector('link[as=image]');
27454
27507
const url = ((el === null || el === void 0 ? void 0 : el.href) || ((_a = this.sheet0Img) === null || _a === void 0 ? void 0 : _a.src));
27455
27508
return url.split('@')[0];
27456
27509
}
27510
+ get dimensions() {
27511
+ const { naturalWidth: width, naturalHeight: height } = this.sheet0Img;
27512
+ return { width, height };
27513
+ }
27457
27514
}
27458
27515
const getActualId = (scoreinfo, _fetch = getFetch()) => __awaiter(void 0, void 0, void 0, function* () {
27459
27516
if (scoreinfo.id <= 1000000000000) {
@@ -27497,7 +27554,7 @@ Please pipe the document into a Node stream.\
27497
27554
});
27498
27555
btnList.add({
27499
27556
name: i18n('DOWNLOAD')('PDF'),
27500
- action: BtnAction.process(() => downloadPDF(scoreinfo, new SheetInfoInPage(document)), fallback, 3 * 60 * 1000 /* 3min */),
27557
+ action: BtnAction.process(() => downloadPDF(scoreinfo, new SheetInfoInPage(document), saveAs ), fallback, 3 * 60 * 1000 /* 3min */),
27501
27558
});
27502
27559
btnList.add({
27503
27560
name: i18n('DOWNLOAD')('MXL'),
0 commit comments