-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
83 lines (71 loc) · 2.15 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//Chrome workaround
if (typeof browser == "undefined") {
// Chrome does not support the browser namespace yet.
globalThis.browser = chrome;
}
/**
* Gets the current week number as an integar
*
* @returns {int} current week number
*/
function getWeekNumber() {
Date.prototype.getWeek = function () {
var target = new Date(this.valueOf());
var dayNr = (this.getDay() + 6) % 7;
target.setDate(target.getDate() - dayNr + 3);
var firstThursday = target.valueOf();
target.setMonth(0, 1);
if (target.getDay() != 4) {
target.setMonth(0, 1 + ((4 - target.getDay() + 7) % 7));
}
return 1 + Math.ceil((firstThursday - target) / 604800000);
};
var d = new Date();
let result = d.getWeek();
return result;
}
async function setIconAndTitle() {
const weekNumber = getWeekNumber();
//const weekNumber = 49;
browser.action.setTitle({
title: 'Week Number: ' + weekNumber,
});
var canvas = new OffscreenCanvas(64, 64);
var ctx = canvas.getContext('2d');
const style = document.createElement('style');
const fontPath = browser.runtime.getURL('fonts/Inter.ttf');
style.textContent = `
@font-face {
font-family: 'Inter';
src: url('${fontPath}') format('truetype');
}
`;
document.head.appendChild(style);
// Ensure the font is loaded before drawing
const font = new FontFace('Inter', `url(${fontPath})`);
await font.load();
document.fonts.add(font);
ctx.fillStyle = '#fff0';
ctx.fillRect(0, 0, 64, 64);
ctx.font = '58px Inter';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
const colorPromise = browser.storage.local.get('color');
colorPromise.then((res) => {
let color = res.color || '#111';
ctx.fillStyle = color;
const text = weekNumber + '';
const textMetrics = ctx.measureText(text);
const textOffsetY =
(textMetrics.actualBoundingBoxAscent -
textMetrics.actualBoundingBoxDescent) /
2;
ctx.fillText(text, 32, 32 + textOffsetY);
browser.action.setIcon({ imageData: ctx.getImageData(0,0,canvas.width,canvas.height) });
});
}
setInterval(setIconAndTitle, 3600000); // update icon and title every hour
setIconAndTitle();
browser.storage.local.onChanged.addListener((e) => {
setIconAndTitle();
});