-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
146 lines (129 loc) · 4.13 KB
/
main.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
(function() {
// debug at air
// var MANIFEST_URL = 'app://33bfa81b-9b3b-8545-80d2-76ca07625ffc/manifest.webapp';
// online version
var MANIFEST_URL = 'https://john-hu.github.io/fxos-text-battery/manifest.webapp';
function TextBatteryLevel(addon) {
this.trailCount = 5;
// this part should be moved to css file, but the CSS injection feature is
// broken now.
this.style = document.createElement('style');
this.style.textContent = `
.sb-icon-battery[data-battery-level]:after {
content: attr(data-battery-level);
text-align: center;
position: absolute;
width: 2.5rem;
height: 1.6rem;
line-height: 1.6rem;
color: black;
}
.sb-icon-battery[data-battery-level][data-charging="true"]:after {
content: attr(data-battery-level);
text-align: left;
position: absolute;
width: 2.5rem;
height: 1.6rem;
line-height: 1.6rem;
color: blue;
padding-left: 0.1rem;
font-size: 0.8rem;
}`;
this.inited = false;
}
TextBatteryLevel.prototype.init = function() {
if (this.inited) {
// We don't know the state of first installation. It should be 'uninited',
// but it isn't always.
// To prevent multiple initialization, I use this variable to pretect it.
return;
}
this.battery = window.navigator.battery;
if (!this.battery) {
return;
}
if (!this.checkBatteryIcon()) {
if (this.trailCount > 0) {
setTimeout(this.init.bind(this), 2000);
this.trailCount--;
}
return;
}
document.body.appendChild(this.style);
this.battery.addEventListener('levelchange', this);
this.updateDataset(this.battery.level);
this.inited = true;
};
TextBatteryLevel.prototype.checkBatteryIcon = function() {
return document.querySelectorAll('.sb-icon-battery').length > 0;
};
TextBatteryLevel.prototype.uninit = function() {
if (!this.battery) {
return;
}
this.battery.removeEventListener('levelchange', this);
this.updateDataset(null);
document.body.removeChild(this.style);
this.inited = false;
};
TextBatteryLevel.prototype.updateDataset = function(level) {
var batteryIcons = document.querySelectorAll('.sb-icon-battery');
[].forEach.call(batteryIcons, function(dom) {
if (!level) {
dom.removeAttribute('data-battery-level');
} else {
dom.dataset.batteryLevel = Math.round(level * 100);
}
});
};
TextBatteryLevel.prototype.handleEvent = function(e) {
switch(e.type) {
case 'levelchange':
this.updateDataset(this.battery.level);
break;
}
};
function TextBatteryLevelMgmt() {
if (document.documentElement.dataset.textBatteryLevel) {
console.log('text-battery-level is already injected');
return;
}
this.batteryIcon = new TextBatteryLevel();
this.batteryIcon.init();
navigator.mozApps.mgmt.addEventListener('enabledstatechange', this);
navigator.mozApps.mgmt.addEventListener('uninstall', this);
document.documentElement.dataset.textBatteryLevel = true;
}
TextBatteryLevelMgmt.prototype.handleEvent = function(e) {
if (e.application.manifestURL !== MANIFEST_URL) {
return;
}
switch(e.type) {
case 'enabledstatechange':
if (e.application.enabled) {
this.batteryIcon.init();
} else {
this.batteryIcon.uninit();
}
break;
case 'uninstall':
this.batteryIcon.uninit();
navigator.mozApps.mgmt.removeEventListener('enabledstatechange', this);
navigator.mozApps.mgmt.removeEventListener('uninstall', this);
document.documentElement.removeAttribute('data-text-battery-level');
break;
}
};
if (document.readyState !== 'loading') {
new TextBatteryLevelMgmt();
} else {
document.addEventListener('readystatechange',
function readyStateChange() {
if (document.readyState === 'interactive') {
document.removeEventListener('readystatechange',
readyStateChange);
new TextBatteryLevelMgmt();
}
});
}
})();