GoFile 增强

在 GoFile 文件页面添加一个按钮,导出当前页面全部文件下载链接。用以配合 IDM、aria2 等下载器使用.

目前為 2024-11-01 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         GoFile 增强
// @name:en      GoFile User Script
// @namespace    https://github.com/ewigl/gofile-userscript
// @version      0.1.1
// @description  在 GoFile 文件页面添加一个按钮,导出当前页面全部文件下载链接。用以配合 IDM、aria2 等下载器使用.
// @description:en Add a button to export download links of current page files. Can be used in IDM, aria2 and similar downloaders.
// @author       Licht
// @license      MIT
// @homepage     https://github.com/ewigl/gofile-userscript
// @match        http*://gofile.io/d/*
// @icon         https://gofile.io/dist/img/favicon16.png
// @grant        GM_addStyle
// ==/UserScript==

;(function () {
    'use strict'

    const DEFAULT_LANGUAGE = 'en-US'

    const FOLDER_TYPE = 'folder'
    const FILE_TYPE = 'file'

    const BUTTON_TEXT = {
        'zh-CN': '导出全部下载链接',
        'en-US': 'Export all file links',
    }

    const utils = {
        getLanguage() {
            return navigator.language || DEFAULT_LANGUAGE
        },
    }

    const operations = {
        // downloadByLink(url) {
        //     // create a element then click
        //     console.log('url:', url)
        //     let element = document.createElement('a')
        //     element.href = url
        //     element.target = '_blank'
        //     element.click()
        // },
        // downloadAllFiles() {
        //     // Get global object "mainFolderObject"
        //     console.log('mainFolderObject,', mainFolderObject)
        //     if (mainFolderObject.children) {
        //         // 遍历对象
        //         Object.keys(mainFolderObject.children).forEach(async (key) => {
        //             const item = mainFolderObject.children[key]
        //             if (item.type === 'folder') {
        //                 // operations.downloadAllFiles(item)
        //                 console.log('folder found, skip...')
        //             } else if (item.type === 'file') {
        //                 console.log('file found, item:', item.name)
        //                 operations.downloadByLink(item.link)
        //             }
        //         })
        //     } else {
        //         console.log('No files to download...')
        //     }
        // },
        writeLinksToTxt() {
            console.log('mainFolderObject,', mainFolderObject)

            let links = ''

            if (mainFolderObject.children) {
                // 遍历对象
                Object.keys(mainFolderObject.children).forEach(async (key) => {
                    const item = mainFolderObject.children[key]
                    if (item.type === FOLDER_TYPE) {
                        console.log('Folder found, skip...')
                    } else if (item.type === FILE_TYPE) {
                        console.log('File found, item:', item.name)
                        links += item.link + '\n'
                    }
                })
            } else {
                console.log('No files to download...')
            }

            const blob = new Blob([links], { type: 'text/plain;charset=utf-8' })
            const url = URL.createObjectURL(blob)
            const link = document.createElement('a')
            link.href = url
            // generate file neme by timestamp
            link.download = mainFolderObject.name + ' - ' + new Date().getTime() + '.txt'
            link.click()
            URL.revokeObjectURL(url)
        },

        addButtonToSidebar() {
            const buttonDom = `
            <a href="javascript:void(0)">
                <div class="row justify-content-center rounded-pill sidebarItem mt-1 mb-1 hover" style="">
                    <div class="col-auto">
                    <span style="font-size: 1.5em"><i class="bi bi-cloud-download-fill"></i></span>
                    </div>
                    <div class="col sidebarMobile d-flex align-items-center" style="display: block;">
                    <span> ${BUTTON_TEXT[utils.getLanguage()]} </span>
                    </div>
                </div>
            </a>
            `

            const hrLine = document.createElement('hr')
            hrLine.classList.add('my-0')

            const buttonEle = document.createElement('div')
            buttonEle.innerHTML = buttonDom
            buttonEle.addEventListener('click', operations.writeLinksToTxt)

            // add to sidebar
            document.querySelector('#sidebar').appendChild(hrLine)
            document.querySelector('#sidebar').appendChild(buttonEle)
        },
    }

    const main = {
        init() {
            // add button to sidebar
            let interval = setInterval(() => {
                if (mainFolderObject.children) {
                    operations.addButtonToSidebar()
                    clearInterval(interval)
                }
            }, 640)
        },
    }

    main.init()
})()