Skip to content

Commit de6698a

Browse files
committed
Favicon message count
1 parent 783b507 commit de6698a

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"title": "Tab Message Count",
3+
"description": "Displays your message count in the tab favicon, the Scratch icon at the top of your tab.",
4+
"credits": [
5+
{ "username": "rgantzos", "url": "https://scratch.mit.edu/users/rgantzos/" }
6+
],
7+
"scripts": [{ "file": "script.js", "runOn": "/*" }],
8+
"type": ["Website"],
9+
"tags": ["New", "Featured"],
10+
"dynamic": true
11+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
export default async function ({ feature, console }) {
2+
function drawNotification(canvas, text) {
3+
var ctx = canvas.getContext("2d");
4+
5+
var favicon = new Image();
6+
favicon.src = "/favicon.ico";
7+
favicon.onload = function () {
8+
ctx.drawImage(favicon, 0, 0, canvas.width, canvas.height);
9+
10+
ctx.beginPath();
11+
ctx.arc(canvas.width - 10, 14, 8, 0, 2 * Math.PI);
12+
ctx.fillStyle = "#ff9f00";
13+
ctx.fill();
14+
15+
ctx.fillStyle = "white";
16+
ctx.font = "bold " + 10 / (text.length * .4) + "px Arial";
17+
ctx.textAlign = "center";
18+
ctx.textBaseline = "middle";
19+
ctx.fillText(text, canvas.width - 10, 14);
20+
21+
var newFavicon = canvas.toDataURL("image/png");
22+
var link = document.createElement("link");
23+
link.type = "image/x-icon";
24+
link.rel = "shortcut icon";
25+
link.href = newFavicon;
26+
document.head.appendChild(link);
27+
};
28+
}
29+
30+
window.setFaviconCount = setFaviconCount
31+
async function setFaviconCount(count) {
32+
var canvas = document.createElement("canvas");
33+
canvas.width = 20;
34+
canvas.height = 20;
35+
36+
let data = await (
37+
await fetch(
38+
"https://scratch.mit.edu/messages/ajax/get-message-count/?scratchtools=" +
39+
Date.now().toString()
40+
)
41+
).json();
42+
drawNotification(canvas, count?.toString() || data.msg_count.toString());
43+
}
44+
45+
let interval = setInterval(setFaviconCount, 60000);
46+
setFaviconCount();
47+
48+
feature.addEventListener("disabled", function () {
49+
clearInterval(interval);
50+
51+
var link = document.createElement("link");
52+
link.type = "image/x-icon";
53+
link.rel = "shortcut icon";
54+
link.href = "/favicon.ico";
55+
document.head.appendChild(link);
56+
});
57+
58+
feature.addEventListener("enabled", function () {
59+
interval = setInterval(setFaviconCount, 60000);
60+
setFaviconCount()
61+
});
62+
}

features/features.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
[
2+
{
3+
"version": 2,
4+
"id": "favicon-messages",
5+
"versionAdded": "v3.7.0"
6+
},
27
{
38
"version": 2,
49
"id": "show-emoji-names",

0 commit comments

Comments
 (0)