master
parent
f9114990e0
commit
7ff0826521
8 changed files with 243 additions and 79 deletions
@ -0,0 +1,24 @@ |
||||
const fs = require('fs'); |
||||
|
||||
// const fetch = require('node-fetch'); // Install this via npm
|
||||
|
||||
|
||||
async function downloadFile(url, path) { |
||||
try { |
||||
const response = await fetch(url); |
||||
if (!response.ok){ |
||||
throw new Error(`HTTP error! Status: ${response.status}`); |
||||
} |
||||
const arrayBuffer = await response.arrayBuffer(); |
||||
const buffer = Buffer.from(arrayBuffer); |
||||
fs.writeFileSync(path, buffer); |
||||
console.log('File downloaded and saved successfully.'); |
||||
} catch (error) { |
||||
console.error('Error downloading the file:', error); |
||||
} |
||||
} |
||||
|
||||
const downloadUrl = 'https://app.yunnan.chinatax.gov.cn/GVv7ud1ebech/MO5zzCMcub4d.b4c45da.js'; // Replace with your file URL
|
||||
const filePath = './downloadedFile.js'; // Desired file path
|
||||
|
||||
downloadFile(downloadUrl, filePath); |
@ -0,0 +1,63 @@ |
||||
let fs = require('node:fs'); |
||||
const path = require("node:path"); |
||||
|
||||
|
||||
class JsUtil { |
||||
// 加载 js 文本
|
||||
static async loadJs(document, areaName, cookie) { |
||||
let js = ''; |
||||
// 加载 页面上的js
|
||||
const allScript = document.querySelectorAll('script[r="m"]'); |
||||
for (let i = 0; i < allScript.length; i++) { |
||||
const script = allScript[i]; |
||||
let attr = script.textContent; |
||||
if (attr) { |
||||
js += attr |
||||
} else { |
||||
//获取script @src 属性
|
||||
let jsSrc = script.src; |
||||
let url = new URL(jsSrc); |
||||
let fileName = areaName + url.pathname.replaceAll('/', '.'); |
||||
let filePath = path.resolve(__dirname, `../public/static/js/`); |
||||
let existDir = fs.existsSync(filePath); |
||||
if (!existDir) { |
||||
fs.mkdirSync(filePath, {recursive: true}); |
||||
} |
||||
let file =path.resolve(filePath, fileName); |
||||
let existFile = fs.existsSync(file); |
||||
let jsText; |
||||
if (existFile) { |
||||
let buffer = fs.readFileSync(file); |
||||
jsText = buffer.toString('utf8') |
||||
} else { |
||||
// 文件下载
|
||||
jsText = await this.downloadJs(jsSrc, filePath, fileName, cookie) |
||||
} |
||||
js += jsText |
||||
} |
||||
js += ";\n" |
||||
} |
||||
return js; |
||||
} |
||||
|
||||
|
||||
static async downloadJs(downloadUrl, filePath, fileName) { |
||||
let url = new URL(downloadUrl); |
||||
const response = await fetch(url); |
||||
if (!response.ok) { |
||||
throw new Error(`HTTP error! Status: ${response.status}`); |
||||
} |
||||
const arrayBuffer = await response.arrayBuffer(); |
||||
const buffer = Buffer.from(arrayBuffer); |
||||
let file = path.resolve(filePath, fileName); |
||||
fs.writeFile(file, buffer, {encoding: 'utf8'}, (err) => { |
||||
if (err){ |
||||
console.error(`文件${file}写入失败 ---> ${err}`) |
||||
} |
||||
}) |
||||
console.log('File downloaded and saved successfully.'); |
||||
return buffer.toString('utf8') |
||||
} |
||||
} |
||||
|
||||
module.exports = JsUtil |
Loading…
Reference in new issue