-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.html
233 lines (199 loc) · 9.43 KB
/
main.html
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
<!DOCTYPE html>
<html>
<head>
<title>UpShot safari Extension</title>
</head>
<script type="text/javascript" charset="utf-8">
// ////////////////////////////////////////////////////////////////////////
// Here we are in the global safari application.
// No access to web pages, but to safari app elements and listening scripts.
// ////////////////////////////////////////////////////////////////////////
function myCommandHandler(event){
if( event.command==="upshot" && safari.extension.settings.is_opened===0 && has_credentials() ){
var imageUrl = safari.application.activeBrowserWindow.activeTab.visibleContentsAsDataURL();
safari.extension.settings.upshot_safari_screenshot = imageUrl;
var box = '';
box = box + "\
<div id='upshot_safari_box'>";
if(safari.extension.settings.upshot_last_public!=undefined && safari.extension.settings.upshot_last_public!=null && safari.extension.settings.upshot_last_public!=""){
box = box + "\
<div id='last_public'>\
Last public upshot :<br/>\
<textarea readonly='readonly' cols='31' rows='3'>"+ safari.extension.settings.upshot_last_public +"</textarea>\
</div>";
}
box = box + "\
<img id='base64' name='upshot[base64]' src='" + imageUrl + "' alt='image.png' />\
<div id='accounts'>\
<select id='upshot_safari_select'>";
user_accounts = JSON.parse(safari.extension.secureSettings.upshot_safari_accounts);
for(u in user_accounts){
if(user_accounts[u].role!="customer"){
box = box + "<option value='"+ user_accounts[u].subdomain +"'>"+ user_accounts[u].name +"</option>";
}
}
box = box + "</select>\
</div>\
<p id='buttons'>\
<button type='button' name='commit' value='Draft' id='Draft' formnovalidate='formnovalidate'>Draft</button>\
<button type='button' name='commit' value='Public' id='Public' formnovalidate='formnovalidate'>Public</button>\
<a id='upshot_safari_cancel' class='red' type='reset' href='#'>Cancel</a>\
</p>\
</div>";
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage("shot", box);
safari.extension.settings.is_opened = 1;
// disabled extension while screenshooting, avoids to have several boxes
event.target.disabled = true;
} else{
connexion(event);
}
}
function myValidateHandler(event){
if (event.command === "upshot") {
// Is there an URL loaded in the tab.
var has_url = event.target.browserWindow.activeTab.url;
// Disable the button if there is no URL loaded in the tab.
event.target.disabled = !has_url;
// if there is no url, say it's already open so it won't try
// otherwise, say it's not opened so it will be able to try.
safari.extension.settings.is_opened = has_url ? 0 : 1;
}
}
function myMessageHandler(event) {
if (event.name === "upshot_safari_extension") {
if( event.message === "activate" ){
// Is there an URL loaded in the tab.
var has_url = event.target.browserWindow.activeTab.url;
safari.extension.toolbarItems[0].disabled = !has_url;
safari.extension.settings.is_opened = 0;
}
} else if(event.name==="upshot_safari_submission"){
// Screnshot taken and draft button pressed
submission(event.message);
} else if(event.name==="upshot_safari_public_submission"){
// Screnshot taken and public button pressed
public_submission(event.message);
} else if(event.name==="upshot_login"){
// open new tab with http://upshot.dev/login
var newTab = safari.application.activeBrowserWindow.openTab();
newTab.url = "http://"+ safari.extension.settings.upshot_web_site +"/login";
} else if(event.name==="upshot_safari_credentials"){
// After an error of sending
request_credentials(event);
}
}
// Listen to events
// 'command' events are received once click on extension is done
// 'validate' events are received from safari to ensure the command is ready to be executed
// 'message' events are received from injected script
safari.application.addEventListener("command", myCommandHandler, false);
safari.application.addEventListener("validate", myValidateHandler, false);
safari.application.addEventListener("message", myMessageHandler, false);
// /////////
// UPSHOT
// /////////
// Test if accounts list is set
function has_credentials(){
if (safari.extension.secureSettings.upshot_safari_accounts === undefined || safari.extension.secureSettings.upshot_safari_accounts === "" ){
return false;
} else return true;
}
// Restrieve user's id in localStorage["upshot_id"].
function connexion(event){
var xhr = new XMLHttpRequest();
// var params = "email="+email+"&token="+token;
xhr.open("GET", "http://" + safari.extension.settings.upshot_web_site + "/users/get_id.json", true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
// JSON.parse does not evaluate the attacker's scripts.
var resp = JSON.parse(xhr.responseText);
// console.log("l.109 connexion() ");
// console.log(" id: "+ resp.id);
// console.log(" state: "+ resp.state);
// console.log(" offset: "+ resp.offset);
// console.log(" accounts : "+ resp.user_accounts);
// console.log(" status: "+ resp.status);
if(resp.status==200){
safari.extension.secureSettings.upshot_safari_user_id = resp.id;
if(resp.user_accounts!=null && resp.user_accounts!="null" ){
safari.extension.secureSettings.upshot_safari_accounts = JSON.stringify(resp.user_accounts);
}
// Self message to try relaunching screenshooting after saving settings
myCommandHandler({"command": "upshot", "target": self});
} else {
safari.extension.secureSettings.upshot_safari_user_id = "";
safari.extension.secureSettings.upshot_safari_accounts = "";
if( !has_credentials() ){
request_credentials(event);
}
}
}
}
xhr.send();
}
function submission(subdomain){
// Form submission by clicking the 'Draft' button
// SENDING
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://" + subdomain + "." + safari.extension.settings.upshot_web_site + "/upshots", true);
// Create form to handle callback
var formData = new FormData();
formData.append("upshot[base64]", safari.extension.settings.upshot_safari_screenshot);
formData.append("upshot[title]", safari.application.activeBrowserWindow.activeTab.title);
formData.append("upshot[temporary_owner_tag_list]", "");
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage("done", xhr.status);
}
}
xhr.send(formData);
}
function public_submission(subdomain){
// Form submission by clicking the 'Draft' button
// SENDING
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://" + subdomain + "." + safari.extension.settings.upshot_web_site + "/upshots", true);
// Create form to handle callback
var formData = new FormData();
formData.append("upshot[base64]", safari.extension.settings.upshot_safari_screenshot);
formData.append("upshot[title]", safari.application.activeBrowserWindow.activeTab.title);
formData.append("upshot[temporary_owner_tag_list]", "");
formData.append("upshot[is_public]", "1");
formData.append("save_as_active", "true");
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
safari.extension.settings.upshot_last_public = JSON.parse(xhr.responseText).public_hash;
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage("public_done", xhr);
}
}
xhr.send(formData);
}
function request_credentials(event){
var box = '';
box = box + "\
<div id='upshot_safari_box'>\
<p>\
<a id='upshot_safari_close' type='reset' formnovalidate='formnovalidate'>\
<img src='"+ safari.extension.baseURI +"cancel.png' />\
</a>\
</p>\
<div id='upshot_safari_logo'>\
<img src='" + safari.extension.baseURI + "logo.png' alt='UpShot'>\
<p>Safari Extension</p>\
</div>\
<p>\
You're not logged (anymore) in UpShot.<br/>\
<small>Please log in to send upshots to your account(s)</small><br/>\
</p>\
<p id='upshot_safari_login'>\
<button type='button' formnovalidate='formnovalidate'>Login</button>\
</p>\
</div>";
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage("credentials", box);
safari.extension.settings.is_opened = 1;
// disabled extension while screenshooting, avoids to have several boxes
event.target.disabled = true;
}
</script>
<body></body>
</html>