-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
85 lines (74 loc) · 2.13 KB
/
index.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
82
83
84
85
'use strict';
var self = require('sdk/self');
var buttons = require('sdk/ui/button/action');
var tabs = require("sdk/tabs");
var URL = require("sdk/url").URL;
var notifications = require("sdk/notifications");
var pageMod = require("sdk/page-mod");
var {
setInterval, clearInterval
} = require("sdk/timers");
var myIconURL = self.data.url("myIcon.png");
var data = self.data.url("inject-notification.js");
function createButton(tab) {
var button = buttons.ActionButton({
id: "hackchat",
label: "Go to " + tab.channel + " channel",
icon: {
"16": "./chatbubble-outline-16.png",
"32": "./chatbubble-outline-32.png",
"64": "./chatbubble-outline-64.png"
},
//badge: tab.channel + ": 0",
badgeColor: "#292222",
onClick: function (state) {
tab.activate();
tab.unread = 0;
resetBadge(tab);
}
});
return button;
}
pageMod.PageMod({
include: "https://hack.chat/*",
contentScriptWhen: 'ready',
contentScriptFile: self.data.url('inject.js'),
contentScriptOptions: {
scriptUrl: data
}
});
tabs.on("activate", function (tab) {
if (tab.button) {
tab.unread = 0;
resetBadge(tab);
}
});
tabs.on("close", function (tab) {
if (tab.button) {
clearInterval(tab.interval);
tab.button.destroy();
}
});
tabs.on("load", function (tab) {
if (tab.url.split("hack.chat/").length > 1) {
tab.channel = tab.url.split("?")[1];
tab.unread = 0;
resetBadge(tab);
tab.button = createButton(tab);
tab.interval = setInterval(function () {
tab.badge.push(tab.badge.shift());
tab.button.badge = tab.badge[0];
}, 800);
var worker = tab.attach({
contentScript: 'document.addEventListener("notify", function (event) {self.port.emit("notify");});'
});
worker.port.on("notify", function () {
tab.unread += 1;
resetBadge(tab);
});
}
});
function resetBadge(tab) {
tab.badge = tab.channel.match(/.{1,4}/g);
tab.badge.push(tab.unread);
}