画面上のPDFリンクを一括ダウンロードするスクリプト
企業のIR情報をGoogleのNotebookLMにアップロードして分析させようと思い、IRから決算短信や決算説明資料をダウンロードしようとしたのですが、数が多く一つ一つ手でダウンロードするのが手間だったので、一括ダウンロードするスクリプトをchatGPT君に作ってもらいました。
PDFリンクのある画面で、以下のコードをブラウザの開発者ツール(F12)のコンソールに貼り付ければ一括ダウンロードしてくれます。
(function() {
// Collect all anchor elements on the page
const anchors = document.querySelectorAll('a[href]');
// Filter for anchors containing .pdf in the href
const pdfLinks = Array.from(anchors).filter(a => a.href.includes('.pdf'));
if (pdfLinks.length === 0) {
alert('No PDF links found on this page.');
return;
}
// Download each PDF link without opening
pdfLinks.forEach(link => {
const url = link.href;
fetch(url)
.then(response => response.blob())
.then(blob => {
const blobUrl = URL.createObjectURL(blob);
const anchor = document.createElement('a');
anchor.href = blobUrl;
// Extract and clean the file name
let fileName = url.split('/').pop();
const pdfIndex = fileName.indexOf('.pdf');
if (pdfIndex !== -1) {
fileName = fileName.substring(0, pdfIndex + 4); // Keep only up to .pdf
}
anchor.download = fileName;
document.body.appendChild(anchor);
anchor.click();
document.body.removeChild(anchor);
URL.revokeObjectURL(blobUrl);
})
.catch(error => console.error('Download failed:', error));
});
alert(`Downloading ${pdfLinks.length} PDF(s).`);
})();
こういうのを数分で作れちゃうの便利な時代ですね。
こういう画面でスクリプトを実行する一括ダウンロードできます。
(PDFのみ対応)