-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPocketAPI.js
More file actions
109 lines (99 loc) · 3.11 KB
/
PocketAPI.js
File metadata and controls
109 lines (99 loc) · 3.11 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
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
// Copyright (c) 2014 Lucas Czekaj
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var Auth = {
CONSUMER_KEY: '32210-4ca8d411325286c8f31157d5',
REDIRECT_URI: chrome.extension.getURL('auth.html'),
OAUTH_PATH: 'oauth/',
getRequestCode: function() {
var data = {
consumer_key: Auth.CONSUMER_KEY,
redirect_uri: Auth.REDIRECT_URI,
};
return PocketAPI.call(OAUTH_PATH + 'request', data).then(function(data) {
return data.code;
});
},
askUserForPermission: function(requestCode) {
localStorage['auth_request_code'] = requestCode;
var url = 'https://getpocket.com/auth/authorize' +
'?request_token=' + requestCode +
'&redirect_uri=' + Auth.REDIRECT_URI;
window.open(url);
},
onUserPermissionReceived: function() {
var requestCode = localStorage['auth_request_code'];
return Auth.getAccessToken(requestCode).then(function(data) {
localStorage.removeItem('auth_request_code');
localStorage['auth_access_token'] = data.access_token;
localStorage['auth_username'] = data.username;
});
},
getAccessToken: function(requestCode) {
var params = {
consumer_key: Auth.CONSUMER_KEY,
code: requestCode,
};
return PocketAPI.call(OAUTH_PATH + 'authorize', params);
},
authNeeded: function() {
console.log('auth_access_token=='+localStorage['auth_access_token'])
return localStorage['auth_access_token'] == null;
},
}
var PocketAPI = {
REQUEST_CODE: null,
call: function(method, data) {
return new Promise(function(resolve, reject) {
var req = new XMLHttpRequest();
req.onload = function() {
if (req.status != 200) {
reject(Error(req.statusText));
return;
}
resolve(JSON.parse(req.responseText));
};
req.onerror = function() {
reject(Error("XHR error"));
};
req.open("POST", 'https://getpocket.com/v3/' + method);
req.setRequestHeader('X-Accept', 'application/json');
req.setRequestHeader('Content-Type', 'application/json; charset=UTF8');
req.send(JSON.stringify(data));
});
},
randomize: function() {
if (Auth.authNeeded())
{
console.log('auth needed')
Auth.getRequestCode().then(function(code){
localStorage['auth_request_code'] = code;
Auth.askUserForPermission(code);
console.log('username_after_auth='+localStorage['auth_username'])
})
}
else
{
console.log('username_no_auth=' + localStorage['auth_username']);
console.log('randomizing!');
var randomizing_params = {
consumer_key: Auth.CONSUMER_KEY,
access_token: localStorage['auth_access_token'],
offset: Math.floor((Math.random() * 700) + 1),
count: 1
};
PocketAPI.call('get',randomizing_params).then(function(data){
for (item in data.list)
{
var newURL = data.list[item].resolved_url
console.log('url='+newURL)
var newItemId = data.list[item].item_id;
chrome.tabs.query({'active': true, 'windowId': chrome.windows.WINDOW_ID_CURRENT},
function(tabs){
chrome.tabs.update(tabs[0].id, {url: 'http://getpocket.com/a/read/' + newItemId});
});
}
})
}
},
}