From efcf2aed6e2a6c68cca97cb64ad8bdd4ccd59b1a Mon Sep 17 00:00:00 2001 From: Rob Wu Date: Mon, 1 Jul 2013 18:38:41 +0200 Subject: [PATCH] [Chrome extension] Added JSDocs to pdfHandler.js And a small refactor to reduce the LOC in the onHeadersReceived callback, to improve the readability of the code. --- extensions/chrome/pdfHandler.js | 118 +++++++++++++++++++++----------- 1 file changed, 77 insertions(+), 41 deletions(-) diff --git a/extensions/chrome/pdfHandler.js b/extensions/chrome/pdfHandler.js index 76811f0aa..c5a1642c9 100644 --- a/extensions/chrome/pdfHandler.js +++ b/extensions/chrome/pdfHandler.js @@ -19,10 +19,21 @@ limitations under the License. 'use strict'; +/** + * @param {Object} details First argument of the webRequest.onHeadersReceived + * event. The property "url" is read. + * @return {boolean} True if the PDF download was initiated by PDF.js + */ function isPdfDownloadable(details) { return details.url.indexOf('pdfjs.action=download') >= 0; } +/** + * Insert the content script in a tab which renders the PDF viewer. + * @param {number} tabId ID of the tab used by the Chrome APIs. + * @param {string} url URL of the PDF file. Used to detect whether the viewer + * should be activated in a specific (i)frame. + */ function insertPDFJSForTab(tabId, url) { chrome.tabs.executeScript(tabId, { file: 'insertviewer.js', @@ -35,6 +46,13 @@ function insertPDFJSForTab(tabId, url) { }); }); } + +/** + * Try to render the PDF viewer when (a frame within) a tab unloads. + * This indicates that a PDF file may be loading. + * @param {number} tabId ID of the tab used by the Chrome APIs. + * @param {string} url The URL of the pdf file. + */ function activatePDFJSForTab(tabId, url) { chrome.tabs.onUpdated.addListener(function listener(_tabId) { if (tabId === _tabId) { @@ -44,60 +62,78 @@ function activatePDFJSForTab(tabId, url) { }); } +/** + * Get the header from the list of headers for a given name. + * @param {Array} headers responseHeaders of webRequest.onHeadersReceived + * @return {undefined|{name: string, value: string}} The header, if found. + */ +function getHeaderFromHeaders(headers, headerName) { + for (var i=0; i 0; + } +} + +/** + * Takes a set of headers, and set "Content-Disposition: attachment". + * @param {Object} details First argument of the webRequest.onHeadersReceived + * event. The property "responseHeaders" is read and + * modified if needed. + * @return {Object|undefined} The return value for the onHeadersReceived event. + * Object with key "responseHeaders" if the headers + * have been modified, undefined otherwise. + */ +function getHeadersWithContentDispositionAttachment(details) { + var headers = details.responseHeaders; + var cdHeader = getHeaderFromHeaders(headers, 'content-disposition'); + if (!cdHeader) { + cdHeader = {name: 'Content-Disposition'}; + headers.push(cdHeader); + } + if (!/^attachment/i.test(cdHeader.value)) { + cdHeader.value = 'attachment' + cdHeader.value.replace(/^[^;]+/i, ''); + return { responseHeaders: headers }; + } +} + chrome.webRequest.onHeadersReceived.addListener( function(details) { - // Check if the response is a PDF file - var isPDF = false; - var headers = details.responseHeaders; - var header, i; - var cdHeader; - if (!headers) - return; - for (i=0; i 0; - break; - } - } - if (!isPDF) + if (!isPdfFile(details)) return; if (isPdfDownloadable(details)) { // Force download by ensuring that Content-Disposition: attachment is set - if (!cdHeader) { - for (; i