Greasy Fork

zip解压工具

传入zip文件链接,返回其中第一个txt文件中的内容。

目前为 2023-09-22 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.greasyfork.cloud/scripts/475748/1254627/zip%E8%A7%A3%E5%8E%8B%E5%B7%A5%E5%85%B7.js

// ==UserScript==
// @name         zip解压工具
// @namespace    unzipandreadtext
// @version      1.0.0
// @description  传入zip文件链接,返回其中第一个txt文件中的内容。
// @author       sehuatang_chen
// @license      MIT

// @grant        GM_xmlhttpRequest
// ==/UserScript==
(function(){
    let script = document.createElement('script');
    script.setAttribute('type', 'text/javascript');
    script.src = "https://cdn.bootcdn.net/ajax/libs/jszip/3.9.1/jszip.min.js";
    document.documentElement.appendChild(script);
})()
headers={
    'User-agent': navigator.userAgent,
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
}
//下载并解压入参地址的zip文件。不支持密码。返回Promise
function readZip(link) {
    return new Promise((resolve, reject) => {
        const jszip = new JSZip()
        GM_xmlhttpRequest({
            method: 'GET',
            url: link,
            headers: headers,
            responseType: 'blob',
            onload: (result) => {
                if (result.status === 200 || result.status === 304) {
                    jszip.loadAsync(result.response).then(zip => resolve(zip)).catch(err => reject(err));          
                }else{
                    reject("download err.")
                }
            }
        });
    })
}
//下载并解压入参地址的zip文件,文本方式读取其中第一个文件的内容。不支持密码。返回Promise
// readfirsttxtfileinzip(zipdownloadlink).then( text => {
//     console.log(text)
// }).catch( error => {
//     console.log(error)
// })
function readfirsttxtfileinzip(link){
    return readZip(link).then(zip => {
        for (let key in zip.files) {
            if (!zip.files[key].dir) {
                return zip.file(zip.files[key].name).async('string')
            }
        }  
    })
}