Skip to content

Commit 37bbd05

Browse files
authored
Update worker.js
1 parent c8187d7 commit 37bbd05

File tree

1 file changed

+64
-73
lines changed

1 file changed

+64
-73
lines changed

worker.js

+64-73
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ function handleRootRequest() {
105105
$(document).ready(function() {
106106
let originalImageURL = '';
107107
108-
// 初始化文件上传
108+
// 初始化文件上传
109109
initFileInput();
110-
110+
111111
// 文件上传初始化函数
112112
function initFileInput() {
113113
$("#fileInput").fileinput({
@@ -127,8 +127,8 @@ $(document).ready(function() {
127127
}).on('filebatchselected', handleFileSelection)
128128
.on('fileclear', handleFileClear);
129129
}
130-
131-
// 处理接口选择器变更事件
130+
131+
// 处理接口选择器变更事件
132132
$('#interfaceSelector').change(function() {
133133
const selectedInterface = $(this).val();
134134
let acceptTypes = '';
@@ -140,12 +140,12 @@ $(document).ready(function() {
140140
}
141141
$('#fileInput').attr('accept', acceptTypes);
142142
}).trigger('change');
143-
144-
// 处理文件选择事件
143+
144+
// 处理文件选择事件
145145
async function handleFileSelection() {
146146
const selectedInterface = $('#interfaceSelector').val();
147147
const file = $('#fileInput')[0].files[0];
148-
148+
149149
if (file && file.size > 5 * 1024 * 1024) {
150150
if (file.type === 'image/gif') {
151151
toastr.error('GIF 文件必须≤5MB');
@@ -155,13 +155,13 @@ $(document).ready(function() {
155155
}
156156
return;
157157
}
158-
158+
159159
if (file && (file.type === 'image/gif' || file.type.includes('image/'))) {
160160
await uploadFile(file);
161161
}
162162
}
163-
164-
// 处理上传文件函数
163+
164+
// 处理上传文件函数
165165
async function uploadFile(file) {
166166
try {
167167
$('#uploadingText').show();
@@ -179,7 +179,42 @@ $(document).ready(function() {
179179
$('#uploadingText').hide();
180180
}
181181
}
182-
182+
183+
//处理图片压缩事件
184+
async function compressImage(file, quality = 0.6, maxResolution = 20000000) {
185+
$('#compressingText').show();
186+
187+
return new Promise((resolve) => {
188+
const image = new Image();
189+
image.onload = () => {
190+
const width = image.width;
191+
const height = image.height;
192+
const resolution = width * height;
193+
let scale = 1;
194+
if (resolution > maxResolution) {
195+
scale = Math.sqrt(maxResolution / resolution);
196+
}
197+
const targetWidth = Math.round(width * scale);
198+
const targetHeight = Math.round(height * scale);
199+
const canvas = document.createElement('canvas');
200+
const ctx = canvas.getContext('2d');
201+
canvas.width = targetWidth;
202+
canvas.height = targetHeight;
203+
ctx.drawImage(image, 0, 0, targetWidth, targetHeight);
204+
canvas.toBlob((blob) => {
205+
const compressedFile = new File([blob], file.name, { type: 'image/jpeg' });
206+
$('#compressingText').hide();
207+
resolve(compressedFile);
208+
}, 'image/jpeg', quality);
209+
};
210+
const reader = new FileReader();
211+
reader.onload = (event) => {
212+
image.src = event.target.result;
213+
};
214+
reader.readAsDataURL(file);
215+
});
216+
}
217+
183218
// 处理上传响应函数
184219
async function handleUploadResponse(response) {
185220
if (response.ok) {
@@ -189,48 +224,45 @@ $(document).ready(function() {
189224
return '文件上传失败!';
190225
}
191226
}
192-
193-
// 处理按钮点击事件
227+
228+
// 处理按钮点击事件
194229
$('#urlBtn, #bbcodeBtn, #markdownBtn').on('click', function() {
195230
const fileLink = originalImageURL.trim();
196231
if (fileLink !== '') {
197232
let formattedLink;
198233
switch ($(this).attr('id')) {
199234
case 'urlBtn':
200235
formattedLink = fileLink;
201-
break
202-
case 'urlBtn':
203-
formattedLink = fileLink;
204-
break;
205-
case 'bbcodeBtn':
206-
formattedLink = '[img]' + fileLink + '[/img]';
207-
break;
208-
case 'markdownBtn':
209-
formattedLink = '![image](' + fileLink + ')';
210-
break;
211-
default:
212-
formattedLink = fileLink;
236+
break;
237+
case 'bbcodeBtn':
238+
formattedLink = '[img]' + fileLink + '[/img]';
239+
break;
240+
case 'markdownBtn':
241+
formattedLink = '![image](' + fileLink + ')';
242+
break;
243+
default:
244+
formattedLink = fileLink;
213245
}
214246
$('#fileLink').val(formattedLink);
215247
adjustTextareaHeight($('#fileLink')[0]);
216248
copyToClipboardWithToastr(formattedLink);
217249
}
218250
});
219-
220-
// 处理移除按钮点击事件
251+
252+
// 处理移除按钮点击事件
221253
function handleFileClear(event) {
222254
$('#fileLink').val('');
223255
adjustTextareaHeight($('#fileLink')[0]);
224256
hideButtonsAndTextarea();
225257
}
226-
258+
227259
// 自动调整文本框高度函数
228260
function adjustTextareaHeight(textarea) {
229261
textarea.style.height = '1px';
230262
textarea.style.height = (textarea.scrollHeight) + 'px';
231263
}
232-
233-
// 复制文本到剪贴板,并显示 toastr 提示框
264+
265+
// 复制文本到剪贴板,并显示 toastr 提示框
234266
function copyToClipboardWithToastr(text) {
235267
const input = document.createElement('input');
236268
input.setAttribute('value', text);
@@ -240,53 +272,12 @@ $(document).ready(function() {
240272
document.body.removeChild(input);
241273
toastr.success('已复制到剪贴板', '', { timeOut: 300 });
242274
}
243-
275+
244276
// 隐藏按钮和文本框
245277
function hideButtonsAndTextarea() {
246278
$('#urlBtn, #bbcodeBtn, #markdownBtn, #fileLink').parent('.form-group').hide();
247279
}
248-
249-
// 处理图片压缩事件
250-
async function compressImage(file, quality = 0.6, maxResolution = 20000000) {
251-
$('#compressingText').show();
252-
253-
return new Promise((resolve) => {
254-
const image = new Image();
255-
image.onload = () => {
256-
const width = image.width;
257-
const height = image.height;
258-
259-
const resolution = width * height;
260-
261-
let scale = 1;
262-
if (resolution > maxResolution) {
263-
scale = Math.sqrt(maxResolution / resolution);
264-
}
265-
266-
const targetWidth = Math.round(width * scale);
267-
const targetHeight = Math.round(height * scale);
268-
269-
const canvas = document.createElement('canvas');
270-
const ctx = canvas.getContext('2d');
271-
272-
canvas.width = targetWidth;
273-
canvas.height = targetHeight;
274-
ctx.drawImage(image, 0, 0, targetWidth, targetHeight);
275-
276-
canvas.toBlob((blob) => {
277-
const compressedFile = new File([blob], file.name, { type: 'image/jpeg' });
278-
$('#compressingText').hide();
279-
resolve(compressedFile);
280-
}, 'image/jpeg', quality);
281-
};
282-
283-
const reader = new FileReader();
284-
reader.onload = (event) => {
285-
image.src = event.target.result;
286-
};
287-
reader.readAsDataURL(file);
288-
});
289-
}
280+
290281
});
291282
292283
</script>

0 commit comments

Comments
 (0)