Restore site-specific Quick Copy settings

Using the last URL passed by a connector

Closes https://github.com/zotero/zotero-connectors/issues/145
This commit is contained in:
Dan Stillman 2017-07-19 03:38:18 -04:00
parent 2928752d19
commit 6d265f448f
8 changed files with 100 additions and 121 deletions

View file

@ -66,9 +66,7 @@ Zotero_Preferences.Export = {
this._lastSelectedLocale = Zotero.Prefs.get("export.quickCopy.locale");
this.updateQuickCopyUI();
if (!Zotero.isStandalone) {
yield this.refreshQuickCopySiteList();
}
yield this.refreshQuickCopySiteList();
}),

View file

@ -64,7 +64,30 @@
oncommand="Zotero_Preferences.Export.buildQuickCopyFormatDropDown(document.getElementById('zotero-quickCopy-menu'), this.checked ? 'html' : '');"/>
</hbox>
<vbox id="zotero-prefpane-export-siteSettings"/>
<separator/>
<label value="&zotero.preferences.quickCopy.siteEditor.setings;" control="quickCopy-siteSettings"/>
<tree flex="1" id="quickCopy-siteSettings" hidecolumnpicker="true" rows="6" seltype="single"
ondblclick="Zotero_Preferences.Export.showQuickCopySiteEditor(this.currentIndex)"
onselect="Zotero_Preferences.Export.enableQuickCopySiteButtons()"
onkeypress="if (event.keyCode == event.DOM_VK_DELETE) { Zotero_Preferences.Export.deleteSelectedQuickCopySite(); }">
<treecols>
<treecol id="quickCopy-urlColumn" label="&zotero.preferences.quickCopy.siteEditor.domainPath;" flex="1"/>
<treecol id="quickCopy-formatColumn" label="&zotero.preferences.quickCopy.siteEditor.outputFormat;" flex="2"/>
<treecol id="quickCopy-localeColumn" label="&zotero.preferences.quickCopy.siteEditor.locale;"/>
<treecol id="quickCopy-copyAsHTML" label="HTML"/>
</treecols>
<treechildren id="quickCopy-siteSettings-rows"/>
</tree>
<separator class="thin"/>
<hbox>
<button disabled="true" id="quickCopy-edit" label="&zotero.general.edit;"
onclick="Zotero_Preferences.Export.showQuickCopySiteEditor(document.getElementById('quickCopy-siteSettings').currentIndex)"/>
<spacer flex="1"/>
<button disabled="true" id="quickCopy-delete" label="-" onclick="Zotero_Preferences.Export.deleteSelectedQuickCopySite()"/>
<button label="+"
onclick="Zotero_Preferences.Export.showQuickCopySiteEditor()"/>
</hbox>
<hbox align="center">
<label value="&zotero.preferences.quickCopy.dragLimit;"/>

View file

@ -1,62 +0,0 @@
<?xml version="1.0"?>
<!--
***** BEGIN LICENSE BLOCK *****
Copyright © 20062013 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 Affero 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Zotero. If not, see <http://www.gnu.org/licenses/>.
***** END LICENSE BLOCK *****
-->
<!DOCTYPE prefwindow [
<!ENTITY % preferencesDTD SYSTEM "chrome://zotero/locale/preferences.dtd"> %preferencesDTD;
<!ENTITY % zoteroDTD SYSTEM "chrome://zotero/locale/zotero.dtd"> %zoteroDTD;
]>
<overlay xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<prefpane id="zotero-prefpane-export">
<groupbox id="zotero-prefpane-export-groupbox">
<vbox id="zotero-prefpane-export-siteSettings">
<separator/>
<label value="&zotero.preferences.quickCopy.siteEditor.setings;" control="quickCopy-siteSettings"/>
<tree flex="1" id="quickCopy-siteSettings" hidecolumnpicker="true" rows="6" seltype="single"
ondblclick="Zotero_Preferences.Export.showQuickCopySiteEditor(this.currentIndex)"
onselect="Zotero_Preferences.Export.enableQuickCopySiteButtons()"
onkeypress="if (event.keyCode == event.DOM_VK_DELETE) { Zotero_Preferences.Export.deleteSelectedQuickCopySite(); }">
<treecols>
<treecol id="quickCopy-urlColumn" label="&zotero.preferences.quickCopy.siteEditor.domainPath;" flex="1"/>
<treecol id="quickCopy-formatColumn" label="&zotero.preferences.quickCopy.siteEditor.outputFormat;" flex="2"/>
<treecol id="quickCopy-localeColumn" label="&zotero.preferences.quickCopy.siteEditor.locale;"/>
<treecol id="quickCopy-copyAsHTML" label="HTML"/>
</treecols>
<treechildren id="quickCopy-siteSettings-rows"/>
</tree>
<separator class="thin"/>
<hbox>
<button disabled="true" id="quickCopy-edit" label="&zotero.general.edit;"
onclick="Zotero_Preferences.Export.showQuickCopySiteEditor(document.getElementById('quickCopy-siteSettings').currentIndex)"/>
<spacer flex="1"/>
<button disabled="true" id="quickCopy-delete" label="-" onclick="Zotero_Preferences.Export.deleteSelectedQuickCopySite()"/>
<button label="+"
onclick="Zotero_Preferences.Export.showQuickCopySiteEditor()"/>
</hbox>
</vbox>
</groupbox>
</prefpane>
</overlay>

View file

@ -45,30 +45,9 @@
<script src="preferences.js"/>
<script src="preferences_export.js"/>
<script>
<![CDATA[
var Zotero_QuickCopySiteEditor = new function () {
this.onAccept = onAccept;
function onAccept() {
var io = window.arguments[0];
io.domain = document.getElementById('zotero-quickCopy-domain').value;
io.format = document.getElementById('zotero-quickCopy-menu').value;
io.locale = '';
if (!document.getElementById('zotero-quickCopy-locale-menu').disabled) {
io.locale = document.getElementById('zotero-quickCopy-locale-menu').value;
}
io.ok = true;
}
}
]]>
</script>
<vbox id="zotero-preferences-quickCopySiteEditor">
<label value="&zotero.preferences.quickCopy.siteEditor.domainPath; &zotero.preferences.quickCopy.siteEditor.domainPath.example;" control="zotero-quickCopy-domain"/>
<textbox id="zotero-quickCopy-domain"/>
<textbox id="zotero-quickCopy-domain" oninput="Zotero_QuickCopySiteEditor.onDomainInput(event)"/>
<separator class="thin"/>
@ -95,6 +74,31 @@
<script>
<![CDATA[
var Zotero_QuickCopySiteEditor = new function () {
var menu = document.getElementById('zotero-quickCopy-menu');
this.onAccept = function () {
var io = window.arguments[0];
io.domain = document.getElementById('zotero-quickCopy-domain').value;
io.format = menu.value;
io.locale = '';
if (!document.getElementById('zotero-quickCopy-locale-menu').disabled) {
io.locale = document.getElementById('zotero-quickCopy-locale-menu').value;
}
io.ok = true;
}
this.onDomainInput = function (event) {
if (event.target.value == 'wikipedia.org') {
Zotero.debug("SETTING TO WIKI");
menu.value = 'export=3f50aaac-7acc-4350-acd0-59cb77faf620';
Zotero_Preferences.Export.updateQuickCopyUI();
}
};
}
var io = window.arguments[0];
var contentType = io.asHTML ? 'html' : '';
document.getElementById('zotero-quickCopy-domain').value = io.domain ? io.domain : '';

View file

@ -2720,10 +2720,8 @@ Zotero.ItemTreeView.prototype.onDragStart = function (event) {
}
}
// Get Quick Copy format for current URL
var url = this._ownerDocument.defaultView.content && this._ownerDocument.defaultView.content.location ?
this._ownerDocument.defaultView.content.location.href : null;
var format = Zotero.QuickCopy.getFormatFromURL(url);
// Get Quick Copy format for current URL (set via /ping from connector)
var format = Zotero.QuickCopy.getFormatFromURL(Zotero.QuickCopy.lastActiveURL);
Zotero.debug("Dragging with format " + format);

View file

@ -26,6 +26,8 @@
"use strict";
Zotero.QuickCopy = new function() {
this.lastActiveURL = null;
var _initTimeoutID
var _initPromise;
var _initialized = false;
@ -36,15 +38,13 @@ Zotero.QuickCopy = new function() {
this.init = Zotero.Promise.coroutine(function* () {
Zotero.debug("Initializing Quick Copy");
yield this.loadSiteSettings();
if (!_initialized) {
// Make sure export translator code is loaded whenever the output format changes
Zotero.Prefs.registerObserver("export.quickCopy.setting", _loadOutputFormat);
_initialized = true;
}
// Load code for selected export translator ahead of time
// Load code for selected export translators ahead of time
// (in the background, because it requires translator initialization)
Zotero.Schema.schemaUpdatePromise
.then(function () {
@ -54,8 +54,14 @@ Zotero.QuickCopy = new function() {
// if an export format is selected
if (Zotero.test) return;
_initPromise = _loadOutputFormat();
});
_initPromise = Zotero.Promise.each(
[
() => _loadOutputFormat(),
() => this.loadSiteSettings()
],
f => f()
);
}.bind(this));
});
@ -80,9 +86,15 @@ Zotero.QuickCopy = new function() {
format: row.format
};
});
yield Zotero.Promise.map(rows, row => _preloadFormat(row.format));
});
this.hasSiteSettings = function () {
return _siteSettings.length > 0;
};
/*
* Return Quick Copy setting object from string, stringified object, or object
*
@ -174,26 +186,11 @@ Zotero.QuickCopy = new function() {
}
var matches = [];
// Match last one or two sections of domain, not counting trailing period
var urlDomain = urlHostPort.match(/(?:[^.]+\.)?[^.]+(?=\.?$)/);
// Hopefully can't happen, but until we're sure
if (!urlDomain) {
Zotero.logError("Quick Copy host '" + urlHostPort + "' not matched");
return quickCopyPref;
}
for (let i=0; i<_siteSettings.length; i++) {
let row = _siteSettings[i];
// Only concern ourselves with entries containing the current domain
// or paths that apply to all domains
if (!row.domainPath.indexOf(urlDomain[0]) != -1 && !row.domainPath.startsWith('/')) {
continue;
}
let domain = row.domainPath.split('/',1)[0];
let path = row.domainPath.substr(domain.length) || '/';
let re = new RegExp('(^|[./])' + Zotero.Utilities.quotemeta(domain) + '$', 'i');
if (re.test(urlHostPort) && urlPath.indexOf(path) === 0) {
if (urlHostPort.endsWith(domain) && urlPath.startsWith(path)) {
matches.push({
format: JSON.stringify(this.unserializeSetting(row.format)),
domainLength: domain.length,
@ -449,18 +446,25 @@ Zotero.QuickCopy = new function() {
/**
* If an export translator is the selected output format, load its code (which must be done
* asynchronously) ahead of time, since drag-and-drop requires synchronous operation
*
* @return {Promise}
*/
var _loadOutputFormat = Zotero.Promise.coroutine(function* () {
var format = Zotero.Prefs.get("export.quickCopy.setting");
return _preloadFormat(format);
});
var _preloadFormat = async function (format) {
format = Zotero.QuickCopy.unserializeSetting(format);
if (format.mode == 'export') {
Zotero.debug("Preloading code for Quick Copy export format");
yield Zotero.Translators.init();
Zotero.debug(`Preloading ${format.id} for Quick Copy`);
await Zotero.Translators.init();
let translator = Zotero.Translators.get(format.id);
translator.cacheCode = true;
yield translator.getCode();
await translator.getCode();
}
});
};
var _loadFormattedNames = Zotero.Promise.coroutine(function* () {

View file

@ -821,12 +821,27 @@ Zotero.Server.Connector.Ping.prototype = {
* Sends 200 and HTML status on GET requests
* @param data {Object} request information defined in connector.js
*/
init: function(data) {
if (data.method == 'GET') {
init: function (req) {
if (req.method == 'GET') {
return [200, "text/html", '<!DOCTYPE html><html><head>' +
'<title>Zotero Connector Server is Available</title></head>' +
'<body>Zotero Connector Server is Available</body></html>'];
} else {
// Store the active URL so it can be used for site-specific Quick Copy
if (req.data.activeURL) {
//Zotero.debug("Setting active URL to " + req.data.activeURL);
Zotero.QuickCopy.lastActiveURL = req.data.activeURL;
}
if (Zotero.QuickCopy.hasSiteSettings()) {
let response = {
prefs: {
reportActiveURL: true
}
};
return [200, 'application/json', JSON.stringify(response)];
}
return [200, 'text/plain', ''];
}
}

View file

@ -2146,8 +2146,7 @@ var ZoteroPane = new function()
return;
}
var url = (window.content && window.content.location ? window.content.location.href : null);
var format = Zotero.QuickCopy.getFormatFromURL(url);
var format = Zotero.QuickCopy.getFormatFromURL(Zotero.QuickCopy.lastActiveURL);
format = Zotero.QuickCopy.unserializeSetting(format);
// determine locale preference