-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (49 loc) · 1.75 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
var self = require('sdk/self');
var tabs = require('sdk/tabs');
var urls = require('sdk/url');
var panel = require('sdk/panel');
// List of dimains ignored for this session
var ignoredDomains = [];
// Check if unicode is contained in a string
// Also allows checks for suspicious ASCII characters
function containsSuspiciousCharacter(str) {
return (/[^\x1f-\x7f]/g.test(str)) ? true: false;
}
exports.containsSuspiciousCharacter = containsSuspiciousCharacter;
// Check a tab object for a suspicious URL and display warnings
function checkTab(tab) {
var url = urls.URL(tab.url);
if (ignoredDomains.indexOf(url.host) > -1) {
// This domain was already ignored by the user
} else if (containsSuspiciousCharacter(url.host)) {
var warningPanel = panel.Panel({
width: 533,
height: 200,
contentURL: self.data.url('warning.html'),
contentScriptFile: self.data.url('warning.js')
});
warningPanel.port.on('leave', function(data) {
tab.url = self.data.url('explain.html');
warningPanel.destroy();
});
warningPanel.port.on('ignore', function(data) {
ignoredDomains.push(url.host);
warningPanel.destroy();
});
warningPanel.port.emit('url', tab.url);
warningPanel.show();
}
}
// Add events to track tabs on open and ready states
function setupTabTracking() {
tabs.on('ready', function onOpen(tab) {
// A page was loaded. If it is the active tab, check it now.
// Otherwise, it will be checked upon activation.
if (tab == tabs.activeTab)
checkTab(tab);
});
tabs.on('activate', function onReady(tab) {
checkTab(tab);
});
}
setupTabTracking();