Skip to content

Commit ff26bb5

Browse files
committed
Adding Day #28
1 parent cb331f6 commit ff26bb5

File tree

11 files changed

+360
-0
lines changed

11 files changed

+360
-0
lines changed
742 KB
Loading
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Day #28
2+
3+
### Chrome Color Picker Extension
4+
In this tutorial ([Open in Youtube](https://youtu.be/5fcXmiRYlrA)), In this tutorial, I am gonna showing to you how to code a chrome extension with javascript. we create a project that you can pick colors as a chrome extension with javascript❗️
5+
6+
# Screenshot
7+
Here we have project screenshot :
8+
9+
![screenshot](28-ScreenShot.png)
7.3 KB
Loading
831 Bytes
Loading
1.72 KB
Loading
6.7 KB
Loading
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<link rel="stylesheet" href="style.css">
7+
<title>DAy #28 - Chrome Color Picker Extension | NaeuCode </title>
8+
</head>
9+
<body>
10+
<div class="container">
11+
<div class="picker">
12+
<button id="picker-btn">Picker Color</button>
13+
<button id="export-btn">Export Colors</button>
14+
</div>
15+
16+
<div class="colors-list hide">
17+
<header>
18+
<p class="title">
19+
Picked Colors
20+
</p>
21+
<span id="clear-btn">Clear All</span>
22+
</header>
23+
<ul class="all-colors"></ul>
24+
</div>
25+
</div>
26+
27+
<script src="script.js"></script>
28+
</body>
29+
</html>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"manifest_version": 3,
3+
"name": "Color Picker",
4+
"description": "Simple Color Picker Extension that we created in AsmrProg Channel. Pick any color on web page, Picked Colors history, Copy RGB and Hex or Clear them with a single click.",
5+
"version": "1.0",
6+
"action": {
7+
"default_popup": "index.html"
8+
},
9+
"icons": {
10+
"16": "icons/icon16.png",
11+
"32": "icons/icon32.png",
12+
"48": "icons/icon48.png",
13+
"128": "icons/icon128.png"
14+
}
15+
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
const pickerBtn = document.querySelector("#picker-btn");
2+
const clearBtn = document.querySelector("#clear-btn");
3+
const colorList = document.querySelector(".all-colors");
4+
const exportBtn = document.querySelector("#export-btn");
5+
6+
// Retrieving picked colors form localStorage or initializing an empty array
7+
let pickedColors = JSON.parse(localStorage.getItem("colors-list")) || [];
8+
9+
// Variable to keep track of the current color popup
10+
let currentPopup = null;
11+
12+
//Function to copy text to the clipboard
13+
const copyToClipboard = async (text, element) => {
14+
try {
15+
await navigator.clipboard.writeText(text);
16+
element.innerText = "Copied!";
17+
//Resetting element text after 1 second
18+
setTimeout(() => {
19+
element.innerText = text;
20+
21+
}, 1000);
22+
} catch (error) {
23+
alert("Filed to Copy text!");
24+
}
25+
};
26+
27+
//Function to export colors as text file
28+
const exportColors = () => {
29+
const colorText = pickedColors.join("\n");
30+
const blob = new Blob([colorText], {type: "text/plain"}) ;
31+
const url = URL.createObjectURL(blob);
32+
const a = document.createElement("a");
33+
a.href = url;
34+
a.download = "Colors.text";
35+
document.body.appendChild(a);
36+
a.click();
37+
document.body.removeChild(a);
38+
URL.revokeObjectURL(url);
39+
};
40+
41+
//Function to create the color popup
42+
const createColorPopup = (color) => {
43+
const popup = document.createElement("div");
44+
popup.classList.add("color-popup");
45+
popup.innerHTML = `
46+
<div class="color-popup-content">
47+
<span class="close-popup">x</span>
48+
<div class="color-info">
49+
<div class="color-preview" style="background: ${color};"></div>
50+
<div class="color-details">
51+
<div class="color-value">
52+
<span class="label">Hex: </span>
53+
<span class="value hex" data-color="${color}">${color}</span>
54+
</div>
55+
<div class="color-value">
56+
<span class="label">RGB:</span>
57+
<span class="value rgb" data-color="${color}">${hexToRgb(color)}</span>
58+
</div>
59+
</div>
60+
</div>
61+
</div>
62+
63+
`;
64+
65+
// Close button inside the popup
66+
const closePopup = popup.querySelector(".close-popup");
67+
closePopup.addEventListener('click', () => {
68+
document.body.removeChild(popup);
69+
currentPopup = null;
70+
});
71+
72+
//Event listeners to copy color value to clipboard
73+
const colorValues = popup.querySelectorAll(".value");
74+
colorValues.forEach((value) => {
75+
value.addEventListener('click', (e) => {
76+
const text = e.currentTarget.innerText;
77+
copyToClipboard(text, e.currentTarget);
78+
});
79+
});
80+
81+
return popup;
82+
83+
};
84+
//Function to display the picked colors
85+
const showColors = () => {
86+
colorList.innerHTML = pickedColors.map((color) =>
87+
`
88+
<li class="color">
89+
<span class="rect" style="background: ${color}; border: 1px solid ${color === "#ffffff" ? "#ccc" : color}"></span>
90+
<span class="value hex" data-color="${color}">${color}</span>
91+
</li>
92+
`
93+
).join("");
94+
95+
const colorElement = document.querySelectorAll(".color");
96+
colorElement.forEach((li) => {
97+
const colorHex = li.querySelector(".value.hex");
98+
colorHex.addEventListener('click', (e) => {
99+
const color = e.currentTarget.dataset.color;
100+
if(currentPopup) {
101+
document.body.removeChild(currentPopup);
102+
}
103+
const popup = createColorPopup(color);
104+
document.body.appendChild(popup);
105+
currentPopup = popup;
106+
});
107+
});
108+
109+
const pickedColorsContainer = document.querySelector(".colors-list");
110+
pickedColorsContainer.classList.toggle("hide", pickedColors.length === 0);
111+
112+
};
113+
114+
// function to convert a hex color code to rgb format
115+
const hexToRgb = (hex) => {
116+
const bigint = parseInt(hex.slice(1), 16);
117+
const r = (bigint >> 16) & 255;
118+
const g = (bigint >> 8) & 255;
119+
const b = bigint & 255;
120+
return `rgb(${r}, ${g}, ${b})`;
121+
122+
};
123+
124+
// function to activate the eye dropper color picker
125+
const activateEyeDropper = async () => {
126+
document.body.style.display = "none";
127+
try {
128+
//Opening the eye dropper and retrieving the selected color
129+
const {sRGBHex} = await new EyeDropper().open();
130+
131+
if (!pickedColors.includes(sRGBHex)) {
132+
pickedColors.push(sRGBHex);
133+
localStorage.setItem("colors-list", JSON.stringify(pickedColors));
134+
}
135+
136+
showColors();
137+
} catch (error) {
138+
alert("Filed to Copy the color code!");
139+
} finally {
140+
document.body.style.display = "block";
141+
}
142+
};
143+
144+
// function to clear all picked colors
145+
const clearAllColors = () => {
146+
pickedColors = [];
147+
localStorage.removeItem("colors-list");
148+
showColors();
149+
};
150+
151+
// Event listeners for buttons
152+
clearBtn.addEventListener('click', clearAllColors);
153+
pickerBtn.addEventListener('click', activateEyeDropper);
154+
exportBtn.addEventListener('click',exportColors);
155+
156+
showColors();
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@100;200;300;400;500;600;700;800&display=swap');
2+
3+
* {
4+
margin: 0;
5+
padding: 0;
6+
box-sizing: border-box;
7+
font-family: 'Poppins', sans-serif;
8+
}
9+
10+
.container {
11+
background-color: #fff;
12+
width: 350px;
13+
}
14+
15+
.container :where(.picker, header, .all-colors){
16+
display: flex;
17+
align-items: center;
18+
}
19+
20+
.container .picker {
21+
padding: 35px 0;
22+
background-color: #cbc8e6;
23+
justify-content: center;
24+
}
25+
26+
.picker #picker-btn {
27+
margin-right: 8px;
28+
}
29+
30+
.picker #picker-btn, .picker #export-btn {
31+
border: none;
32+
outline: none;
33+
color: #fff;
34+
font-size: 1rem;
35+
padding: 6px 12px;
36+
cursor: pointer;
37+
background-color: #434ea0;
38+
border-radius: 5px;
39+
transition: all .3s ease;
40+
}
41+
42+
.picker #picker-btn:hover, .picker #export-btn:hover {
43+
background-color: #2e527d;
44+
}
45+
46+
.colors-list {
47+
margin: 10px 15px;
48+
}
49+
50+
.colors-list header {
51+
justify-content: space-between;
52+
}
53+
54+
header .title {
55+
font-size: 1rem;
56+
}
57+
58+
header #clear-btn {
59+
cursor: pointer;
60+
font-size: .9rem;
61+
color: #435ca0;
62+
}
63+
64+
header #clear-btn:hover {
65+
color: #2e527d;
66+
}
67+
68+
.colors-list.hide {
69+
display: none;
70+
}
71+
72+
.colors-list .all-colors {
73+
flex-wrap: wrap;
74+
list-style: none;
75+
margin: 10px 0 0;
76+
}
77+
78+
.all-colors .color {
79+
display: flex;
80+
cursor: pointer;
81+
margin-bottom: 10px;
82+
width: calc(100% / 3);
83+
}
84+
85+
.all-colors .rect {
86+
height: 22px;
87+
width: 22px;
88+
display: block;
89+
margin-right: 3px;
90+
border-radius: 3px;
91+
}
92+
93+
.all-colors .color span {
94+
font-size: .8rem;
95+
font-weight: 500;
96+
text-transform: uppercase;
97+
}
98+
99+
.color-popup {
100+
position: fixed;
101+
top: 52px;
102+
left: 50%;
103+
transform: translate(-50%, -50%);
104+
width: 300px;
105+
background-color: #fff;
106+
border-radius: 5px;
107+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
108+
z-index: 9999;
109+
}
110+
111+
.color-popup-content {
112+
padding: 20px;
113+
}
114+
115+
.color-info {
116+
display: flex;
117+
align-items: center;
118+
}
119+
120+
.color-preview {
121+
width: 50px;
122+
height: 50px;
123+
border-radius: 5px;
124+
margin-right: 10px;
125+
}
126+
127+
.color-details {
128+
flex-grow: 1;
129+
}
130+
131+
.color-value {
132+
display: flex;
133+
align-items: center;
134+
margin-bottom: 5px;
135+
}
136+
137+
.label {
138+
font-weight: 700;
139+
margin-right: 5px;
140+
}
141+
142+
.value {
143+
cursor: pointer;
144+
}
145+
146+
span.close-popup {
147+
font-size: 18px;
148+
cursor: pointer;
149+
float: right;
150+
}

0 commit comments

Comments
 (0)