-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.js
142 lines (98 loc) · 4.53 KB
/
upload.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
/*global chrome, alert, XMLHttpRequest, FormData, document, window, setTimeout */
function thereIsAnError(textToShow, errorToShow, imageUrl) {
"use strict";
document.getElementById('wrap').innerHTML = '<p></p><br/><br/><center><h1>Wow! Some error arrived!</h1></center><br/><br/><p>' + textToShow + '</p><br/><br/><p>' + errorToShow + '</p><p>' + imageUrl + '</p>';
}
/**
* Main function to upload an image
*
* @param {string} imageUrl URL of the uploaded image
* @param {string} fileName Name of the new uploaded file on VK documents
* @param {string} accToken Access token with vk authentication permissions
*/
function upload(imageUrl, fileName, accToken) {
"use strict";
var uploadHttpRequest = new XMLHttpRequest();
uploadHttpRequest.onload = function () {
var documentUploadServer = new XMLHttpRequest(),
requestFormData,
documentUploadRequest;
documentUploadServer.open('GET', 'https://api.vk.com/method/docs.getUploadServer?access_token=' + accToken);
documentUploadServer.onload = function () {
var answer = JSON.parse(documentUploadServer.response);
if (answer.error !== undefined) {
chrome.storage.local.remove('vkaccess_token');
document.getElementById('wrap').innerHTML = '<p></p><br/><br/><center><h1>Ops. Something went wrong. Please try again.</h1></center><br/>';
setTimeout(function () { window.close(); }, 3000);
return;
}
if (answer.response.upload_url === undefined) {
thereIsAnError('documentUploadServer response problem', answer, imageUrl);
return;
}
requestFormData = new FormData();
documentUploadRequest = new XMLHttpRequest();
requestFormData.append("file", uploadHttpRequest.response, fileName);
documentUploadRequest.open('POST', answer.response.upload_url, true);
documentUploadRequest.onload = function () {
var answer = JSON.parse(documentUploadRequest.response),
documentSaveRequest;
if (answer.file === undefined) {
thereIsAnError('Upload blob problem response problem', answer, imageUrl);
return;
}
documentSaveRequest = new XMLHttpRequest();
documentSaveRequest.open('GET', 'https://api.vk.com/method/docs.save?file=' + answer.file + '&access_token=' + accToken);
documentSaveRequest.onload = function () {
var answer = JSON.parse(documentSaveRequest.response);
if (answer.response[0].url === undefined) {
thereIsAnError('documentSaveRequest - no file in response', answer, imageUrl);
return;
}
document.getElementById('wrap').innerHTML = '<p></p><br/><br/><center><h1>Successfully uploaded!</h1></center><br/>';
setTimeout(function () { window.close(); }, 3000);
};
documentSaveRequest.send();
};
documentUploadRequest.send(requestFormData);
};
documentUploadServer.send();
};
uploadHttpRequest.responseType = 'blob';
uploadHttpRequest.open('GET', imageUrl);
uploadHttpRequest.send();
}
/**
* Add a listener for DOMContentLoaded event
*
* @param {string} Event name
* @param {function} Event handler
*/
document.addEventListener("DOMContentLoaded", function () {
"use strict";
var params = window.location.hash.substring(1).split('&'),
imageUrl = null,
filename,
imageName;
if (params === undefined || params.length === undefined || params.length !== 2) {
thereIsAnError('Parsing image url', 'params || params.length != 2', imageUrl);
return;
}
filename = params[0].split('/');
if (filename.length === undefined || filename.length === 0) {
thereIsAnError('Getting image filename', 'filename.length <= 0', imageUrl);
return;
}
imageUrl = params[0];
imageName = filename[filename.length - 1];
if (imageName.indexOf('?') > -1) {
imageName = imageName.slice(0, imageName.indexOf('?'));
}
if (imageName.indexOf('#') > -1) {
imageName = imageName.slice(0, imageName.indexOf('#'));
}
if (imageName.indexOf('&') > -1) {
imageName = imageName.slice(0, imageName.indexOf('&'));
}
upload(imageUrl, imageName, params[1]);
});