-
Notifications
You must be signed in to change notification settings - Fork 0
/
a.html
124 lines (109 loc) · 3.91 KB
/
a.html
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>文章表示</title>
</head>
<body>
<h1>文章の評価</h1>
<div id="result">
<p>以下の文章を評価してください:</p>
<p id="sentence">これは素晴らしい文章です!</p>
<button id="yesButton">Yes</button>
<button id="noButton">No</button>
</div>
<div id="voteCounts">
<p>Yes: <span id="yesCount">0</span></p>
<p>No: <span id="noCount">0</span></p>
</div>
<script>
const userId = getUserId();
let hasVoted = checkIfUserHasVoted();
let yesCount = 0;
let noCount = 0;
let isDisplayed = false;
const yesButton = document.getElementById("yesButton");
const noButton = document.getElementById("noButton");
const resultDiv = document.getElementById("result");
const sentenceElement = document.getElementById("sentence");
const yesCountDisplay = document.getElementById("yesCount");
const noCountDisplay = document.getElementById("noCount");
if (hasVoted) {
yesButton.disabled = true;
noButton.disabled = true;
}
yesButton.addEventListener("click", function () {
if (!hasVoted && !isDisplayed) {
yesCount++;
storeUserVote();
updateVoteCounts();
checkAndDisplay();
}
});
noButton.addEventListener("click", function () {
if (!hasVoted && !isDisplayed) {
noCount++;
storeUserVote();
updateVoteCounts();
checkAndDisplay();
}
});
function checkAndDisplay() {
if (!isDisplayed) {
const totalVotes = yesCount + noCount;
const yesPercentage = (yesCount / totalVotes) * 100;
if (yesPercentage >= 50) {
isDisplayed = true;
sentenceElement.style.display = "block";
}
}
}
function getUserId() {
return Math.random().toString(36).substring(2, 15);
}
function storeUserVote() {
const votedUsers = getVotedUsers();
votedUsers.push(userId);
setCookie("votedUsers", JSON.stringify(votedUsers));
hasVoted = true;
yesButton.disabled = true;
noButton.disabled = true;
}
function checkIfUserHasVoted() {
const votedUsers = getVotedUsers();
return votedUsers.includes(userId);
}
function getVotedUsers() {
const votedUsersCookie = getCookie("votedUsers");
return votedUsersCookie ? JSON.parse(votedUsersCookie) : [];
}
function setCookie(name, value, days) {
const date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
const expires = "expires=" + date.toUTCString();
document.cookie = name + "=" + value + ";" + expires + ";path=/";
}
function getCookie(name) {
const cookieName = name + "=";
const cookies = document.cookie.split(';');
for (let i = 0; i < cookies.length; i++) {
let cookie = cookies[i];
while (cookie.charAt(0) === ' ') {
cookie = cookie.substring(1);
}
if (cookie.indexOf(cookieName) === 0) {
return cookie.substring(cookieName.length, cookie.length);
}
}
return "";
}
function updateVoteCounts() {
// 投票数を表示する関数
yesCountDisplay.textContent = yesCount;
noCountDisplay.textContent = noCount;
}
// ページロード時に投票数を表示
updateVoteCounts();
</script>
</body>
</html>