-
Notifications
You must be signed in to change notification settings - Fork 10
/
contentScript.js
39 lines (32 loc) · 1.25 KB
/
contentScript.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
(function(){
var token = extractTokenFromURL(window.location.href);
if (token) {
chrome.storage.sync.set({ 'token': token });
chrome.storage.sync.set({ 'papers': [] });
//get initial paper list
var searchUrl = 'https://api.dropboxapi.com/2/paper/docs/search';
var searchXhr = new XMLHttpRequest();
searchXhr.open('POST', searchUrl , true);
searchXhr.setRequestHeader('Authorization', 'Bearer ' + token);
searchXhr.setRequestHeader('Content-Type', 'application/json');
searchXhr.onreadystatechange = function () {
if (searchXhr.readyState == XMLHttpRequest.DONE && searchXhr.status == 200) {
var allDocs = JSON.parse(searchXhr.responseText);
chrome.extension.sendMessage({docs: allDocs});
}
}
var searchQuery = {
'query': 'a OR b OR c OR d OR e OR f OR g OR h OR i OR j OR k OR l OR m OR n OR o OR p OR q OR r OR s OR t OR u OR v OR w OR x OR y OR z',
'limit': 50
}
searchXhr.send(JSON.stringify(searchQuery));
}
})();
function extractTokenFromURL(url) {
var urlFragment = url.match(/[#?](.*)/);
if (!urlFragment || urlFragment.length < 1) {
return null;
}
var params = new URLSearchParams(urlFragment[0].split("#")[1]);
return params.get("access_token");
}