aboutsummaryrefslogtreecommitdiff
path: root/main_background.js
blob: 27b788b17258bd26155e6c5ac86e50554c1fc2b8 (plain) (blame)
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
console.debug("main_background.js");

/**
*	
*	Sets global variable "webex" to either "chrome" or "browser" for
*	use on Chrome or a Firefox variant.
*
*	Change this to support a new browser that isn't Chrome or Firefox,
*	given that it supports webExtensions.
*
*	(Use the variable "webex" for all API calls after calling this)
*/
var webex;
function set_webex(){
	if(typeof(browser) == "undefined"){
		webex = chrome;
	} else{
		webex = browser;
	}
}

/*
*
*	Called when something changes the persistent data of the add-on.
*
*	The only things that should need to change this data are:
*	a) The "Whitelist this page" button
*	b) The options screen
*
*	When the actual blocking is implemented, this will need to comminicate
*	with its code to update accordingly
* 
*	The object stored in storage will look like this:
*
*
*	{
*		pref_body : "",
*		pref_complaint_tab : "",
*		pref_notify_analyze : "",
*		pref_subject : "",
*		// The domains that are "blanket" whitelisted
*		pref_whitelist : {
*			"a.com" : true,
*			"b.com" : true
*		},
*		// Individual scripts that have been whitelisted
*		whitelist : {
*			"example.com":{
*				"a.js" : true
*			},
*			"domain.com": {
*				"a.js" : true,
*				"b.js" : false
*			}
*		}
*	}
*	
*
*
*
*
*
*
*/
function options_listener(changes, area){
	console.log("Items updated in area" + area +": ");

	var changedItems = Object.keys(changes);
	var changed_items = "";
	for (var i = 0; i < changedItems.length; i++){
		var item = changedItems[i];		
		changed_items += item + ",";
	}
	console.log(changed_items);

}
/**
*	Executes the "Display this report in new tab" function
*	by opening a new tab with whatever HTML is in the popup
*	at the moment.
*/
function open_popup_tab(){
	function gotPopup(popupURL){
		var creating = webex.tabs.create({"url":popupURL});
	}

	var gettingPopup = webex.browserAction.getPopup({},gotPopup);
}

/**
*
*	This is what you call when a page gets changed to update the info box.
*
*	Sends a message to the content script that updates the popup for a page.
*
*	var example_blocked_info = {
*		"accepted": [["REASON 1","SOURCE 1"],["REASON 2","SOURCE 2"]],
*		"blocked": [["REASON 1","SOURCE 1"],["REASON 2","SOURCE 2"]],
*		"url": "example.com"
*	}
*
*/

var active_connections = {};
var unused_data = {};
function update_popup(tab_id,blocked_info){
	// this will happen almost every time (browser action not opened before javascript has been filtered)
	// store the blocked info until it is opened and needed
	if(active_connections[tab_id] === undefined){
		console.log("[TABID:"+tab_id+"]"+"Storing blocked_info for when the browser action is opened.");
		unused_data[tab_id] = blocked_info; 
	} else{
		console.log("[TABID:"+tab_id+"]"+"Sending blocked_info directly to browser action");
		active_connections[tab_id].postMessage({"show_info":blocked_info});
		delete active_connections[tab_id];
	}
}
/**
*
*	This is the callback where the content scripts of the browser action will contact the background script.
*
*/
var portFromCS;
function connected(p) {
	console.log("Message:");	
	console.log(p);
	p.onMessage.addListener(function(m) {

		/**
		* Updates the entry of the current URL in whitelist with
		*
		*/
		function set_script(script,tof){
			console.log("setting script '" + script + "' whitelisted status to "+tof);
			// Remember that we do not trust the names of whitelisted scripts.
			var current_url = "";
			function geturl(tabs) {
				// Got the URL of the current open tab
				current_url = tabs[0]["url"];
				function storage_got(items){
					console.log("got storage:");
					console.log(items);
					var new_items = items;
					if(new_items["whitelist"] === undefined){
						new_items["whitelist"] = {};
					}
					if(new_items["whitelist"][current_url] === undefined){
						new_items["whitelist"][current_url] = {};
					}
					console.log(script);
					new_items["whitelist"][current_url][script] = tof;
					webex.storage.local.set(new_items);			
				}
				webex.storage.local.get(storage_got);
			}
			var querying = webex.tabs.query({active: true,currentWindow: true},geturl);			
			return;
		}
		if(m["whitelist_script"] !== undefined){
			set_script(m["whitelist_script"][0],true);
		}
		if(m["unwhitelist_script"] !== undefined){
			set_script(m["whitelist_script"][0],false);
		}
		function logTabs(tabs) {
			for(var i = 0; i < tabs.length; i++) {
				var tab = tabs[i]
				var tab_id = tab["id"]
				console.log(tab_id)
				if(unused_data[tab_id] !== undefined){
					// If we have some data stored here for this tabID, send it and then delete our copy 	
					console.log("[TABID:"+tab_id+"]"+"Sending stored data associated with browser action");								
					p.postMessage({"show_info":unused_data[tab_id]});
				} else{
					// create a new entry
					unused_data[tab_id] = {"url":tab["url"],"blocked":"","accepted":""};
					p.postMessage({"show_info":unused_data[tab_id]});							
					console.log("[TABID:"+tab_id+"]"+"No data found, creating a new entry for this window.");	
				}
			}
		}
		var querying = webex.tabs.query({active: true,currentWindow: true},logTabs);
		
	});
}

/**
*	The callback for tab closings.
*
*	Delete the info we are storing about this tab if there is any.
*
*/
function delete_removed_tab_info(tab_id, remove_info){
	console.log("[TABID:"+tab_id+"]"+"Deleting stored info about closed tab");
	if(unused_data[tab_id] !== undefined){
		delete unused_data[tab_id];
	}
	if(active_connections[tab_id] !== undefined){
		delete active_connections[tab_id];
	}
}


/**
*	Initializes various add-on functions
*	only meant to be called once when the script starts
*/
function init_addon(){
	set_webex();
	webex.runtime.onConnect.addListener(connected);
	webex.storage.onChanged.addListener(options_listener);
	webex.tabs.onRemoved.addListener(delete_removed_tab_info);
}


init_addon();

/**************** some misc. debugging: ***************************/


function debug_print_local(){
	function storage_got(items){
		console.log("Local Storage:");
		console.log(items);
	}
	webex.storage.local.get(storage_got);
}

function clr_local(){
	webex.storage.local.set({});				
}

// Valid input for update_popup
var example_input = {
	"accepted": [["FILENAME 1","REASON 1"],["FILENAME 2","REASON 2"]],
	"blocked": [["FILENAME 1","REASON 1"],["FILENAME 2","REASON 2"]],
	"url":"example.com"
};
update_popup(2,example_input);
// To test the default text
//example_input["accepted"] = [];
//example_input["blocked"] = [];

debug_print_local();