-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurlsx.js
More file actions
107 lines (99 loc) · 3.35 KB
/
urlsx.js
File metadata and controls
107 lines (99 loc) · 3.35 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// サインアップフォームの送信を処理
document.getElementById('signup-form').addEventListener('submit', function(event) {
event.preventDefault();
const username = document.getElementById('username').value;
const password = document.getElementById('password').value;
fetch('signup.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}`,
})
.then(response => response.text())
.then(data => {
if (data.includes('Location: login.php')) {
window.location.href = 'login.php';
} else {
alert('サインアップに失敗しました。');
}
})
.catch(error => console.error('Error:', error));
});
// ログインフォームの送信を処理
document.getElementById('login-form').addEventListener('submit', function(event) {
event.preventDefault();
const username = document.getElementById('username').value;
const password = document.getElementById('password').value;
fetch('login.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}`,
})
.then(response => response.text())
.then(data => {
if (data.includes('Location: /')) {
window.location.href = '/';
} else {
alert('ログインに失敗しました。');
}
})
.catch(error => console.error('Error:', error));
});
// URL短縮フォームの送信を処理
document.getElementById('url-form').addEventListener('submit', function(event) {
event.preventDefault();
const originalUrl = document.getElementById('url-input').value;
const customPath = document.getElementById('custom-path-input').value;
fetch('shorturl.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `url=${encodeURIComponent(originalUrl)}&customPath=${encodeURIComponent(customPath)}`,
})
.then(response => response.json())
.then(data => {
if (data.short_url) {
alert(`短縮URL: ${data.short_url}`);
} else {
alert('URLの短縮に失敗しました。');
}
})
.catch(error => console.error('Error:', error));
});
// クリップボードにコピーする関数
function copyToClipboard() {
const copyText = document.getElementById("copyMessage").textContent;
navigator.clipboard.writeText(copyText).then(() => {
alert("URLがクリップボードにコピーされました");
}, (error) => {
alert("エラー: URLをコピーできませんでした");
});
}
// URLを短縮する関数
function shortenUrl() {
const originalUrl = document.getElementById('url').value;
const customPath = document.getElementById('customPath').value;
fetch('shorturl.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `url=${encodeURIComponent(originalUrl)}&customPath=${encodeURIComponent(customPath)}`,
})
.then(response => response.json())
.then(data => {
if (data.short_url) {
document.getElementById('copyMessage').textContent = data.short_url;
copyToClipboard();
} else {
alert('URLの短縮に失敗しました。');
}
})
.catch(error => {
console.error('Error:', error);
});
}