Кейсы

Расширение Google Chrome v. 88

Возникла идея написать расширение для хрома, которое будет загружать слайды презентации с портала вебинаров BigBlueButton.

Расширение написано на JavaScript под браузер Google Chrome 88 версии.

Для начала нужно создать файл manifest.json, в котором будет прописаны основные параметры расширения

{
    "name": "Webinar",
    "description": "Download from Webinar.",
    "version": "0.1",
    "manifest_version": 2,
    "permissions": [
      "activeTab"
    ],
    "browser_action": {
      "default_popup": "Page.html"
    }
  }

В файле Page.html пропишем следующее

<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <div id="id1">-</div>
    <input type="hidden" value="" id="hid">
    <button class="classname">Save svg</button>
    <script type="text/javascript" src="popup.js"></script>
</body>

</html>

В основном файле popup.js

window.downloadFile = function (sUrl) {

    //iOS devices do not support downloading. We have to inform user about this.
    if (/(iP)/g.test(navigator.userAgent)) {
        alert('Your device do not support files downloading. Please try again in desktop browser.');
        return false;
    }

    //If in Chrome or Safari - download via virtual link click
    if (window.downloadFile.isChrome || window.downloadFile.isSafari) {
        //Creating new link node.
        var link = document.createElement('a');
        link.href = sUrl;

        if (link.download !== undefined) {
            //Set HTML5 download attribute. This will prevent file from opening if supported.
            var fileName = sUrl.substring(sUrl.lastIndexOf('/') + 1, sUrl.length);
            link.download = fileName;
        }

        //Dispatching click event.
        if (document.createEvent) {
            var e = document.createEvent('MouseEvents');
            e.initEvent('click', true, true);
            link.dispatchEvent(e);
            return true;
        }
    }

    // Force file download (whether supported by server).
    var query = '?download';

    window.open(sUrl + query, '_self');
}

window.downloadFile.isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
window.downloadFile.isSafari = navigator.userAgent.toLowerCase().indexOf('safari') > -1;

document.addEventListener("DOMContentLoaded", function (event) {
    var base = document.querySelector(".classname");

    base.addEventListener('click', function (event) {
        a = document.querySelector("#hid").value;
        ls = a.split("/")
        a = "";
        for (i = 0; i < ls.length - 1; i++) {
            a += ls[i] + "/";
        }
        
        for (i = 1; i <= 100; i++) {
            downloadFile(a + String(i))
        }
    })
});

var tab_title = '';
function display_h1(link) {
    a = link
    document.querySelector("#id1").innerHTML = "Downloads"
    document.querySelector("#id1").innerHTML = a;
    document.querySelector("#hid").value = a;
}
chrome.tabs.query({ active: true }, function (tabs) {
    var tab = tabs[0];
    tab_title = tab.title;
    chrome.tabs.executeScript(tab.id, {
        code: 'document.querySelector("image").getAttribute("xlink:href")'
    }, display_h1);
});