青丘白泽 / 追梦之旅铺路卷 音频下载

// ==UserScript==
// @name         追梦之旅铺路卷 音频下载
// @namespace    https://mp.bookln.cn/
// @version      2024-03-22
// @description  追梦之旅铺路卷 音频下载
// @author       青丘白泽
// @copyright    2023, 青丘白泽 (https://openuserjs.org/users/青丘白泽)
// @license      Apache-2.0
// @match        https://mp.bookln.cn/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// ==/UserScript==


(function() {
    'use strict';

    // Your code here...
    // 等待网页完成加载
    window.addEventListener('load', function() {
        // 加载完成后执行的代码
        var url = document.getElementById('audio_media').src;
        var title = document.title;
        if(url) {
            console.log(`%c${title}`, 'color: green;');
            console.log(`%c${url}`, 'color: green;');
            // 下载含有url的文件
            function downloadUrlFile(url, fileName) {
                const urlObject = window.URL || window.webkitURL || window;
                const url2 = url.replace(/\\/g, '/');
                const xhr = new XMLHttpRequest();
                xhr.open('GET', url2, true);
                xhr.responseType = 'blob';
                //xhr.setRequestHeader('Authorization', 'Basic a2VybWl0Omtlcm1pdA==');
                // 为了避免大文件影响用户体验,建议加loading
                xhr.onload = () => {
                    if (xhr.status === 200) {
                        // 获取文件blob数据并保存
                        // saveAs(xhr.response, fileName);
                        const export_blob = new Blob([xhr.response]);
                        const save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
                        save_link.href = urlObject.createObjectURL(export_blob);
                        save_link.download = fileName;
                        save_link.click();
                    }
                };
                xhr.send();
            }
            downloadUrlFile(url, `${title}.mp3`)
            //FileSaver.saveAs(url, `${title}.mp3`);
            //window.open(url)
        };
    }, false);
})();