-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
165 lines (109 loc) · 3.63 KB
/
background.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
/* Extensions that are used in this script */
if (typeof String.prototype.startsWith != 'function') {
String.prototype.startsWith = function (str){
return this.slice(0, str.length) == str;
};
}
//Defines the actions that can be used
var actions = Object.freeze({
RELOAD: "reload",
FORCERELOAD: "force"
});
//Allows different display targets to be used via constants
var displayTypes = Object.freeze({
CONSOLE: "CONSOLE",
PAGE: "PAGE"
});
//controls the operation of the extension
var Controller = (function () {
//constructor
function Controller () {
//Tab we are currently controlling
this.activeTargetId = 0;
//Where is the data being displayed
this.displayTarget = displayTypes.PAGE;
//load the users settings
this.loadSettings();
}
//send request to regenerate field data
Controller.prototype.update = function (userCommand) {
if (this.activeTargetId !== 0) {
chrome.tabs.sendMessage(this.activeTargetId, { action: actions.RELOAD, target: this.displayTarget, command: userCommand });
}
};
//checks that a given url is in the approved list managed by the user
Controller.prototype.isApprovedUrl = function (urlToApprove) {
return (urlToApprove.startsWith("http://") || urlToApprove.startsWith("https://"));
};
//clears the active tab
Controller.prototype.clearActiveTab = function () {
this.activeTargetId = 0;
};
//reads the latest settings
Controller.prototype.updateSettings = function (newSettings) {
if (newSettings) {
this.displayTarget = newSettings.display;
}
};
//load user settings and apply them
Controller.prototype.loadSettings = function () {
var _this = this;
evadi.blabr.data.getSettings(function (settings) {
_this.updateSettings(settings);
});
};
return Controller;
})();
//create the main controller instance
var controller = new Controller();
//find the active tab
function findActiveTab() {
//get information about this tab
chrome.tabs.query({ active: true, status: "complete", lastFocusedWindow: true }, function (tabs) {
//we may have more than one result if the tab has dev tools open
tabs.forEach(function (tab) {
setActiveTabIfValidUrl(tab);
});
});
}
//set the active tab if the tab url matches an approved one
function setActiveTabIfValidUrl(tab) {
if (controller.isApprovedUrl(tab.url)) {
//make this the currently active tab
controller.activeTargetId = tab.id;
}
}
/* Chrome API's */
//Capture requests from other areas of the extension
chrome.extension.onMessage.addListener(function (request, sender, sendRequest) {
if (request.action == actions.FORCERELOAD) {
controller.activeTargetId = sender.tab.id;
controller.update();
}
});
//when a tab is updated, and only when it is complete then check it's url
//if it matches the approved list then hold on to it's id
chrome.tabs.onUpdated.addListener(function (tabId, change, tab) {
if (change.status == "complete") {
setActiveTabIfValidUrl(tab);
}
});
//when a tab becomes active, then check it's url
//if it matches the approved list then hold onto it's id
chrome.tabs.onActivated.addListener(function (activeInfo) {
findActiveTab();
});
//capture window switching events
chrome.windows.onFocusChanged.addListener(function(windowId) {
findActiveTab();
});
//Listen for keyboard shortcut presses
chrome.commands.onCommand.addListener(function (command) {
switch (command) {
case "hidden_fields":
case "max_lengths":
case "toggle_validation":
controller.update(command);
break;
}
});