-
Notifications
You must be signed in to change notification settings - Fork 0
/
importer-api.js
83 lines (71 loc) · 1.5 KB
/
importer-api.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
/**
* Mendeley Importer API
*
* Exposes methods for interacting with the importer bookmarket:
*
* open()
* close()
* registerDataCallback()
* registerUserIdentityCallback()
* registerHostId()
*/
MendeleyImporterApi = {};
(function(api) {
var data = {},
bookmarkletUrl = 'https://www.mendeley.com/minified/bookmarklet.js';
// Main
init();
/**
* Initialise the API
*/
function init() {
api.open = open;
api.close = close;
api.setOpenedWithApi = getSetter('openedWithApi');
api.getOpenedWithApi = getGetter('openedWithApi');
// Create methods for getting/setting data and callbacks
var methods = ['HostId', 'UserIdentityCallback', 'DataCallback'];
for(var i=0, l=methods.length; i < l; i++) {
var key = methods[i];
api['register' + key] = getSetter(key);
api['get' + key] = getGetter(key);
}
}
/**
* Generate getter
*
* @param string key for data
*/
function getGetter(key) {
return function() {
return data[key];
};
}
/**
* Generate setter
*
* @param string key for data
*/
function getSetter(key) {
return function(value) {
data[key] = value;
};
}
/**
* Open the bookmarklet
*/
function open() {
this.setOpenedWithApi(true);
document.getElementsByTagName('body')[0]
.appendChild(document.createElement('script'))
.setAttribute('src', bookmarkletUrl);
}
/**
* Close the bookmarklet
*/
function close() {
try {
document.getElementById('mendeley-bookmarklet-close').click();
} catch (e) {}
}
})(MendeleyImporterApi);