Skip to content

Commit

Permalink
U
Browse files Browse the repository at this point in the history
  • Loading branch information
xuming212 authored Nov 16, 2024
1 parent 25c2b83 commit fc1f864
Showing 1 changed file with 41 additions and 50 deletions.
91 changes: 41 additions & 50 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,65 +41,56 @@ function hideLoading() {
document.getElementById('loadingIndicator').style.display = 'none';
}

function fetchFileInfo(url) {
fetch(url)
.then(response => response.json())
.then(data => {
window.fileinfo = data; // 假设服务器返回的数据格式与 fileinfo 相同
updateFileInfo();
})
.catch(error => {
console.error('获取文件信息失败:', error);
document.getElementById('linkInfo').textContent = "无法提取信息,请检查链接格式!";
});
}

function updateFileInfo() {
const outputElement = document.getElementById('linkInfo');
const fileInfo = parseFileInfo();

if (fileInfo) {
outputElement.innerHTML = `
文件名:${fileInfo.name}<br>
文件大小:${fileInfo.size}<br>
文件类型:${fileInfo.type}<br>
下载链接:<a href="${fileInfo.download}" target="_blank">点击下载</a>`;
} else {
outputElement.textContent = "无法提取信息,请检查链接格式!";
}
}

function extractFileInfo() {
async function extractFileInfo() {
const inputUrl = document.getElementById('inputUrl').value.trim();
const outputElement = document.getElementById('linkInfo');

console.log("输入的链接是:", inputUrl);

if (!inputUrl) {
document.getElementById('linkInfo').textContent = "请输入链接!";
outputElement.textContent = "请输入链接!";
return;
}

// 调用 fetchFileInfo 以获取数据
fetchFileInfo(inputUrl);
}
// 显示加载动画
showLoading();

function parseFileInfo() {
// 确保 fileinfo 是全局变量
if (!window.fileinfo) {
console.error("fileinfo 对象未定义");
return null;
}
try {
// 从链接中提取 objectId
const match = inputUrl.match(/objectId=([^&]+)/);
if (!match) {
outputElement.textContent = "无法识别的链接格式!";
hideLoading();
return;
}

const objectId = match[1];
const apiUrl = `https://sharewh.chaoxing.com/share/info/${objectId}`;

const name = fileinfo.name;
const size = (fileinfo.filesize / (1024 * 1024)).toFixed(2) + ' MB'; // 将字节转换为 MB
const type = fileinfo.suffix;
const download = fileinfo.download;
// 发送请求获取文件信息
const response = await fetch(apiUrl);
const data = await response.json();

console.log("提取的信息:", { name, size, type, download });
if (data.status === false) {
outputElement.textContent = "获取文件信息失败,请检查链接是否有效!";
} else {
const fileInfo = {
name: data.data.name || '未知',
size: ((data.data.filesize || 0) / (1024 * 1024)).toFixed(2) + ' MB',
type: data.data.suffix || '未知',
download: `https://sharewh.chaoxing.com/share/download/${objectId}`
};

return {
name: name,
size: size,
type: type,
download: download
};
outputElement.innerHTML = `
文件名:${fileInfo.name}<br>
文件大小:${fileInfo.size}<br>
文件类型:${fileInfo.type}<br>
下载链接:<a href="${fileInfo.download}" target="_blank">点击下载</a>`;
}
} catch (error) {
console.error("获取文件信息时出错:", error);
outputElement.textContent = "获取文件信息时发生错误,请稍后重试!";
} finally {
hideLoading();
}
}

0 comments on commit fc1f864

Please sign in to comment.