-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.js
139 lines (121 loc) · 4.55 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
//This is a addon for habbitRPG.com
//every time when you visit vice website it report to habbitRPG and you loose HP.
var PanelButton = require("sdk/ui/button/toggle").ToggleButton;
var tabs = require('sdk/tabs');
var notifications = require("sdk/notifications");
var Url = require("sdk/url");
var Prefs = require("sdk/simple-prefs").prefs;
var Request = require("sdk/request").Request;
var Panel = require("sdk/panel").Panel;
var Self = require("sdk/self");
var api = require("./API").API;
var settings = require("settings").Settings;
var _ = require("sdk/l10n").get;
var { on, once, off, emit } = require('sdk/event/core');
var is_vice_host = function (host)
{
let viceList = Prefs.viceHosts;
if (viceList.match(new RegExp("(?:^|,)[\\.\\w]*"+host+"(?:,|$)"))) {
return true;
} else {
return false;
}
}
function is_benefical_host (host){
let benList = Prefs.benHosts;
if (benList.match(new RegExp("(?:^|,)[\\.\\w]*"+host+"(?:,|$)"))) {
return true;
} else {
return false;
}
}
var is_penalty_time = function (){
var current_time = new Date();
var time = current_time.getHours();
if(Prefs.penaltystart>Prefs.penaltyend||Prefs.penaltyend>24){
notifications.notify({
title: _("Habitica"),
text: _("time_format_wrong")
})
return (false);
}
return (Prefs.penaltystart<=time&&time<=Prefs.penaltyend);
};
exports.main = function()
{
let open_url_on_click = "http://habitica.com"
let panelButton = PanelButton({
id: "habit-rpg-button",
label: "My Habitica Status",
icon: {
"16" : Self.data.url("favicon.png"),
"64" : Self.data.url("favicon64.png")
},
});
//mini panel, contain health and XP bars, opens on click on widget
let info_panel = Panel({
focus: false,
width:240,
height:120,
contentURL: Self.data.url("panel-content.html"),
contentStyleFile: Self.data.url("panel-content.css"),
contentScriptFile: Self.data.url("panel-content.js"),
position: panelButton
});
//on click to gear icon open settings
info_panel.port.on("open_settings", function () {settings.open(); info_panel.hide();});
//on click open new tab and hide panel
info_panel.port.on("clicked", function () {tabs.open(open_url_on_click); info_panel.hide();});
//update info every time when user open panel
info_panel.on("show", function() {info_panel.port.emit("show_splash_screen"); api.update_info();});
//show and hide panel when clicking on addon button
info_panel.on("hide", function() {panelButton.state('window', {checked: false})});
panelButton.on("click", function(state) { if (state.checked) {info_panel.show(); } else {info_panel.hide()};});
on(api, "updated", function(stats) {
open_url_on_click = "http://habitica.com";
info_panel.port.emit("update_html", stats);
});
on(api, "invalid_credentials", function(error_message) {
open_url_on_click = Self.data.url("welcome.html");
info_panel.port.emit("show_error_message", error_message);
});
on(api, "error", function(error_message) {
console.log(error_message)
info_panel.port.emit("show_error_message", error_message);
if (! info_panel.isShowing) {
notifications.notify({
title: _("notifications_title_error"),
text: error_message,
iconURL: Self.data.url("favicon.png")
})
}
});
//add event listener on every tab
tabs.on('open', function(tab){
tab.on('ready', function(tab){
if (!Prefs.taskId)
return;
let host = (new Url.URL(tab.url)).host;
if (is_vice_host(host)&&is_penalty_time()) {
//punish user
notifications.notify({
title: _("Habitica"),
text: _("notification_punish")
})
api.change_task_rate(Prefs.taskId, "down");
}
if(is_benefical_host(host)&&is_penalty_time()){
notifications.notify({
title: _("Habitica"),
//@todo: show gained exp
text: _("notification_reward")
})
api.change_task_rate(Prefs.taskId, "up");
}
});
});
// FIXME, remove this ugly hack with 'require'
require("sdk/simple-prefs").on("openSettings", function () {settings.open()})
if (Self.loadReason == "install")
settings.open()
};