Books download

Added download button for audio book

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Books download
// @namespace    http://tampermonkey.net/
// @version      2024-05-28
// @description  Added download button for audio book
// @author       Dead4W
// @match        https://knigavuhe.info/*
// @license      MIT
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const downloadButton = document.createElement("a");
    downloadButton.innerText = "Download mp3"
    downloadButton.style = "color: black;cursor: pointer; margin: 3px; display: block";

    function init() {
        const videoElem = document.querySelector("#player video");

        if (!videoElem) {
            setTimeout(init, 100);
            return;
        }

        downloadButton.onclick = () => {downloadUrl(videoElem.src)};
        document.querySelector("#oframeplayer").appendChild(downloadButton);
    }

    function downloadUrl(url) {
        const options = {
            headers: {
                Referer: location.origin,
            }
        };

        downloadButton.style = "color: black;cursor: progress; margin: 3px; display: block";

        fetch(url, options)
            .then( res => res.blob() )
            .then( blob => {
                var file = window.URL.createObjectURL(blob);
                window.open(file, '_blank');
                downloadButton.style = "color: black;cursor: pointer; margin: 3px; display: block";
            });
    }

    $(document).ready(function() {
        init();
    });
})();