(folderName, fileName, zip = null)
| 1945 | |
| 1946 | // 下载并展示文件 |
| 1947 | async function downloadFile(folderName, fileName, zip = null) { |
| 1948 | return new Promise((resolve, reject) => { |
| 1949 | // 发起 GET 请求下载文件内容 |
| 1950 | const GET = { |
| 1951 | method: 'GET', |
| 1952 | path: folderName + '/' + fileName, |
| 1953 | success: (xhr) => { |
| 1954 | const fileContent = xhr.responseText |
| 1955 | const jsonData = JSON.parse(fileContent) |
| 1956 | const content = generateJsonIndexContent(jsonData) |
| 1957 | |
| 1958 | if (zip) { |
| 1959 | // 如果传入了压缩包实例,则将文件内容添加到压缩包中 |
| 1960 | const sanitizedFileName = sanitizeFileName(fileName.replace('.json', '')) + '.html' |
| 1961 | zip.file(sanitizedFileName, content) |
| 1962 | resolve() |
| 1963 | } else { |
| 1964 | // 否则直接下载文件 |
| 1965 | const blob = new Blob([content], { type: 'text/html' }) |
| 1966 | const htmlUrl = URL.createObjectURL(blob) |
| 1967 | const a = document.createElement('a') |
| 1968 | a.href = htmlUrl |
| 1969 | a.download = sanitizeFileName(fileName.replace('.json', '')) + '.html' |
| 1970 | a.click() |
| 1971 | resolve() |
| 1972 | } |
| 1973 | }, |
| 1974 | fail: (xhr) => { |
| 1975 | reject(new Error('下载文件失败:' + xhr.status)) |
| 1976 | } |
| 1977 | } |
| 1978 | |
| 1979 | GM_xhr({ ...GET }) |
| 1980 | }) |
| 1981 | } |
| 1982 | |
| 1983 | async function downloadAndDisplayFile(folderName, fileName) { |
| 1984 | const GET = { |
no test coverage detected