-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
135 lines (107 loc) · 4.35 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
/*global require, chrome, alert */
/**
* Display an alert with an error message, description
*
* @param {string} textToShow Error message text
* @param {string} errorToShow Error to show
*/
function displayeAnError(textToShow, errorToShow) {
"use strict";
alert(textToShow + '\n' + errorToShow);
}
/**
* Retrieve a value of a parameter from the given URL string
*
* @param {string} url Url string
* @param {string} parameterName Name of the parameter
*
* @return {string} Value of the parameter
*/
function getUrlParameterValue(url, parameterName) {
"use strict";
var urlParameters = url.substr(url.indexOf("#") + 1),
parameterValue = "",
index,
temp;
urlParameters = urlParameters.split("&");
for (index = 0; index < urlParameters.length; index += 1) {
temp = urlParameters[index].split("=");
if (temp[0] === parameterName) {
return temp[1];
}
}
return parameterValue;
}
/**
* Chrome tab update listener handler. Return a function which is used as a listener itself by chrome.tabs.obUpdated
*
* @param {string} authenticationTabId Id of the tab which is waiting for grant of permissions for the application
* @param {string} imageSourceUrl URL of the image which is uploaded
*
* @return {function} Listener for chrome.tabs.onUpdated
*/
function listenerHandler(authenticationTabId, imageSourceUrl) {
"use strict";
return function tabUpdateListener(tabId, changeInfo) {
var vkAccessToken,
vkAccessTokenExpiredFlag;
if (tabId === authenticationTabId && changeInfo.url !== undefined && changeInfo.status === "loading") {
if (changeInfo.url.indexOf('oauth.vk.com/blank.html') > -1) {
authenticationTabId = null;
chrome.tabs.onUpdated.removeListener(tabUpdateListener);
vkAccessToken = getUrlParameterValue(changeInfo.url, 'access_token');
if (vkAccessToken === undefined || vkAccessToken.length === undefined) {
displayeAnError('vk auth response problem', 'access_token length = 0 or vkAccessToken == undefined');
return;
}
vkAccessTokenExpiredFlag = Number(getUrlParameterValue(changeInfo.url, 'expires_in'));
if (vkAccessTokenExpiredFlag !== 0) {
displayeAnError('vk auth response problem', 'vkAccessTokenExpiredFlag != 0' + vkAccessToken);
return;
}
chrome.storage.local.set({'vkaccess_token': vkAccessToken}, function () {
chrome.tabs.update(
tabId,
{
'url' : 'upload.html#' + imageSourceUrl + '&' + vkAccessToken,
'active': true
},
function (tab) {}
);
});
}
}
};
}
/**
* Handle main functionality of 'onlick' chrome context menu item method
*/
function getClickHandler() {
"use strict";
return function (info, tab) {
var imageSourceUrl = info.srcUrl,
imageUploadHelperUrl = 'upload.html#',
vkCLientId = '3315996',
vkRequestedScopes = 'docs,offline',
vkAuthenticationUrl = 'https://oauth.vk.com/authorize?client_id=' + vkCLientId + '&scope=' + vkRequestedScopes + '&redirect_uri=http%3A%2F%2Foauth.vk.com%2Fblank.html&display=page&response_type=token';
chrome.storage.local.get({'vkaccess_token': {}}, function (items) {
if (items.vkaccess_token.length === undefined) {
chrome.tabs.create({url: vkAuthenticationUrl, selected: true}, function (tab) {
chrome.tabs.onUpdated.addListener(listenerHandler(tab.id, imageSourceUrl));
});
return;
}
imageUploadHelperUrl += imageSourceUrl + '&' + items.vkaccess_token;
chrome.tabs.create({url: imageUploadHelperUrl, selected: true});
});
};
}
/**
* Handler of chrome context menu creation process -creates a new item in the context menu
*/
chrome.contextMenus.create({
"title": "Rehost on vk.com",
"type": "normal",
"contexts": ["image"],
"onclick": getClickHandler()
});