Skip to content

Commit

Permalink
Add a context-menu option to attach current page to selected item
Browse files Browse the repository at this point in the history
Closes 102
  • Loading branch information
adomasven committed Aug 9, 2017
1 parent f7119cf commit 5543144
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 12 deletions.
55 changes: 50 additions & 5 deletions src/browserExt/background.js
Expand Up @@ -55,7 +55,7 @@ Zotero.Connector_Browser = new function() {
var isPDF = contentType == 'application/pdf';
_tabInfo[tab.id] = {translators, instanceID, isPDF};

_updateExtensionUI(tab);
this._updateExtensionUI(tab);
}

/**
Expand Down Expand Up @@ -139,7 +139,7 @@ Zotero.Connector_Browser = new function() {
}

this.onTabActivated = function(tab) {
_updateExtensionUI(tab);
this._updateExtensionUI(tab);
};

/**
Expand Down Expand Up @@ -314,7 +314,11 @@ Zotero.Connector_Browser = new function() {
/**
* Update status and tooltip of Zotero button
*/
function _updateExtensionUI(tab) {
this._updateExtensionUI = function(tab) {
if (!tab) {
return chrome.tabs.query( { lastFocusedWindow: true, active: true },
(tabs) => tabs.length && this._updateExtensionUI(tabs[0]));
}
if (Zotero.Prefs.get('firstUse') && Zotero.isFirefox) return _showFirstUseUI(tab);
chrome.contextMenus.removeAll();

Expand Down Expand Up @@ -342,6 +346,8 @@ Zotero.Connector_Browser = new function() {
_showWebpageContextMenuItem();
}

_showSSEContextMenuItems();

if (Zotero.isFirefox) {
_showPreferencesContextMenuItem();
}
Expand Down Expand Up @@ -506,12 +512,46 @@ Zotero.Connector_Browser = new function() {
});
}

function _showSSEContextMenuItems() {
let SSEAvailable = Zotero.Connector.SSE.available && Zotero.Connector.isOnline;
let singleItemSelected = Zotero.Connector.selected.items.length == 1;
let item = singleItemSelected && Zotero.Connector.selected.items[0];
let itemIsAttachment = ['attachment', 'note'].includes(item.type);
// Don't even bother with the context menu options if SSE is not available
if (SSEAvailable) {
chrome.contextMenus.create({
type: "separator",
id: "zotero-context-menu-sse-separator",
contexts: ['all']
});
chrome.contextMenus.create({
id: "zotero-context-menu-attach-snapshot",
title: "Attach Snapshot of Current Page",
// Disable if a non-viable item is selected
enabled: singleItemSelected && !itemIsAttachment,
onclick: function (info, tab) {
Zotero.Connector_Browser._saveAsAttachment(tab, false);
},
contexts: ['all'],
});
chrome.contextMenus.create({
id: "zotero-context-menu-attach-link",
title: "Attach Link of Current Page",
enabled: singleItemSelected && !itemIsAttachment,
onclick: function (info, tab) {
Zotero.Connector_Browser._saveAsAttachment(tab, true);
},
contexts: ['all'],
});
}
}

function _browserAction(tab) {
if (Zotero.Prefs.get('firstUse') && Zotero.isFirefox) {
Zotero.Messaging.sendMessage("firstUse", null, tab)
.then(function () {
Zotero.Prefs.set('firstUse', false);
_updateExtensionUI(tab);
Zotero.Connector_Browser._updateExtensionUI(tab);
});
}
else if(_tabInfo[tab.id] && _tabInfo[tab.id].translators && _tabInfo[tab.id].translators.length) {
Expand Down Expand Up @@ -552,6 +592,11 @@ Zotero.Connector_Browser = new function() {
}
);
}
},

this._saveAsAttachment = function(tab, link=false) {
return Zotero.Messaging.sendMessage("saveAsAttachment",
[tab.title, link, Zotero.Connector.selected.items[0]], tab);
}

function _getTranslatorLabel(translator) {
Expand All @@ -570,7 +615,7 @@ Zotero.Connector_Browser = new function() {
chrome.tabs.onRemoved.addListener(_clearInfoForTab);

chrome.tabs.onUpdated.addListener(function(tabID, changeInfo, tab) {
_updateExtensionUI(tab);
Zotero.Connector_Browser._updateExtensionUI(tab);
if(!changeInfo.url) return;
Zotero.debug("Connector_Browser: URL changed for tab " + tab.url);
_clearInfoForTab(tabID);
Expand Down
10 changes: 6 additions & 4 deletions src/common/connector.js
Expand Up @@ -33,14 +33,16 @@ Zotero.Connector = {
// Disable for all browsers, except IE, which may be used frequently with ZSA
isOnline: Zotero.isBookmarklet && !Zotero.isIE ? false : null,
_shouldReportActiveURL: true,
_selected: {collection: null, library: null, item: null},
selected: {collection: null, library: null, item: null},

init: function() {
this.addEventListener('init', {notify: function(data) {
this._selected = data.selected;
this.selected = data.selected;
Zotero.Connector_Browser._updateExtensionUI();
}.bind(this)});
this.addEventListener('select', {notify: function(data) {
Object.assign(this._selected, data);
Object.assign(this.selected, data);
Zotero.Connector_Browser._updateExtensionUI();
}.bind(this)});

Zotero.Connector.SSE.init();
Expand Down Expand Up @@ -91,7 +93,7 @@ Zotero.Connector = {
if (!Zotero.Connector.isOnline) {
throw new this.CommunicationError('Zotero is Offline');
} else if (Zotero.Connector.SSE.available) {
return this._selected;
return this.selected;
} else {
return this.callMethod('getSelectedCollection', {}).then(function(response) {
let selected = {library: {editable: response.libraryEditable}};
Expand Down
53 changes: 53 additions & 0 deletions src/common/inject/inject.jsx
Expand Up @@ -432,6 +432,58 @@ Zotero.Inject = new function() {
if (err) throw err;
}.bind(this));
};

this.saveAsAttachment = function (args) {
var [title, link, item] = args;
var progress;

var data = {
title,
url: document.location.toString(),
cookie: document.cookie,
html: document.documentElement.innerHTML,
link
};

if (document.contentType == 'application/pdf') {
data.pdf = true;
}
if (link) {
var image = 'attachment-link';
} else if (data.pdf) {
var image = "attachment-pdf";
} else {
var image = "webpage";
}

progress = new Zotero.ProgressWindow.ItemProgress(
Zotero.ItemTypes.getImageSrc(image), title || document.title
);
Zotero.ProgressWindow.changeHeadline(`Attaching a ${link ? 'link' : 'snapshot'} to `, `treeitem-${item.type}.png`, item.title);
return Zotero.Connector.callMethodWithCookies("attachSnapshot", data)
.then(function(result) {
if (progress) {
progress.setProgress(100);
Zotero.ProgressWindow.startCloseTimer(2500);
}
return result;
}.bind(this), function(e) {
var err;
// Client unavailable
if (e.status === 0) {
err = new Zotero.ProgressWindow.ErrorMessage("clientRequired");
} else if (e.value && e.value.libraryEditable === false) {
err = new Zotero.ProgressWindow.ErrorMessage("collectionNotEditable");
} else if (e.value && e.value.itemsSelected != 1) {
// This should really never occur since the option is disabled when multiple items are selected
err = new Zotero.ProgressWindow.ErrorMessage("cannotAttach");
} else {
err = new Zotero.ProgressWindow.ErrorMessage("unexpectedError");
}
Zotero.ProgressWindow.startCloseTimer(8000);
if (err) throw err;
}.bind(this));
};
};

// check whether this is a hidden browser window being used for scraping
Expand All @@ -452,6 +504,7 @@ if(!isHiddenIFrame && (isWeb || isTestPage)) {
});
// add a listener to save as webpage when translators unavailable
Zotero.Messaging.addMessageListener("saveAsWebpage", Zotero.Inject.saveAsWebpage);
Zotero.Messaging.addMessageListener("saveAsAttachment", Zotero.Inject.saveAsAttachment);
// add listener to rerun detection on page modifications
Zotero.Messaging.addMessageListener("pageModified", function() {
Zotero.Inject.init(true);
Expand Down
5 changes: 4 additions & 1 deletion src/common/inject/progressWindow.js
Expand Up @@ -36,7 +36,7 @@ Zotero.ProgressWindow = new function() {
"fontWeight":"bold", "marginBottom":"6px", "overflow":"hidden",
"whiteSpace":"nowrap", "textOverflow":"ellipsis"};
const cssHeadlineIcon = {"display":"none", "width":"16px", "height":"16px",
"backgroundPosition":"center", "backgroundRepeat":"no-repeat",
"backgroundPosition":"center", "backgroundRepeat":"no-repeat", "backgroundSize":"contain",
"verticalAlign":"-3px"};
const cssItem = {"fontSize":"11px", "margin":"4px 0 4px 0"};
const cssIcon = {"position":"absolute", "fontSize":"11px", "width":"16px", "height":"16px",
Expand Down Expand Up @@ -178,6 +178,9 @@ Zotero.ProgressWindow = new function() {
} else if (err === "collectionNotEditable") {
this._div.appendChild(doc.createTextNode("The currently selected collection is not editable. "+
"Please select a different collection in Zotero."));
} else if (err === "cannotAttach") {
this._div.appendChild(doc.createTextNode("Zotero could not attach current page to currently selected items. "+
"Please select a single item in Zotero."));
} else if (err === "clientRequired") {
this._div.appendChild(doc.createTextNode("This item could not be saved because Zotero "+
"is not open or is unreachable. Please open Zotero and try again."));
Expand Down
4 changes: 2 additions & 2 deletions src/common/test/tests/connectorTest.js
Expand Up @@ -84,7 +84,7 @@ describe('Connector', function() {
let s = yield background(function() {
Zotero.Connector.isOnline = true;
Zotero.Connector.SSE.available = true;
Zotero.Connector._selected = {collection: 'selected'};
Zotero.Connector.selected = {collection: 'selected'};
return Zotero.Connector.getSelectedCollection()
});
assert.equal(s, 'selected');
Expand All @@ -93,7 +93,7 @@ describe('Connector', function() {
let call = yield background(function() {
Zotero.Connector.isOnline = true;
Zotero.Connector.SSE.available = false;
Zotero.Connector._selected = {collection: 'selected'};
Zotero.Connector.selected = {collection: 'selected'};
sinon.stub(Zotero.Connector, 'callMethod');
return Zotero.Connector.getSelectedCollection().then(function() {
let call = Zotero.Connector.callMethod.lastCall;
Expand Down

0 comments on commit 5543144

Please sign in to comment.