-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrename.js
81 lines (63 loc) · 2.66 KB
/
rename.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
let renamedFiles = []; // 全局變數
document.getElementById('openfile-rename').addEventListener('click', function() {
document.getElementById('fileInput-rename').click();
});
document.getElementById('openfile2-rename').addEventListener('click', function() {
document.getElementById('fileInput-rename').click();
});
document.getElementById('delete').addEventListener('click', function() {
// 恢復預設顯示狀態
document.getElementById('openfile-rename').style.display = 'block';
document.getElementById('openfile2-rename').style.display = 'none';
document.getElementById('download-all').style.display = 'none';
document.getElementById('delete').style.display = 'none';
// 清空文件選擇器
document.getElementById('fileInput-rename').value = '';
// 清除已重命名的文件列表
renamedFiles = [];
// 清空預覽區
document.querySelector('.overview').innerHTML = '';
});
document.getElementById('fileInput-rename').addEventListener('change', function() {
// 切換按鈕顯示狀態
document.getElementById('openfile-rename').style.display = 'none';
document.getElementById('openfile2-rename').style.display = 'block';
document.getElementById('download-all').style.display = 'block';
document.getElementById('delete').style.display = 'block';
// 獲取選擇的文件
const files = this.files;
// 清空之前的重命名文件
renamedFiles = [];
const renamePattern = /[a-zA-Z]\d{9}/;
// 重新命名文件
for (const file of files) {
let newName = file.name.match(renamePattern);
if (newName) {
newName = newName[0] + file.name.substring(file.name.lastIndexOf('.')); // 保留副檔名
} else {
newName = file.name;
}
renamedFiles.push(new File([file], newName, { type: file.type }));
}
// 更新預覽區
const overviewDiv = document.querySelector('.overview');
overviewDiv.innerHTML = '';
renamedFiles.forEach(file => {
const div = document.createElement('div');
div.textContent = file.name;
overviewDiv.appendChild(div);
});
document.getElementById('download-all').addEventListener('click', function() {
const zip = new JSZip();
renamedFiles.forEach(file => {
zip.file(file.name, file);
});
zip.generateAsync({ type: "blob" })
.then(function(content) {
const a = document.createElement('a');
a.href = URL.createObjectURL(content);
a.download = '重設檔案名稱後的所有照片.zip';
a.click();
});
});
});