|
| 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 | +} |
0 commit comments