Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
68 lines (59 loc) · 1.71 KB
/
Copy pathbackground.js
File metadata and controls
68 lines (59 loc) · 1.71 KB
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
const maxMeows = 6;
const meanDelay = 400;
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === "complete" && tab.active) {
highlightCats(tabId);
autoDetectCats(tabId);
}
});
chrome.tabs.onActivated.addListener((activeTab) =>
autoDetectCats(activeTab.tabId)
);
const autoDetectCats = (tabId) => {
chrome.browserAction.setBadgeText({ text: "" });
chrome.storage.sync.get(["autodetect"], ({ autodetect }) => {
if (autodetect) {
detectCats(tabId);
} else {
deactivateIcon();
}
});
};
const detectCats = (tabId) => {
chrome.tabs.sendMessage(tabId, { text: "cat_count" }, onCatCount);
};
const highlightCats = (tabId) => {
chrome.tabs.sendMessage(tabId, { text: "cat_highlight" });
};
const onCatCount = (catNumber) => {
if (catNumber) {
animateBadge(catNumber);
} else {
deactivateIcon();
}
};
const deactivateIcon = () => {
chrome.tabs.query({ active: true, currentWindow: true }, (activeTab) => {
chrome.browserAction.disable(activeTab[0].id);
});
};
const animateBadge = (catNumber) => {
// Limit meows, we don't want to play 1000 sounds
let i = catNumber - maxMeows > 0 ? catNumber - maxMeows : 1;
let j = 0;
// Cats will meow at random times
for (; i <= catNumber - 1; i++, j++) {
updateBadge(i, j * Math.random() * meanDelay);
}
// Last cat should come last, let's give it a high delay without random weighing
updateBadge(catNumber, j * meanDelay);
};
const updateBadge = (catIndex, delay) => {
setTimeout(() => {
var meow = new Audio(
chrome.runtime.getURL(`audios/meow_${catIndex % 7}.mp3`)
);
meow.play();
chrome.browserAction.setBadgeText({ text: catIndex.toString() });
}, delay);
};