-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
288 lines (197 loc) · 5.48 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// required modules
var tabs = require("sdk/tabs"),
utils = require('sdk/window/utils'),
self = require('sdk/self'),
ActionButton = require("sdk/ui/button/action").ActionButton,
storage = require("sdk/simple-storage"),
Request = require("sdk/request").Request;
// internal modules
var URI = require('./vendor/URI.js'),
CONSTANTS = {
DOMAINS_KEY: 'domains',
INSTALL_KEY: 'install',
CACHE: 60
},
CURRENT_TAB = null;
/**
* Opens a tab to the welcome page when the addon is first installed
**/
function installNotice() {
// check if we gave the welcome page already
if(!storage.storage[ CONSTANTS.INSTALL_KEY ]) {
// current date
current_date = new Date()
// set the key
storage.storage[ CONSTANTS.INSTALL_KEY ] = current_date.getTime()
// open the welcome page
tabs.open("http://passmarked.com/welcome?source=firefox.ext×tamp=" + current_date.getTime())
}
}
/**
* Finds the domain from the cache
**/
function byCache(domain_str, fn) {
// is there not a cache ?
if(storage[CONSTANTS.DOMAINS_KEY]) {
// current timestamp
current_timestamp = new Date().getTime()
// loop all the domains
for (let report_obj of (storage[CONSTANTS.DOMAINS_KEY] || []) ) {
// check if domain matches
if( report_obj.domain === domain_str ) {
if( ( current_timestamp - report_obj.timestamp ) <= 1000 * 60 * CONSTANTS.CACHE) {
// return the time
fn(report_obj);
} else {
// return nothing
fn(null);
}
// stop here
return;
}
}
// handle defaults
fn(null)
} else fn(null);
}
/**
* Handles getting data from our store
**/
function getReportInfoByURL(url_str, fn) {
// parse the url
var url_obj = URI(url_str);
// find the hostname
var hostname_str = ( url_obj.hostname() ).toLowerCase();
// check the cache
byCache( hostname_str, function(result) {
// if we found a result
if(!result) {
Request({
url: "https://api.passmarked.com/v1/query?domain=" + hostname_str,
onComplete: function (response) {
// start our response
var current_param = response.json || {};
// param to add
current_param.domain = hostname_str;
current_param.timestamp = new Date().getTime()
// set the cache
if(!storage.storage[ CONSTANTS.DOMAINS_KEY ])
storage.storage[ CONSTANTS.DOMAINS_KEY ] = [];
// add the storage
storage.storage[ CONSTANTS.DOMAINS_KEY ].push( current_param )
// handle back
fn(current_param);
}
}).get();
} else fn(result);
} );
}
/**
* Shows the table
**/
function handleIconClick(url_str) {
// check if not the password result website
if(url.indexOf('passmarked.com') != -1 && url.replace('http:').split('/').length > 4) {
// open up the tabs
tabs.open("http://passmarked.com/dawg?source=firefox.ext&url=" + encodeURIComponent(url_str))
} else {
// open up the tabs
tabs.open("https://api.passmarked.com/v1/redirect?url=" + encodeURIComponent(url_str))
}
}
/**
* Show a blank icon
**/
function showIcon(tab, score) {
// get the doc
var doc = utils.getMostRecentBrowserWindow().document;
// get a button
var btn = doc.getElementById('passmarkbadge');
// check for a element
if(btn) {
// remove the btn
btn.parentElement.removeChild(btn);
}
// pick a badge to use
var badge_path_str = self.data.url('faces/face.png');
// check for a score
if( Math.floor(score) > 0 ) {
// convert to string
var score_str = '' + Math.floor(score).toString();
// add padding if needed
if(score_str.length == 1)
score_str = '0' + score_str;
// set the final path
badge_path_str = self.data.url('faces/' + score_str + '.png');
}
// create the button
var urlBarIcons = doc.getElementById('urlbar-icons')
var btn = doc.createElement('image');
btn.setAttribute('id', 'passmarkbadge');
btn.width = '16px'
btn.height = '16px'
btn.setAttribute('style', 'cursor: pointer;-moz-user-focus:');
btn.setAttribute('src', self.data.url(badge_path_str));
btn.onclick = handleIconClick;
urlBarIcons.appendChild(btn);
}
/**
* Handle if we hit qouta ...
**/
storage.on("OverQuota", function(){
// delete the cache of domains
if(storage.storage[ CONSTANTS.DOMAINS_KEY ]) {
// set to blank
storage.storage[ CONSTANTS.DOMAINS_KEY ] = []
// remove it
delete storage.storage[ CONSTANTS.DOMAINS_KEY ];
}
});
/**
* Checks if we should allow this url
**/
function allowUrlView(url_str) {
// check the url
if(!url_str) return false;
// parse the url
var url_obj = URI(url_str);
// find the hostname
var hostname_str = ( url_obj.hostname() ).toLowerCase();
// handle it
if( hostname_str.indexOf('localhost') === 0) return false;
if( url_str.indexOf('chrome://') === 0) return false;
if( hostname_str.indexOf('about:') !== -1) return false;
// return a true !
return true;
}
/**
* Handles receiving the tab which we would then show
**/
function handleTabEvent(tab) {
// set the current tab then
CURRENT_TAB = tab;
// sanity check
if(tab && tab.url && allowUrlView(tab.url)) {
// cool so get the score
getReportInfoByURL(tab.url, function(result) {
// only if we still have the same tab
if(CURRENT_TAB == tab) {
if(result && (result.count || 0) > 0) {
// output the counter icon
showIcon(tab, result.score);
} else {
// show our blank icon ...
showIcon(tab, null);
}
}
});
}
}
/**
* Listen for new tabs
**/
tabs.on("onPageShow", handleTabEvent);
tabs.on("ready", handleTabEvent);
tabs.on("activate", handleTabEvent);
// show a install notice
installNotice();