リンクのPDFファイルをzipしてダウンロード

    @@ -39,12 +39,14 @@ let blob = await response.blob(); blob = blob.slice(0, blob.size, "application/pdf") + let filename = element.textContent.trim(); + if (zip.file('files/' + filename)) { filename = Number(new Date()) + ':' + filename }; zip.file('files/' + fileName, blob); }; for (let target of document.querySelectorAll('a[href*=".pdf"] ')) { await downloader(target).catch(() => { }) }; - for (let target of [...document.querySelectorAll('a')].filter(e => e.textContent.includes('pdf') || e.textContent.includes('PDF'))) { await downloader(target).catch(() => { }) }; + for (let target of [...document.querySelectorAll('a')].filter(e => e.textContent.includes('pdf') || e.textContent.includes('PDF'))) { await downloader(target).catch(e => { throw e }) }; const content = await zip.generateAsync({ type: "blob" }); saveAs(content, 'files.zip');
  • /*
     * @title リンクのPDFファイルをzipしてダウンロード
     * @description ページ内にある~.PDFというリンクすべてからPDFファイルを取得して、zipして一括ダウンロードします。
     * @include http://*
     * @license MIT License
     * @javascript_url
     */
    
    
    (async () => {
        function _import(s) {
            return new Promise(function (resolve) {
                const script = document.createElement('script');
                script.src = s;
                script.onload = function () {
                    resolve();
                };
                document.head.appendChild(script);
            })
        };
    
        const sleep = time => new Promise(resolve => setTimeout(resolve, time));
        await import('https://cdnjs.cloudflare.com/ajax/libs/jszip/3.2.0/jszip.min.js');
        await _import('https://cdn.jsdelivr.net/npm/file-saver@2.0.2/dist/FileSaver.min.js');
    
        const zip = new JSZip();
    
        async function downloader(element) {
            const response = await fetch(
                element.href,
            );
            element.style.backgroundColor = 'gray';
            const fileName = decodeURIComponent(
                element.href.match(/(?=[^\/]+\.pdf)[^\/]+\.pdf/) ??
                element.textContent.match(/(?=[^\/]+\.pdf)[^\/]+\.pdf/)
            );
            console.log(fileName);
            await sleep(2000);
    
            let blob = await response.blob();
            blob = blob.slice(0, blob.size, "application/pdf")
            let filename = element.textContent.trim();
            if (zip.file('files/' + filename)) { filename = Number(new Date()) + ':' + filename };
            zip.file('files/' + fileName, blob);
    
        };
    
        for (let target of document.querySelectorAll('a[href*=".pdf"] ')) { await downloader(target).catch(() => { }) };
        for (let target of [...document.querySelectorAll('a')].filter(e => e.textContent.includes('pdf') || e.textContent.includes('PDF'))) { await downloader(target).catch(e => { throw e }) };
        const content = await zip.generateAsync({ type: "blob" });
        saveAs(content, 'files.zip');
    
    })()
  • Permalink
    このページへの個別リンクです。
    RAW
    書かれたコードへの直接のリンクです。
    Packed
    文字列が圧縮された書かれたコードへのリンクです。
    Userscript
    Greasemonkey 等で利用する場合の .user.js へのリンクです。
    Loader
    @require やソースコードが長い場合に多段ロードする Loader コミのコードへのリンクです。
    Metadata
    コード中にコメントで @xxx と書かれたメタデータの JSON です。

History

  1. 2023/06/30 11:41:04 - 2023-06-30
  2. 2022/05/12 11:57:52 - 2022-05-12
  3. 2022/04/21 17:39:55 - 2022-04-21
  4. 2021/05/11 11:55:06 - 2021-05-11
  5. 2020/10/21 17:59:00 - 2020-10-21
  6. 2020/02/07 14:06:17 - 2020-02-07
  7. 2020/02/05 15:53:15 - 2020-02-05
  8. 2020/02/04 18:07:05 - 2020-02-04
  9. 2020/02/04 17:51:38 - 2020-02-04
  10. 2020/01/11 10:34:49 - 2020-01-11