-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
59 lines (49 loc) · 1.5 KB
/
auth.js
File metadata and controls
59 lines (49 loc) · 1.5 KB
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
var $ = require('jquery');
require('../css/auth.css');
var waitForChrome = function(callback) {
if ( typeof(chrome) === 'undefined') {
window.setTimeout(function() {
waitForChrome(callback);
}, 250);
} else {
callback();
}
};
$(function() {
waitForChrome(function() {
var url = null;
var search = window.location.search;
console.log('Search is' + search);
if(search.length > 1 && search.charAt(0) == '?') {
search = search.substr(1);
}
if (search.length > 0) {
var terms = search.split("&");
for(var i = 0; i < terms.length; i++) {
var key = terms[i].split('=')[0];
var value = terms[i].split('=')[1];
if (key === 'url') {
// This is only way, which I managed to get URL with symbols like (=,?,...) through login process
url = decodeURIComponent(window.atob(value));
}
}
}
if(url === 'null') {
// The tab originally had no URL
url = 'about:blank';
}
console.log('Response with ' + url);
if (url) {
chrome.runtime.sendMessage({command: 'getOauthRequestToken'}, function(err, reqToken) {
chrome.runtime.sendMessage({command: 'getOauthAccessToken'}, function(err, accessToken) {
if (err) {
console.warn('Could not authenticate with pocket: ' + JSON.stringify(err));
return;
}
console.log('Authentication to pocket successful.');
window.location.href = url;
});
});
}
});
});