-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
71 lines (59 loc) · 2.42 KB
/
Copy pathscript.js
File metadata and controls
71 lines (59 loc) · 2.42 KB
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
document.addEventListener('DOMContentLoaded', () => {
const originalUrlInput = document.getElementById('originalUrl');
const shortenBtn = document.getElementById('shortenBtn');
const resultContainer = document.getElementById('resultContainer');
const shortenedUrlInput = document.getElementById('shortenedUrl');
const copyBtn = document.getElementById('copyBtn');
// TinyURL API를 사용하여 URL 단축
async function shortenUrl(url) {
try {
const response = await fetch(`https://tinyurl.com/api-create.php?url=${encodeURIComponent(url)}`);
if (!response.ok) throw new Error('URL 단축에 실패했습니다.');
return await response.text();
} catch (error) {
console.error('Error:', error);
throw error;
}
}
// URL 단축 버튼 클릭 이벤트
shortenBtn.addEventListener('click', async () => {
const originalUrl = originalUrlInput.value.trim();
if (!originalUrl) {
alert('URL을 입력해주세요.');
return;
}
try {
// URL 유효성 검사
new URL(originalUrl);
shortenBtn.disabled = true;
shortenBtn.textContent = '처리중...';
const shortUrl = await shortenUrl(originalUrl);
shortenedUrlInput.value = shortUrl;
resultContainer.style.display = 'block';
} catch (error) {
alert('유효한 URL을 입력해주세요.');
} finally {
shortenBtn.disabled = false;
shortenBtn.textContent = '단축하기';
}
});
// 복사 버튼 클릭 이벤트
copyBtn.addEventListener('click', () => {
shortenedUrlInput.select();
document.execCommand('copy');
const originalText = copyBtn.textContent;
copyBtn.textContent = '복사됨!';
setTimeout(() => {
copyBtn.textContent = originalText;
}, 2000);
});
// Enter 키 입력 시 단축 실행
originalUrlInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
shortenBtn.click();
}
});
});
// 드래그, 우클릭 금지
document.addEventListener('contextmenu', (e) => e.preventDefault());
document.addEventListener('selectstart', (e) => e.preventDefault());