image.png

    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <meta charset="UTF-8">
    5. <!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
    6. <title>Hello World!</title>
    7. </head>
    8. <body>
    9. <h1>Hello World!</h1>
    10. We are using Node.js <span id="node-version"></span>,
    11. Chromium <span id="chrome-version"></span>,
    12. and Electron <span id="electron-version"></span>.
    13. <br />
    14. <button id="download">Download</button>
    15. <h4 id="process"></h4>
    16. <!-- You can also require other files to run in this process -->
    17. <script src="./renderer.js"></script>
    18. </body>
    19. </html>
    1. // Modules to control application life and create native browser window
    2. const { app, BrowserWindow } = require('electron')
    3. const path = require('path')
    4. function createWindow () {
    5. // Create the browser window.
    6. const mainWindow = new BrowserWindow({
    7. width: 800,
    8. height: 600,
    9. webPreferences: {
    10. preload: path.join(__dirname, 'preload.js')
    11. }
    12. })
    13. // and load the index.html of the app.
    14. mainWindow.loadFile('index.html')
    15. // Open the DevTools.
    16. // mainWindow.webContents.openDevTools()
    17. }
    18. // This method will be called when Electron has finished
    19. // initialization and is ready to create browser windows.
    20. // Some APIs can only be used after this event occurs.
    21. app.whenReady().then(() => {
    22. createWindow()
    23. app.on('activate', function () {
    24. // On macOS it's common to re-create a window in the app when the
    25. // dock icon is clicked and there are no other windows open.
    26. if (BrowserWindow.getAllWindows().length === 0) createWindow()
    27. })
    28. })
    29. // Quit when all windows are closed, except on macOS. There, it's common
    30. // for applications and their menu bar to stay active until the user quits
    31. // explicitly with Cmd + Q.
    32. app.on('window-all-closed', function () {
    33. if (process.platform !== 'darwin') app.quit()
    34. })
    35. // In this file you can include the rest of your app's specific main process
    36. // code. You can also put them in separate files and require them here.
    1. {
    2. "name": "video-download",
    3. "productName": "video-download",
    4. "description": "My Electron application description",
    5. "keywords": [],
    6. "main": "./main.js",
    7. "version": "1.0.0",
    8. "author": "81492",
    9. "scripts": {
    10. "start": "electron ."
    11. },
    12. "dependencies": {},
    13. "devDependencies": {
    14. "electron": "20.0.3"
    15. }
    16. }
    1. // All of the Node.js APIs are available in the preload process.
    2. // It has the same sandbox as a Chrome extension.
    3. window.addEventListener('DOMContentLoaded', () => {
    4. const replaceText = (selector, text) => {
    5. const element = document.getElementById(selector)
    6. if (element) element.innerText = text
    7. }
    8. for (const type of ['chrome', 'node', 'electron']) {
    9. replaceText(`${type}-version`, process.versions[type])
    10. }
    11. })
    1. // This file is required by the index.html file and will
    2. // be executed in the renderer process for that window.
    3. // No Node.js APIs are available in this process because
    4. // `nodeIntegration` is turned off. Use `preload.js` to
    5. // selectively enable features needed in the rendering
    6. // process.
    7. const download = document.querySelector('#download')
    8. download.addEventListener('click', () => {
    9. down('https://mdn.github.io/learning-area/html/multimedia-and-embedding/video-and-audio-content/rabbit320.mp4')
    10. })
    11. const process = document.querySelector('#process')
    12. function down(url) {
    13. // 1. 创建一个 new XMLHttpRequest 对象
    14. let xhr = new XMLHttpRequest();
    15. xhr.responseType = 'blob'
    16. // 2. 配置它:从 URL /article/.../load GET-request
    17. xhr.open('GET', url);
    18. xhr.setRequestHeader('token', 'haha');
    19. // 3. 通过网络发送请求
    20. xhr.send();
    21. // 4. 当接收到响应后,将调用此函数
    22. xhr.onload = function () {
    23. if (xhr.status != 200) { // 分析响应的 HTTP 状态
    24. alert(`Error ${xhr.status}: ${xhr.statusText}`); // 例如 404: Not Found
    25. } else { // 显示结果
    26. // alert(`Done, got ${xhr.response.length} bytes`); // response 是服务器响应
    27. debugger
    28. console.log(`Done, got ${xhr.response.size} bytes`, xhr.response)
    29. const downloadA = document.createElement('a');
    30. const blob = xhr.response
    31. const URL = window.URL || window.webkitURL
    32. const herf = URL.createObjectURL(blob)
    33. downloadA.href = herf
    34. downloadA.download = url;
    35. downloadA.click();
    36. window.URL.revokeObjectURL(herf)
    37. }
    38. };
    39. xhr.onprogress = function (event) {
    40. if (event.lengthComputable) {
    41. // alert(`Received ${event.loaded} of ${event.total} bytes`);
    42. console.log(`Received ${event.loaded} of ${event.total} bytes`)
    43. process.innerHTML = ((event.loaded / event.total) * 100) + '%'
    44. } else {
    45. console.log(`Received ${event.loaded} bytes`, '没有 Content-Length')
    46. // alert(`Received ${event.loaded} bytes`); // 没有 Content-Length
    47. }
    48. };
    49. xhr.onerror = function () {
    50. alert("Request failed");
    51. };
    52. }

    使用
    打开链接
    https://gist.github.com/liulinboyi/1a8a08763d1b12b3169f831dc0aa8de1