Skip to content

Commit 91b9653

Browse files
committed
added getCloak() and added better error handling
- Added getCloak() - Added Better error handling
1 parent 1a810f4 commit 91b9653

File tree

3 files changed

+60
-11
lines changed

3 files changed

+60
-11
lines changed

demo/docs.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<link rel="icon" type="image/x-icon" href="cloaks/favicon.ico" />
77
<title>Docs - Cloak.js</title>
8+
<script src="../src/index.js" type=""></script>
89
<meta
910
name="description"
1011
content="Cloak js is a simple library that allows you to change the title and favicon of your webpage."
@@ -129,6 +130,26 @@ <h4 class="text-xl font-semibold">Example:</h4>
129130
</code>
130131
</pre>
131132
</div>
133+
<div class="bg-base-100 p-4 rounded-lg shadow-md mb-4">
134+
<h3 class="text-2xl font-semibold">getCloak()</h3>
135+
<p>
136+
Retrieves the active cloak from <code>localStorage</code> and
137+
returns the title and icon in an array format.
138+
</p>
139+
<h4 class="text-xl font-semibold">Example:</h4>
140+
<pre class="bg-base-300 p-4 rounded-lg">
141+
<code>
142+
const [title, icon] = getCloak(); // Returns [title, icon]
143+
144+
console.log(title); // Access the title
145+
console.log(icon); // Access the icon
146+
</code>
147+
</pre>
148+
<p class="text-sm text-gray-500 mt-2">
149+
Note: The <code>getCloak()</code> function will return an array with
150+
the title and icon. Ensure you handle both values correctly.
151+
</p>
152+
</div>
132153
</section>
133154
</main>
134155

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@parcoil/cloak",
3-
"version": "1.3.0",
3+
"version": "1.4.0",
44
"description": "Tab Cloaking lib",
55
"main": "src/index.js",
66
"repository": {

src/index.js

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,59 @@ const logo = "\x1b[91m[Parcoil Cloak]\x1b[0m";
22

33
const cloak = {
44
getFavicon() {
5-
const icons = document.querySelectorAll('link[rel="icon"]');
6-
return icons.length > 0 ? icons[0].href : null;
5+
try {
6+
const icons = document.querySelectorAll('link[rel="icon"]');
7+
return icons.length > 0 ? icons[0].href : null;
8+
} catch (err) {
9+
console.error(logo, err);
10+
}
711
},
812
setFavicon(url) {
9-
const icons = document.querySelectorAll('link[rel="icon"]');
10-
icons.forEach((icon) => (icon.href = url));
11-
localStorage.setItem("cloakFavicon", url);
13+
try {
14+
const icons = document.querySelectorAll('link[rel="icon"]');
15+
if (icons.length === 0) {
16+
throw new Error("Favicon not found. Try adding rel='icon'");
17+
}
18+
icons.forEach((icon) => (icon.href = url));
19+
localStorage.setItem("cloakFavicon", url);
20+
} catch (err) {
21+
console.error(logo, err);
22+
}
1223
},
1324
getTitle() {
14-
return document.title;
25+
try {
26+
return document.title;
27+
} catch (err) {
28+
console.error(logo, err);
29+
}
1530
},
1631
setTitle(newTitle) {
17-
document.title = newTitle;
18-
localStorage.setItem("cloakTitle", newTitle);
32+
try {
33+
document.title = newTitle;
34+
localStorage.setItem("cloakTitle", newTitle);
35+
} catch (err) {
36+
console.error(logo, err);
37+
}
1938
},
2039
setCloak(newTitle, url) {
21-
this.setTitle(newTitle);
22-
this.setFavicon(url);
40+
try {
41+
this.setTitle(newTitle);
42+
this.setFavicon(url);
43+
} catch (err) {
44+
console.error(logo, err);
45+
}
2346
},
2447
init() {
2548
console.warn(
2649
logo,
2750
"cloak.init() has been deprecated. theres no need to call it anymore."
2851
);
2952
},
53+
getCloak() {
54+
const title = localStorage.getItem("cloakTitle");
55+
const icon = localStorage.getItem("cloakFavicon");
56+
return [title, icon];
57+
},
3058
aboutBlank(url) {
3159
if (!url) url = "https://www.google.com/search?q=how+many+seconds+in+a+day";
3260
const newWindow = window.open();

0 commit comments

Comments
 (0)