-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.html
149 lines (135 loc) · 4.78 KB
/
popup.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="styles.css">
<style>
::selection{
background-color: skyblue;
color: #333;
}
#dashboard{
margin: auto;
align-items: center;
display: inline-block;
justify-content: center;
text-align: center;
background-color: #333;
border-radius: 50px;
width: 100%;
}
button{
height: 20px;
width: 25px;
background-color: antiquewhite;
cursor: pointer;
gap: 15px;
width: auto;
}
button:hover{
opacity: 0.6;
}
.colorDropdown{
display : block;
position : absolute;
top : 30px;
left : 580px;
z-index: 1000;
background-color: white;
overflow : auto;
}
</style>
</head>
<body>
<div id="dashboard">
<button id="color">Color</button>
<button id="delete">Delete</button>
<button id="eraser" onclick="eraseHighlight()">Erase</button>
</div>
<p id="paragraph">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eius vero
necessitatibus tenetur nesciunt aut eaque natus accusantium
consequatur cupiditate architecto rerum, debitis maxime quibusdam,
libero, labore nam non commodi sapiente? Aut iste sapiente officia
pariatur tenetur vitae accusantium, ex voluptas omnis debitis
aliquid enim. Explicabo magnam, officiis error enim unde inventore
amet sequi reprehenderit in? Eligendi, repellat tempora pariatur
inventore quo non eos eveniet distinctio ipsa illo blanditiis optio
labore dignissimos aut quidem esse! Nam hic consequuntur doloribus
eligendi molestias nulla magni sit vero reiciendis, consequatur
repellendus est. Excepturi atque quod molestias explicabo,
laboriosam quam reprehenderit odit ipsam tempora vero voluptates
dolorem autem aut odio culpa voluptas dolorum vitae possimus quae
placeat, quos modi soluta quaerat dignissimos. Magni ad sapiente
minus molestias explicabo, deleniti minima veniam voluptatibus. Enim
incidunt suscipit velit minus quaerat delectus perferendis cum a
quis obcaecati nulla voluptatibus, voluptatum ipsum magnam error
recusandae deserunt adipisci eius ad?
</p>
</body>
<script>
const targetParagraph = document.getElementById('paragraph');
const colorButton = document.getElementById('color');
const deleteButton = document.getElementById('delete');
const eraserButton = document.getElementById('eraser');
let selectedColor = '';
let isColorDropdownOpen = false;
let colorDropdown;
colorButton.addEventListener('click', ()=>{
if(isColorDropdownOpen){
colorDropdown.remove();
isColorDropdownOpen = false;
} else {
isColorDropdownOpen = true;
colorDropdown = document.createElement('div');
colorDropdown.classList.add('colorDropdown');
const colors = ['red', 'blue', 'yellow', 'green', 'purple'];
let count = 0;
for(let i=0;i<colors.length;i++){
const option = document.createElement('button');
option.id = colors[i];
option.value = colors[i];
option.style.backgroundColor = colors[i];
option.addEventListener('click',()=>{
selectedColor = colors[i];
})
colorDropdown.appendChild(option);
}
document.body.appendChild(colorDropdown);
}
});
// Add event listener for the Eraser button
deleteButton.addEventListener('click', ()=>{
removeHighlight();
});
function highlightText(){
const highlight = window.getSelection();
if(!highlight.isCollapsed){
const range = highlight.getRangeAt(0);
const span = document.createElement('span');
span.style.backgroundColor = selectedColor || 'skyblue'
span.style.borderRadius = '50px';
span.appendChild(range.extractContents());
range.insertNode(span);
highlight.removeAllRanges();
}
}
function removeHighlight(){
const highlights = document.querySelectorAll('span');
highlights.forEach(highlight=>{
highlight.outerHTML = highlight.innerHTML;
})
}
function eraseHighlight() {
selectedColor = 'white';
}
eraserButton.addEventListener('click', ()=>{
eraseHighlight();
})
targetParagraph.addEventListener('mouseup', ()=>{
highlightText();
})
</script>
</html>