This repository has been archived by the owner on Dec 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
background.js
330 lines (306 loc) · 11.7 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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/**
* @fileOverview This script sets up the extension button on the toolbar and handles authentication with the content server.
* @author Sanchit Karve
* @version 0.1.2.2
**/
// Quick-Hack to load JQuery before Background process loads.
document.write("<script type=\"text/javascript\" src=\"scripts/jquery-1.7.2.min.js\"></script>");
// Add a button to Opera's toolbar when the extension loads.
window.addEventListener("load", function() {
// Buttons are members of the UIItem family.
// Firstly we set some properties to apply to the button.
var UIItemProperties = {
//disabled: false, The button is disabled.
// The tooltip title.
title: "Privly",
// The icon (18x18) to use for the button.
icon: "icons/logo_64.png",
// The Popup menu to be displayed when the button is clicked.
popup: {
href: "popup.html",
width: 200,
height: 320
},
// The badge title used to display the current operating mode of the extension.
badge: {
display: "none",
textContent: "A",
color: "white",
backgroundColor: "rgba(211, 0, 4, 1)"
}
/*onclick: function() {
// Send a message to the currently-selected tab.
var tab = opera.extension.tabs.getFocused();
if (tab) { // Null if the focused tab is not accessible by this extension
//TODO: Use this button onClickHandler to submit content to Privly (Allow user to select option)
//tab.postMessage('go');
}
}*/
};
// Next, we create the button and apply the above properties.
extensionButton = opera.contexts.toolbar.createItem(UIItemProperties);
// Finally, we add the button to the toolbar.
opera.contexts.toolbar.addItem(extensionButton);
if(widget.preferences.authToken) {
privlyAuthentication.setAuthToken(widget.preferences.authToken);
}
else {
privlyAuthentication.setAuthToken("");
}
// Disables the button if no tabs are open.
function enableButton() {
var tab = opera.extension.tabs.getFocused();
if (tab) {
extensionButton.disabled = false;
}
else {
extensionButton.disabled = true;
}
}
// Enable the button when a tab is ready.
// Prevents user from attempting to post content when no tabs are open.
// opera.extension.onconnect = enableButton;
// opera.extension.tabs.onfocus = enableButton;
// opera.extension.tabs.onblur = enableButton;
/**
* Sends a message to the injected script to tie it with this script for inter-script communication.
*/
opera.extension.onconnect = function(event) {
// Post message to the source, that is, the thing which connected to us (in this case the injected script)
event.source.postMessage({type: "privly:init"});
// Post this message in the opera error console
//opera.postError("sent privly:init");
}
/**
* Listen for messages.
*/
opera.extension.onmessage = function(event) {
if(event.data.type == "contentPost") {
postInfo.content = event.data.message;
postInfo.postTag = event.data.inputTag;
postInfo.postLocation = event.data.inputLocation;
}
// Post a sentence (which includes the message received) to the opera error console
//opera.postError("This is what I got from injected script: "+event.data);
}
}, false);
/**
* Reference to the extension's button.
*/
var extensionButton;
/*
* enum to hold various extension modes and their value. extension modes are set through Opera's
* extension api.
*/
var extensionModeEnum = {
ACTIVE: 0,
PASSIVE: 1,
CLICKTHROUGH: 2
};
//TODO: Remove postLocation and related code. No longer needed.
var postInfo = {
content: "",
postTag: "",
postLocation: ""
};
/**
* Broadcasts a message to all injected scripts to notify an operating mode change.
*
* @param {int} newMode The new operating mode.
*/
function broadcastModeChange(newMode) {
opera.postError("initiate broadcast mode call");
opera.extension.broadcastMessage({type : "extensionModeChange", mode: newMode});
}
/**
* @namespace
* Handles authentication with the remote server.
*
*/
var privlyAuthentication = {
//When added to every request to the content server
//as the parameter auth_token, the extension has access
//to the referenced user account
authToken: "",
/**
* Saves the authentication Token to Opera's extension data store (AKA preference store).
*
* @param {string} auth The authentication token.
*/
setAuthToken : function(auth) {
privlyAuthentication.authToken = auth;
widget.preferences.authToken = auth;
},
/**
* Retrieves the authentication Token to Opera's extension data store (AKA preference store).
*
* @returns {string} The authentication token.
*/
getAuthToken : function() {
return privlyAuthentication.authToken;
},
/**
* Logs in to the content server and gets authentication token if successful.
*
* @param {string} The email address of the user.
*
* @param {string} The password of the user.
*/
loginToPrivly : function(userEmailAddress, userPassword) {
// Opera 11.x does not support cross-origin scripting.
// Rumors suggest Opera 12 will fix it.
// Until then, jQuery provides a nice hack.
$.support.cors = true;
//opera.postError('pre-login-ajax');
$.ajax({
//TODO: Get version number directly from config.xml
data: {
email: userEmailAddress,
password: userPassword,
endpoint: "extension",
browser: "opera",
version: "0.1.1.3"
},
type: "POST",
//TODO:Use opera.widgets.preferences to store contentServerUrl and other strings. Remove hardcoded url
//url: privlyExtension.preferences.getCharPref("contentServerUrl")+"/token_authentications.json",
url: "https://priv.ly/token_authentications.json",
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
dataType: "json",
accepts: "json",
success: function(data, textStatus, jqXHR) {
privlyAuthentication.setAuthToken(data.auth_key);
if(privlyAuthentication.getAuthToken()) {
//opera.postError('loginsuccess');
//Broadcast Success Message to Preferences Page (options.html)
opera.extension.broadcastMessage({type : "loginSuccess"});
}
else {
//Broadcast Login Failed Message to Preferences Page (options.html)
privlyAuthentication.setAuthToken("");
//opera.postError('loginfailed');
opera.extension.broadcastMessage({type : "loginFailed"});
}
},
error: function(data, textStatus, jqXHR) {
//Broadcast Server Error Message to Preferences Page (options.html)
//opera.postError('unreachable');
opera.extension.broadcastMessage({type : "loginServerUnreachable"});
}
});
},
/**
* Send data anonymously from the right-clicked form element to the content server, then
* assign the form element to the returned URL.
*/
postAnonymouslyToPrivly: function () {
// Opera 11.x does not support cross-origin scripting.
// Rumors suggest Opera 12 will fix it.
// Until then, jQuery provides a nice hack.
$.support.cors = true;
$.ajax({
//TODO: Get version number directly from config.xml
data: {
auth_token: privlyAuthentication.authToken,
"post[content]": postInfo.content,
"post[public]": true,
endpoint: "extension",
browser:"opera",
version:"0.1.1.3"
},
type: "POST",
//TODO:Use opera.widgets.preferences to store contentServerUrl and other strings. Remove hardcoded url
//url: privlyExtension.preferences.getCharPref("contentServerUrl")+"/token_authentications.json",
url: "https://priv.ly/posts/posts_anonymous.json",
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
success: function(data, textStatus, jqXHR) {
//Broadcast Success Message to Preferences Page (options.html)
opera.extension.broadcastMessage({type: "postContentSuccess", message:jqXHR.getResponseHeader("privlyurl"), post: postInfo});
postInfo.content = "";
},
error: function(data, textStatus, jqXHR) {
//Broadcast Server Error Message to Preferences Page (options.html)
opera.extension.broadcastMessage({type: "postContentFailed"});
}
});
},
/**
* Sends new content to the content server.
* @param {string} privacySetting Indicates the privacy level of the post.
* Accepted values are 'public', and anything else.
* Unlike the firefox extension, even if the extension is set
* to "allPostsArePublic", this parameter is NOT ignored.
* If "allPostsArePublic" is not true, and privacySetting is anything other
* than public, the post is made as private.
*/
postToPrivly : function(privacySetting) {
// Opera 11.x does not support cross-origin scripting.
// Rumors suggest Opera 12 will fix it.
// Until then, jQuery provides a nice hack.
$.support.cors = true;
//Currently, declaring allPostsPublic is unnecessary
//var allPostsPublic = (widget.preferences.allPostsPublic == "true");
var postPrivacySetting = (privacySetting === 'public');
$.ajax({
//TODO: Get version number directly from config.xml
data: {
auth_token: privlyAuthentication.authToken,
"post[content]": postInfo.content,
"post[public]": postPrivacySetting,
endpoint: "extension",
browser:"opera",
version:"0.1.1.3"
},
type: "POST",
//TODO:Use opera.widgets.preferences to store contentServerUrl and other strings. Remove hardcoded url
//url: privlyExtension.preferences.getCharPref("contentServerUrl")+"/token_authentications.json",
url: "https://priv.ly/posts",
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
success: function(data, textStatus, jqXHR) {
//Broadcast Success Message to Preferences Page (options.html)
opera.extension.broadcastMessage({type: "postContentSuccess", message:jqXHR.getResponseHeader("privlyurl"), post: postInfo});
postInfo.content = "";
},
error: function(data, textStatus, jqXHR) {
//Broadcast Server Error Message to Preferences Page (options.html)
opera.extension.broadcastMessage({type: "postContentFailed"});
}
});
},
/**
* Logs out from the content server. Destroys authentication token if successful.
*/
logoutFromPrivly : function() {
// Opera 11.x does not support cross-origin scripting.
// Rumors suggest Opera 12 will fix it.
// Until then, jQuery provides a nice hack.
$.support.cors = true;
//opera.postError("pre-ajax");
$.ajax({
data: {
_method: "delete",
endpoint: "extension",
browser: "opera",
version: "0.1.1.3",
auth_token: privlyAuthentication.getAuthToken()
},
type: "POST",
//url: privlyExtension.preferences.getCharPref("contentServerUrl")+"/token_authentications.json",
url: "https://priv.ly/token_authentications.json",
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
dataType: "json",
accepts: "json",
success: function(data, textStatus, jqXHR) {
privlyAuthentication.setAuthToken("");
//alert("You are logged out from Priv.ly");
//opera.postError("S");
opera.extension.broadcastMessage({type : "logoutSuccess"});
},
error: function(data, textStatus, jqXHR) {
//alert(data.error);
//opera.postError("F");
opera.extension.broadcastMessage({type : "logoutFailure", message: data.error});
}
});
}
}