Skip to content

Commit

Permalink
Merges with main
Browse files Browse the repository at this point in the history
  • Loading branch information
javierarce committed Nov 26, 2024
2 parents 57ca7e0 + 357f99e commit 997f031
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 85 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ Makes your tab disappear when nobody is paying attention.

![favicon chameleon](./favicon-chameleon.gif)

[See it in action](https://javierarce.github.io/favicon-chameleon)
Binary file added favicon
Binary file not shown.
Binary file added favicons/bluesky.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added favicons/mastodon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed favicons/twitter.ico
Binary file not shown.
40 changes: 21 additions & 19 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Favicon Chameleon. Makes your tab disappear when nobody is paying attention.</title>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<head>
<title>Favicon Chameleon. Makes your tab disappear when nobody is paying attention.</title>

<link href="favicons/default.ico" rel="shortcut icon" type="image/x-icon">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="style.css">
<link href="favicons/default.ico" rel="shortcut icon" type="image/x-icon">

<script src="script.js" defer></script>
<link rel="stylesheet" href="style.css">
<script src="script.js" defer></script>

<script type="text/javascript">
window.onload = () => {
new Chameleon(['mastodon', 'bluesky', 'gmail', 'tumblr'])
}
</script>
</head>

<body>
<p>
Ok, now jump to another tab.
</p>
</body>

<script type="text/javascript">
window.onload = () => {
new Chameleon(['twitter', 'gmail', 'tumblr'])
}
</script>
</head>
<body>
<p>
Ok, now jump to another tab.
</p>
</body>
</html>
151 changes: 85 additions & 66 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,111 +1,130 @@
const SERVICES = {
const SERVICES = {
tumblr: () => {
let title = 'Tumblr'
let favicon = 'favicons/tumblr.ico'
let title = "Tumblr";
let favicon = "favicons/tumblr.ico";

return {
title, favicon
}
title,
favicon,
};
},

twitter: () => {
let title = 'Twitter'
let favicon = 'favicons/twitter.ico'
mastodon: () => {
let title = "Home - Mastodon";
let favicon = "favicons/mastodon.png";

return {
title, favicon
}
title,
favicon,
};
},

bluesky: () => {
let title = "Following — Bluesky";
let favicon = "favicons/bluesky.png";

return {
title,
favicon,
};
},

gmail: () => {
let title = 'Inbox'
let count = Math.round(Math.random() * 12)
let emailCount = count
let gmailIcon = 'favicons/gmail_'
let title = "Inbox";
let count = Math.round(Math.random() * 12);
let emailCount = count;
let gmailIcon = "favicons/gmail_";

if (count === 11) {
count = 50
emailCount = `${count}+`
count = 50;
emailCount = `${count}+`;
} else if (count === 12) {
count = 100
emailCount = `${count}+`
count = 100;
emailCount = `${count}+`;
}

let favicon = `${gmailIcon}${count}.png`
let favicon = `${gmailIcon}${count}.png`;

if (emailCount) {
title = `Inbox (${emailCount})`
title = `Inbox (${emailCount})`;
}

return {
title, favicon
}
}
}
title,
favicon,
};
},
};

class Chameleon {
constructor (enabledServices) {
this.enabledServices = enabledServices
this.services = SERVICES
this.hidden = 'hidden'
this.visibilityChange = 'visibilitychange'

this.favicon = document.querySelector("[rel='shortcut icon']").href
this.title = document.title
this.init()
constructor(enabledServices) {
this.enabledServices = enabledServices;
this.services = SERVICES;
this.hidden = "hidden";
this.visibilityChange = "visibilitychange";

this.favicon = document.querySelector("[rel='shortcut icon']").href;
this.title = document.title;

this.init();
}

init () {
if (typeof document.mozHidden !== 'undefined') {
this.hidden = 'mozHidden'
this.visibilityChange = 'mozvisibilitychange'
} else if (typeof document.msHidden !== 'undefined') {
this.hidden = 'msHidden'
this.visibilityChange = 'msvisibilitychange'
} else if (typeof document.webkitHidden !== 'undefined') {
this.hidden = 'webkitHidden'
this.visibilityChange = 'webkitvisibilitychange'
init() {
if (typeof document.mozHidden !== "undefined") {
this.hidden = "mozHidden";
this.visibilityChange = "mozvisibilitychange";
} else if (typeof document.msHidden !== "undefined") {
this.hidden = "msHidden";
this.visibilityChange = "msvisibilitychange";
} else if (typeof document.webkitHidden !== "undefined") {
this.hidden = "webkitHidden";
this.visibilityChange = "webkitvisibilitychange";
}

document.addEventListener(this.visibilityChange, this.handleVisibilityChange.bind(this), false)
document.addEventListener(
this.visibilityChange,
this.handleVisibilityChange.bind(this),
false,
);
}

setDefaultFavicon () {
let title = this.title
let favicon = this.favicon
setDefaultFavicon() {
let title = this.title;
let favicon = this.favicon;

this.updateFavicon({ title, favicon})
this.updateFavicon({ title, favicon });
}

updateFavicon (data) {

let randomString = "?v=" + Math.round(Math.random() * 10000000)
let link = document.createElement('link')
updateFavicon(data) {
let randomString = "?v=" + Math.round(Math.random() * 10000000);
let link = document.createElement("link");

link.type = 'image/x-icon'
link.rel = 'shortcut icon'
link.href = data.favicon + randomString
link.type = "image/x-icon";
link.rel = "shortcut icon";
link.href = data.favicon + randomString;

document.getElementsByTagName('head')[0].querySelector("[rel='shortcut icon']").remove()
document.getElementsByTagName('head')[0].appendChild(link)
document.title = data.title
document
.getElementsByTagName("head")[0]
.querySelector("[rel='shortcut icon']")
.remove();
document.getElementsByTagName("head")[0].appendChild(link);
document.title = data.title;
}

enableService () {
let i = Math.round(Math.random() * (this.enabledServices.length - 1))
let service = this.enabledServices[i]
enableService() {
let i = Math.round(Math.random() * (this.enabledServices.length - 1));
let service = this.enabledServices[i];

if (service && this.services[service]) {
this.updateFavicon(this.services[service]())
this.updateFavicon(this.services[service]());
}
}

handleVisibilityChange () {
handleVisibilityChange() {
if (document[this.hidden]) {
this.enableService()
this.enableService();
} else {
this.setDefaultFavicon()
this.setDefaultFavicon();
}
}
}

0 comments on commit 997f031

Please sign in to comment.