-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
connector.js
150 lines (133 loc) · 4.71 KB
/
connector.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
if (!Date.prototype.toISODate) {
Date.prototype.toISODate = function() {
return this.getFullYear() + '-' +
('0' + (this.getMonth() + 1)).slice(-2) + '-' +
('0' + this.getDate()).slice(-2);
}
}
Zotero.Connector = new function() {
this.callMethod = Zotero.Promise.method(function(options, data, cb, tab) {
throw new Error("JabRef: Tried to contact Zotero standalone: " + options);
});
this.callMethodWithCookies = function(options, data, tab) {
if (options === "saveItems") {
browser.storage.sync.get({'exportMode': 'bibtex', 'takeSnapshots': false, 'retrieveCitationCounts': false})
.then(configuration => {
// fetch current settings
console.debug("exportMode: " + configuration.exportMode);
console.debug("takeSnapshots: " + configuration.takeSnapshots);
console.debug("retrieveCitationCounts: " + configuration.retrieveCitationCounts);
let items = [];
if (configuration.retrieveCitationCounts) {
console.log("[scholar-citations] fetching citation counts...");
// create zsc compatible items
for (let i = 0; i < data.items.length; i++) {
let item = new ZscItem(data.items[i]);
// add internal metadata
item.setField('_externalRequest', false); // false: triggered from browser; true: triggered from JabRef
item.setStatus(false, true, false, false); // init: no success, item complete (initial assumption), no captcha, not too many requests
items.push(item);
}
// get citations counts for all items
zsc.processItems(items);
} else {
items = data.items;
}
this.convertToBibTex(items, configuration.exportMode, configuration.takeSnapshots)
.then((bibtex) => this.sendBibTexToJabRef(bibtex));
});
} else if (options === "saveSnapshot") {
// Ignore this
} else {
throw new Error("JabRef: Tried to contact Zotero standalone: " + options);
}
};
this.checkIsOnline = Zotero.Promise.method(function() {
var deferred = Zotero.Promise.defer();
// Pretend that we are connected to Zotero standalone
deferred.resolve(true);
return deferred.promise;
});
this.prepareForExport = function(items, takeSnapshots) {
for (var i = 0; i < items.length; i++) {
var item = items[i];
for (var j = 0; j < item.attachments.length; j++) {
var attachment = item.attachments[j];
var isLink = attachment.mimeType === 'text/html' || attachment.mimeType === 'application/xhtml+xml';
if (isLink && attachment.snapshot !== false) {
// Snapshot
if (takeSnapshots && attachment.url) {
attachment.localPath = attachment.url;
} else {
// Ignore
}
} else {
// Normal file
// Pretend we downloaded the file since otherwise it is not exported
if (attachment.url) {
attachment.localPath = attachment.url;
}
}
}
// Fix date string
if (item.accessDate) {
item.accessDate = new Date().toISODate();
}
}
};
this.convertToBibTex = function(items, conversionMode, takeSnapshots) {
this.prepareForExport(items, takeSnapshots);
browser.runtime.sendMessage({
"onConvertToBibtex": "convertStarted"
});
return browser.tabs.query({
currentWindow: true,
active: true
}).then(tabs => {
for (let tab of tabs) {
return browser.tabs.sendMessage(
tab.id, {
convertToBibTex: items,
conversionMode: conversionMode
}
);
}
})
};
this.sendBibTexToJabRef = function(bibtex) {
browser.runtime.sendMessage({
"onSendToJabRef": "sendToJabRefStarted"
});
console.log("JabRef: Send BibTeX to JabRef: %o", bibtex);
handleError = function(message, details, stacktrace) {
// Open error page
browser.tabs.create({
url: "/data/error.html?message="
+ encodeURIComponent(message)
+ "&details=" + encodeURIComponent(details ?? "")
+ "&stacktrace=" + encodeURIComponent(stacktrace ?? "")
})
}
browser.runtime.sendNativeMessage("org.jabref.jabref", {
"text": bibtex
})
.then(response => {
if (response.message === 'ok') {
console.log("JabRef: Got success response from JabRef with details %o", response.output);
browser.runtime.sendMessage({
"popupClose": "close"
});
} else if(response.message === 'error') {
console.error(`JabRef: Error connecting to JabRef: '${response.output}' at '${response.stacktrace}'`);
handleError(response.output, '', response.stacktrace)
} else {
console.error(`JabRef: Error connecting to JabRef: '${response.message}' with details '${response.output}' at '${response.stacktrace}'`);
handleError(response.message, response.output, response.stacktrace)
}
})
.catch(error => {
console.error(`JabRef: Error connecting to JabRef: ${error}`);
handleError(error.message, '', error.stack)
});
}
};