Skip to content

Commit 78f0911

Browse files
committed
v0.26.0
1 parent 04a7660 commit 78f0911

File tree

3 files changed

+94
-37
lines changed

3 files changed

+94
-37
lines changed

dist/main.js

+92-35
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.25.0
8+
// @version 0.26.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
@@ -358,10 +358,15 @@
358358
}
359359
};
360360
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);
362363
const data = yield r.arrayBuffer();
363364
return new Uint8Array(data);
364365
});
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+
});
365370
const assertRes = (r) => {
366371
if (!r.ok)
367372
throw new Error(`${r.url} ${r.status} ${r.statusText}`);
@@ -449,7 +454,7 @@
449454
};
450455

451456
const PDFWorker = function () {
452-
(function () {
457+
var worker = (function (exports) {
453458

454459
function __awaiter(thisArg, _arguments, P, generator) {
455460
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.\
2644426449
});
2644526450

2644626451
/// <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
2645526470
if (type === 'dataUrl') {
26456-
reader.readAsDataURL(blob);
26471+
return 'data:image/png;base64,' + data.toString('base64');
2645726472
}
2645826473
else {
26459-
reader.readAsText(blob);
26474+
return data.toString('utf-8');
2646026475
}
26461-
});
26476+
}
2646226477
};
26478+
/**
26479+
* @platform browser
26480+
*/
2646326481
const fetchBlob = (imgUrl) => __awaiter(void 0, void 0, void 0, function* () {
2646426482
const r = yield fetch(imgUrl, {
2646526483
cache: 'no-cache',
2646626484
});
2646726485
return r.blob();
2646826486
});
26487+
/**
26488+
* @example
26489+
* import { PDFWorker } from '../dist/cache/worker'
26490+
* const { generatePDF } = PDFWorker()
26491+
* const pdfData = await generatePDF(...)
26492+
*/
2646926493
const generatePDF = (imgBlobs, imgType, width, height) => __awaiter(void 0, void 0, void 0, function* () {
2647026494
// @ts-ignore
2647126495
const pdf = new PDFDocument({
@@ -26498,20 +26522,35 @@ Please pipe the document into a Node stream.\
2649826522
const buf = yield pdf.getBuffer();
2649926523
return buf.buffer;
2650026524
});
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+
}
2650726536

26508-
}());
26537+
exports.generatePDF = generatePDF;
26538+
26539+
return exports;
26540+
26541+
}({}));
26542+
return worker
2650926543
};
2651026544

2651126545
const scriptUrlFromFunction = (fn) => {
2651226546
const blob = new Blob(['(' + fn.toString() + ')()'], { type: 'application/javascript' });
2651326547
return window.URL.createObjectURL(blob);
2651426548
};
26549+
// Node.js fix
26550+
if (typeof Worker === 'undefined') {
26551+
globalThis.Worker = class {
26552+
}; // noop shim
26553+
}
2651526554
class PDFWorkerHelper extends Worker {
2651626555
constructor() {
2651726556
const url = scriptUrlFromFunction(PDFWorker);
@@ -26706,20 +26745,19 @@ Please pipe the document into a Node stream.\
2670626745
return info.url;
2670726746
});
2670826747

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* () {
2671626749
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);
2671826751
worker.terminate();
26719-
pdfBlob = new Blob([pdfArrayBuffer]);
26720-
FileSaver_min.saveAs(pdfBlob, `${name}.pdf`);
26752+
return pdfArrayBuffer;
2672126753
});
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* () {
2672326761
const imgType = sheet.imgType;
2672426762
const pageCount = sheet.pageCount;
2672526763
const rs = Array.from({ length: pageCount }).map((_, i) => {
@@ -26731,7 +26769,23 @@ Please pipe the document into a Node stream.\
2673126769
}
2673226770
});
2673326771
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`);
2673526789
});
2673626790

2673726791
const MSCZ_BUF_SYM = Symbol('msczBufferP');
@@ -27449,11 +27503,14 @@ Please pipe the document into a Node stream.\
2744927503
}
2745027504
get thumbnailUrl() {
2745127505
var _a;
27452-
// url to the image of the first page
2745327506
const el = this.document.querySelector('link[as=image]');
2745427507
const url = ((el === null || el === void 0 ? void 0 : el.href) || ((_a = this.sheet0Img) === null || _a === void 0 ? void 0 : _a.src));
2745527508
return url.split('@')[0];
2745627509
}
27510+
get dimensions() {
27511+
const { naturalWidth: width, naturalHeight: height } = this.sheet0Img;
27512+
return { width, height };
27513+
}
2745727514
}
2745827515
const getActualId = (scoreinfo, _fetch = getFetch()) => __awaiter(void 0, void 0, void 0, function* () {
2745927516
if (scoreinfo.id <= 1000000000000) {
@@ -27497,7 +27554,7 @@ Please pipe the document into a Node stream.\
2749727554
});
2749827555
btnList.add({
2749927556
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 */),
2750127558
});
2750227559
btnList.add({
2750327560
name: i18n('DOWNLOAD')('MXL'),

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.25.0",
3+
"version": "0.26.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)