-
Notifications
You must be signed in to change notification settings - Fork 117
/
global.js
301 lines (259 loc) · 9.96 KB
/
global.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
***** BEGIN LICENSE BLOCK *****
Copyright © 2017 Center for History and New Media
George Mason University, Fairfax, Virginia, USA
http://zotero.org
This file is part of Zotero.
Zotero is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Zotero is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Zotero. If not, see <http://www.gnu.org/licenses/>.
***** END LICENSE BLOCK *****
*/
Zotero.Connector_Browser = new function() {
var _selectCallbacksForTabIDs = {};
var _incompatibleVersionMessageShown;
var _zoteroButton;
this.activeTab = null;
/**
* Called when a new page has been loaded to clear previous translators
*/
this.onPageLoad = function(tab) {
_updateButtonStatus();
if (Zotero.Proxies.transparent) {
Zotero.Proxies.onPageLoadSafari(tab);
}
};
/**
* If there's a frame with a PDF mimeType this gets invoked
* @param frameURL
* @param tabId
*/
this.onPDFFrame = function(frameURL, instanceID, tab) {
if (tab.translators && tab.translators.length) {
return;
}
tab.isPDFFrame = true;
tab.instanceID = instanceID;
_updateButtonStatus(tab);
}
/**
* Called when translators are available for a given page
*/
this.onTranslators = function(translators, instanceID, contentType, tab) {
if (!translators.length) return;
tab.contentType = contentType;
let existingTranslators = tab.translators;
// If translators already exist for tab we need to figure out if the new translators
// are more important/higher priority
if (existingTranslators && existingTranslators.length) {
let existingTranslatorsHaveHigherPriority = existingTranslators[0].priority < translators[0].priority;
if (existingTranslatorsHaveHigherPriority) return;
let priorityEqual = translators[0].priority == existingTranslators[0].priority;
let newTranslatorsAreFromTopFrame = instanceID == 0;
if (priorityEqual && !newTranslatorsAreFromTopFrame) return;
}
tab.translators = translators;
tab.instanceID = instanceID;
tab.isPDFFrame = false;
_updateButtonStatus();
}
/**
* Called to display select items dialog
*/
this.onSelect = function(items, tab) {
var deferred = Zotero.Promise.defer();
var newTab = safari.application.openBrowserWindow().activeTab;
newTab.url = safari.extension.baseURI+"itemSelector/itemSelector.html#"+encodeURIComponent(JSON.stringify([tab.id, items]));
_selectCallbacksForTabIDs[tab.id] = deferred.resolve;
return deferred.promise;
}
/**
* Called when select items dialog is closed to pass data back to injected script
*/
this.onSelectDone = function(data) {
_selectCallbacksForTabIDs[data[0]](data[1]);
delete _selectCallbacksForTabIDs[data[0]];
}
/**
* Called when Zotero button is pressed
*/
this.onPerformCommand = function(event) {
var command = event.command;
var tab = safari.application.activeBrowserWindow.activeTab;
if (command === "zotero-button") {
if(tab.translators && tab.translators.length) {
Zotero.Connector_Browser.saveWithTranslator(tab, 0);
} else {
Zotero.Connector_Browser.saveAsWebpage(tab);
}
} else if (command === "zotero-preferences") {
Zotero.Connector_Browser.openTab(safari.extension.baseURI+"preferences/preferences.html");
}
}
/**
* Called to determine if current page can be scraped
*/
this.onValidateCommand = function(event) {
if(event.command === "zotero-button") {
Zotero.Connector_Browser.activeTab = safari.application.activeBrowserWindow.activeTab;
_zoteroButton = event.target;
_updateButtonStatus();
}
}
/**
* Called when Zotero goes online or offline
*/
this.onStateChange = function() {
if (Zotero.Connector.isOnline) {
Zotero.Prefs.set('firstSaveToServer', true);
}
else {
Zotero.debug("Standalone went offline, invalidating standalone translators");
for (let browserWindow of safari.application.browserWindows) {
for (let tab of browserWindow.tabs) {
if (tab.translators && tab.translators.length) {
tab.translators = tab.translators.filter(
(t) => t.runMode !== Zotero.Translator.RUN_MODE_ZOTERO_STANDALONE);
}
}
}
_updateButtonStatus();
}
};
/**
* Called if Zotero version is determined to be incompatible with Standalone
*/
this.onIncompatibleStandaloneVersion = function(zoteroVersion, standaloneVersion) {
if(_incompatibleVersionMessageShown) return;
alert('Zotero Connector for Safari '+zoteroVersion+' is incompatible with the running '+
'version of Zotero Standalone'+(standaloneVersion ? " ("+standaloneVersion+")" : "")+
'. Zotero Connector will continue to operate, but functionality that relies upon '+
'Zotero Standalone may be unavaliable.\n\n'+
'Please ensure that you have installed the latest version of these components. See '+
'https://www.zotero.org/support/standalone for more details.');
_incompatibleVersionMessageShown = true;
}
this.saveWithTranslator = function(tab, i) {
return Zotero.Messaging.sendMessage("translate", [tab.instanceID, tab.translators[i].translatorID], tab);
}
this.saveAsWebpage = function(tab) {
let title = tab.title.split('/');
title = title[title.length-1];
return Zotero.Messaging.sendMessage("saveAsWebpage", [tab.instanceID || 0, title], tab);
}
this.openTab = function(url) {
safari.application.activeBrowserWindow.openTab().url = url;
};
this.openPreferences = function(paneID) {
Zotero.Connector_Browser.openTab(safari.extension.baseURI + `preferences/preferences.html#${paneID}`);
};
this.openConfigEditor = function() {
Zotero.Connector_Browser.openTab(safari.extension.baseURI + "preferences/config.html");
};
/**
* Display an old-school firefox notification by injecting HTML directly into DOM.
* This has a side-effect of navigation (user-initiated or JS-redirect-based)
* removing the notification so we keep on re-injecting it into DOM.
*
* The timeout argument specifies how long the notification has to be displayed for
* without navigation, before it is considered "seen" and further navigation on the tab
* will not make it re-appear.
*
* @param {String} text
* @param {String[]} buttons - labels for buttons
* @param {Number} [seenTimeout=5000]
* @param {Tab} [tab=currentTab]
* @returns {Promise{Number}} button pressed idx or undefined if timed-out and navigated away from
*/
this.notify = async function(text, buttons, seenTimeout=5000, tab=null) {
await Zotero.Promise.delay(1000);
// Get current tab if not provided
if (!tab) {
tab = safari.application.activeBrowserWindow.activeTab;
}
let timedOut = false;
seenTimeout && setTimeout(() => timedOut = true, seenTimeout);
var response = await Zotero.Messaging.sendMessage('notify', [text, buttons, null, 'complete'], tab);
if (response != undefined || timedOut) return response;
// Tab url changed or tab got removed, hence the undefined response
// Wait a sec to not run a busy-waiting loop
await Zotero.Promise.delay(1000);
return this.notify(text, buttons, seenTimeout, tab);
}
function _isDisabledForURL(url, excludeTests=false) {
return !url || url.includes('file://') || (url.includes('-extension://') && (!excludeTests || !url.includes('/test/data/')));
}
/**
* Update status and tooltip of Zotero button
*
* Called on changing tabs or when Zotero goes offline
*/
function _updateButtonStatus() {
if (!_zoteroButton) return;
var tab = safari.application.activeBrowserWindow.activeTab;
if (_isDisabledForURL(tab.url, true)) {
_showZoteroStatus();
return;
}
_zoteroButton.disabled = false;
var translators = tab.translators;
var isPDF = tab.contentType == 'application/pdf';
if (isPDF || tab.isPDFFrame) {
Zotero.Connector_Browser._showPDFIcon();
} else if (translators) {
_showTranslatorIcon(translators[0]);
}
}
function _showZoteroStatus() {
_zoteroButton.disabled = true;
Zotero.Connector.checkIsOnline().then(function(isOnline) {
if (isOnline) {
_zoteroButton.image = safari.extension.baseURI+"images/toolbar/zotero-new-z-16px.png";
_zoteroButton.toolTip = "Zotero is Online";
} else {
_zoteroButton.image = safari.extension.baseURI+"images/toolbar/zotero-z-16px-offline.png";
_zoteroButton.toolTip = "Zotero is Offline";
}
});
}
function _showTranslatorIcon(translator) {
var itemType = translator.itemType;
_zoteroButton.image = (itemType === "multiple"
? safari.extension.baseURI + "images/toolbar/treesource-collection.png"
: Zotero.ItemTypes.getImageSrc(itemType).replace('images/', 'images/toolbar/'));
_zoteroButton.toolTip = _getTranslatorLabel(translator);
}
this._showPDFIcon = function() {
_zoteroButton.image = safari.extension.baseURI + "images/toolbar/pdf.png";
_zoteroButton.toolTip = "Save to Zotero (PDF)";
}
function _getTranslatorLabel(translator) {
var translatorName = translator.label ? ` (${translator.label})` : '';
return `Save to Zotero${translatorName}`;
}
}
// register handlers
safari.application.addEventListener("command", Zotero.Connector_Browser.onPerformCommand, false);
safari.application.addEventListener("validate", Zotero.Connector_Browser.onValidateCommand, false);
safari.application.addEventListener('activate', function(e) {
Zotero.Connector.reportActiveURL(e.target.url);
}, true);
safari.application.addEventListener('beforeNavigate', function(e) {
if (e.target == safari.application.activeBrowserWindow.activeTab) {
Zotero.Connector.reportActiveURL(e.target.url);
}
if (e.target.translators) {
e.target.translators = null;
e.target.isPDFFrame = false;
}
}, true);
Zotero.Messaging.addMessageListener("selectDone", Zotero.Connector_Browser.onSelectDone);
// initialize
Zotero.initGlobal();