pdf.js/web/firefoxcom.js

322 lines
9.1 KiB
JavaScript
Raw Normal View History

2012-09-01 07:48:21 +09:00
/* Copyright 2012 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import '../extensions/firefox/tools/l10n';
import { createObjectURL, PDFDataRangeTransport, shadow, URL } from 'pdfjs-lib';
import { BasePreferences } from './preferences';
2017-04-15 00:32:36 +09:00
import { PDFViewerApplication } from './app';
if (typeof PDFJSDev === 'undefined' ||
!PDFJSDev.test('FIREFOX || MOZCENTRAL')) {
throw new Error('Module "pdfjs-web/firefoxcom" shall not be used outside ' +
'FIREFOX and MOZCENTRAL builds.');
}
let FirefoxCom = (function FirefoxComClosure() {
2012-08-02 07:31:25 +09:00
return {
/**
* Creates an event that the extension is listening for and will
* synchronously respond to.
* NOTE: It is reccomended to use request() instead since one day we may not
* be able to synchronously reply.
* @param {String} action The action to trigger.
* @param {String} data Optional data to send.
* @return {*} The response.
*/
requestSync(action, data) {
let request = document.createTextNode('');
2012-08-02 07:31:25 +09:00
document.documentElement.appendChild(request);
let sender = document.createEvent('CustomEvent');
sender.initCustomEvent('pdf.js.message', true, false,
{ action, data, sync: true, });
2012-08-02 07:31:25 +09:00
request.dispatchEvent(sender);
let response = sender.detail.response;
2012-08-02 07:31:25 +09:00
document.documentElement.removeChild(request);
return response;
},
2012-08-02 07:31:25 +09:00
/**
* Creates an event that the extension is listening for and will
* asynchronously respond by calling the callback.
* @param {String} action The action to trigger.
* @param {String} data Optional data to send.
* @param {Function} callback Optional response callback that will be called
* with one data argument.
*/
request(action, data, callback) {
let request = document.createTextNode('');
2012-08-02 07:31:25 +09:00
if (callback) {
document.addEventListener('pdf.js.response', function listener(event) {
let node = event.target;
let response = event.detail.response;
2012-08-02 07:31:25 +09:00
document.documentElement.removeChild(node);
document.removeEventListener('pdf.js.response', listener);
2012-08-02 07:31:25 +09:00
return callback(response);
});
2012-08-02 07:31:25 +09:00
}
document.documentElement.appendChild(request);
let sender = document.createEvent('CustomEvent');
sender.initCustomEvent('pdf.js.message', true, false, {
action,
data,
sync: false,
Fix inconsistent spacing and trailing commas in objects in `web/` files, so we can enable the `comma-dangle` and `object-curly-spacing` ESLint rules later on http://eslint.org/docs/rules/comma-dangle http://eslint.org/docs/rules/object-curly-spacing Given that we currently have quite inconsistent object formatting, fixing this in in *one* big patch probably wouldn't be feasible (since I cannot imagine anyone wanting to review that); hence I've opted to try and do this piecewise instead. *Please note:* This patch was created automatically, using the ESLint `--fix` command line option. In a couple of places this caused lines to become too long, and I've fixed those manually; please refer to the interdiff below for the only hand-edits in this patch. ```diff diff --git a/web/pdf_thumbnail_view.js b/web/pdf_thumbnail_view.js index 002dbf29..1de4e530 100644 --- a/web/pdf_thumbnail_view.js +++ b/web/pdf_thumbnail_view.js @@ -420,8 +420,8 @@ var PDFThumbnailView = (function PDFThumbnailViewClosure() { setPageLabel: function PDFThumbnailView_setPageLabel(label) { this.pageLabel = (typeof label === 'string' ? label : null); - this.l10n.get('thumb_page_title', { page: this.pageId, }, 'Page {{page}}'). - then((msg) => { + this.l10n.get('thumb_page_title', { page: this.pageId, }, + 'Page {{page}}').then((msg) => { this.anchor.title = msg; }); diff --git a/web/secondary_toolbar.js b/web/secondary_toolbar.js index 160e0410..6495fc5e 100644 --- a/web/secondary_toolbar.js +++ b/web/secondary_toolbar.js @@ -65,7 +65,8 @@ class SecondaryToolbar { { element: options.printButton, eventName: 'print', close: true, }, { element: options.downloadButton, eventName: 'download', close: true, }, { element: options.viewBookmarkButton, eventName: null, close: true, }, - { element: options.firstPageButton, eventName: 'firstpage', close: true, }, + { element: options.firstPageButton, eventName: 'firstpage', + close: true, }, { element: options.lastPageButton, eventName: 'lastpage', close: true, }, { element: options.pageRotateCwButton, eventName: 'rotatecw', close: false, }, @@ -76,7 +77,7 @@ class SecondaryToolbar { { element: options.cursorHandToolButton, eventName: 'switchcursortool', eventDetails: { tool: CursorTool.HAND, }, close: true, }, { element: options.documentPropertiesButton, - eventName: 'documentproperties', close: true, } + eventName: 'documentproperties', close: true, }, ]; this.items = { firstPage: options.firstPageButton, ```
2017-06-01 19:46:12 +09:00
responseExpected: !!callback,
});
2012-08-02 07:31:25 +09:00
return request.dispatchEvent(sender);
Fix inconsistent spacing and trailing commas in objects in `web/` files, so we can enable the `comma-dangle` and `object-curly-spacing` ESLint rules later on http://eslint.org/docs/rules/comma-dangle http://eslint.org/docs/rules/object-curly-spacing Given that we currently have quite inconsistent object formatting, fixing this in in *one* big patch probably wouldn't be feasible (since I cannot imagine anyone wanting to review that); hence I've opted to try and do this piecewise instead. *Please note:* This patch was created automatically, using the ESLint `--fix` command line option. In a couple of places this caused lines to become too long, and I've fixed those manually; please refer to the interdiff below for the only hand-edits in this patch. ```diff diff --git a/web/pdf_thumbnail_view.js b/web/pdf_thumbnail_view.js index 002dbf29..1de4e530 100644 --- a/web/pdf_thumbnail_view.js +++ b/web/pdf_thumbnail_view.js @@ -420,8 +420,8 @@ var PDFThumbnailView = (function PDFThumbnailViewClosure() { setPageLabel: function PDFThumbnailView_setPageLabel(label) { this.pageLabel = (typeof label === 'string' ? label : null); - this.l10n.get('thumb_page_title', { page: this.pageId, }, 'Page {{page}}'). - then((msg) => { + this.l10n.get('thumb_page_title', { page: this.pageId, }, + 'Page {{page}}').then((msg) => { this.anchor.title = msg; }); diff --git a/web/secondary_toolbar.js b/web/secondary_toolbar.js index 160e0410..6495fc5e 100644 --- a/web/secondary_toolbar.js +++ b/web/secondary_toolbar.js @@ -65,7 +65,8 @@ class SecondaryToolbar { { element: options.printButton, eventName: 'print', close: true, }, { element: options.downloadButton, eventName: 'download', close: true, }, { element: options.viewBookmarkButton, eventName: null, close: true, }, - { element: options.firstPageButton, eventName: 'firstpage', close: true, }, + { element: options.firstPageButton, eventName: 'firstpage', + close: true, }, { element: options.lastPageButton, eventName: 'lastpage', close: true, }, { element: options.pageRotateCwButton, eventName: 'rotatecw', close: false, }, @@ -76,7 +77,7 @@ class SecondaryToolbar { { element: options.cursorHandToolButton, eventName: 'switchcursortool', eventDetails: { tool: CursorTool.HAND, }, close: true, }, { element: options.documentPropertiesButton, - eventName: 'documentproperties', close: true, } + eventName: 'documentproperties', close: true, }, ]; this.items = { firstPage: options.firstPageButton, ```
2017-06-01 19:46:12 +09:00
},
2012-08-02 07:31:25 +09:00
};
})();
2013-07-13 03:14:13 +09:00
class DownloadManager {
constructor(options) {
this.disableCreateObjectURL = false;
}
downloadUrl(url, filename) {
FirefoxCom.request('download', {
originalUrl: url,
filename,
});
}
2015-02-03 00:12:52 +09:00
downloadData(data, filename, contentType) {
let blobUrl = createObjectURL(data, contentType);
FirefoxCom.request('download', {
blobUrl,
originalUrl: blobUrl,
filename,
isAttachment: true,
});
}
2013-07-13 03:14:13 +09:00
download(blob, url, filename) {
let blobUrl = URL.createObjectURL(blob);
let onResponse = (err) => {
if (err && this.onerror) {
this.onerror(err);
}
URL.revokeObjectURL(blobUrl);
};
FirefoxCom.request('download', {
blobUrl,
originalUrl: url,
filename,
}, onResponse);
}
}
2013-11-19 07:51:06 +09:00
class FirefoxPreferences extends BasePreferences {
async _writeToStorage(prefObj) {
return new Promise(function(resolve) {
FirefoxCom.request('setPreferences', prefObj, resolve);
});
}
2013-11-19 07:51:06 +09:00
async _readFromStorage(prefObj) {
return new Promise(function(resolve) {
FirefoxCom.request('getPreferences', prefObj, function(prefStr) {
let readPrefs = JSON.parse(prefStr);
resolve(readPrefs);
});
});
}
}
class MozL10n {
constructor(mozL10n) {
this.mozL10n = mozL10n;
}
async getLanguage() {
return this.mozL10n.getLanguage();
}
async getDirection() {
return this.mozL10n.getDirection();
}
async get(property, args, fallback) {
return this.mozL10n.get(property, args, fallback);
}
async translate(element) {
this.mozL10n.translate(element);
}
}
(function listenFindEvents() {
const events = [
'find',
'findagain',
'findhighlightallchange',
'findcasesensitivitychange',
'findentirewordchange',
];
let handleEvent = function(evt) {
if (!PDFViewerApplication.initialized) {
return;
}
PDFViewerApplication.eventBus.dispatch('find', {
source: window,
type: evt.type.substring('find'.length),
query: evt.detail.query,
phraseSearch: true,
caseSensitive: !!evt.detail.caseSensitive,
entireWord: !!evt.detail.entireWord,
highlightAll: !!evt.detail.highlightAll,
Fix inconsistent spacing and trailing commas in objects in `web/` files, so we can enable the `comma-dangle` and `object-curly-spacing` ESLint rules later on http://eslint.org/docs/rules/comma-dangle http://eslint.org/docs/rules/object-curly-spacing Given that we currently have quite inconsistent object formatting, fixing this in in *one* big patch probably wouldn't be feasible (since I cannot imagine anyone wanting to review that); hence I've opted to try and do this piecewise instead. *Please note:* This patch was created automatically, using the ESLint `--fix` command line option. In a couple of places this caused lines to become too long, and I've fixed those manually; please refer to the interdiff below for the only hand-edits in this patch. ```diff diff --git a/web/pdf_thumbnail_view.js b/web/pdf_thumbnail_view.js index 002dbf29..1de4e530 100644 --- a/web/pdf_thumbnail_view.js +++ b/web/pdf_thumbnail_view.js @@ -420,8 +420,8 @@ var PDFThumbnailView = (function PDFThumbnailViewClosure() { setPageLabel: function PDFThumbnailView_setPageLabel(label) { this.pageLabel = (typeof label === 'string' ? label : null); - this.l10n.get('thumb_page_title', { page: this.pageId, }, 'Page {{page}}'). - then((msg) => { + this.l10n.get('thumb_page_title', { page: this.pageId, }, + 'Page {{page}}').then((msg) => { this.anchor.title = msg; }); diff --git a/web/secondary_toolbar.js b/web/secondary_toolbar.js index 160e0410..6495fc5e 100644 --- a/web/secondary_toolbar.js +++ b/web/secondary_toolbar.js @@ -65,7 +65,8 @@ class SecondaryToolbar { { element: options.printButton, eventName: 'print', close: true, }, { element: options.downloadButton, eventName: 'download', close: true, }, { element: options.viewBookmarkButton, eventName: null, close: true, }, - { element: options.firstPageButton, eventName: 'firstpage', close: true, }, + { element: options.firstPageButton, eventName: 'firstpage', + close: true, }, { element: options.lastPageButton, eventName: 'lastpage', close: true, }, { element: options.pageRotateCwButton, eventName: 'rotatecw', close: false, }, @@ -76,7 +77,7 @@ class SecondaryToolbar { { element: options.cursorHandToolButton, eventName: 'switchcursortool', eventDetails: { tool: CursorTool.HAND, }, close: true, }, { element: options.documentPropertiesButton, - eventName: 'documentproperties', close: true, } + eventName: 'documentproperties', close: true, }, ]; this.items = { firstPage: options.firstPageButton, ```
2017-06-01 19:46:12 +09:00
findPrevious: !!evt.detail.findPrevious,
});
};
for (let event of events) {
window.addEventListener(event, handleEvent);
}
})();
function FirefoxComDataRangeTransport(length, initialData) {
PDFDataRangeTransport.call(this, length, initialData);
}
FirefoxComDataRangeTransport.prototype =
Object.create(PDFDataRangeTransport.prototype);
FirefoxComDataRangeTransport.prototype.requestDataRange =
function FirefoxComDataRangeTransport_requestDataRange(begin, end) {
FirefoxCom.request('requestDataRange', { begin, end, });
};
FirefoxComDataRangeTransport.prototype.abort =
function FirefoxComDataRangeTransport_abort() {
// Sync call to ensure abort is really started.
FirefoxCom.requestSync('abortLoading', null);
};
PDFViewerApplication.externalServices = {
updateFindControlState(data) {
FirefoxCom.request('updateFindControlState', data);
},
updateFindMatchesCount(data) {
// FirefoxCom.request('updateFindMatchesCount', data);
},
initPassiveLoading(callbacks) {
let pdfDataRangeTransport;
window.addEventListener('message', function windowMessage(e) {
if (e.source !== null) {
// The message MUST originate from Chrome code.
console.warn('Rejected untrusted message from ' + e.origin);
return;
}
let args = e.data;
if (typeof args !== 'object' || !('pdfjsLoadAction' in args)) {
return;
}
switch (args.pdfjsLoadAction) {
case 'supportsRangedLoading':
pdfDataRangeTransport =
new FirefoxComDataRangeTransport(args.length, args.data);
callbacks.onOpenWithTransport(args.pdfUrl, args.length,
pdfDataRangeTransport);
break;
case 'range':
pdfDataRangeTransport.onDataRange(args.begin, args.chunk);
break;
case 'rangeProgress':
pdfDataRangeTransport.onDataProgress(args.loaded);
break;
case 'progressiveRead':
pdfDataRangeTransport.onDataProgressiveRead(args.chunk);
break;
case 'progress':
callbacks.onProgress(args.loaded, args.total);
break;
case 'complete':
if (!args.data) {
callbacks.onError(args.errorCode);
break;
}
callbacks.onOpenWithData(args.data);
break;
}
});
FirefoxCom.requestSync('initPassiveLoading', null);
},
fallback(data, callback) {
FirefoxCom.request('fallback', data, callback);
},
reportTelemetry(data) {
FirefoxCom.request('reportTelemetry', JSON.stringify(data));
},
createDownloadManager(options) {
return new DownloadManager(options);
},
createPreferences() {
return new FirefoxPreferences();
},
createL10n(options) {
let mozL10n = document.mozL10n;
// TODO refactor mozL10n.setExternalLocalizerServices
return new MozL10n(mozL10n);
},
get supportsIntegratedFind() {
let support = FirefoxCom.requestSync('supportsIntegratedFind');
return shadow(this, 'supportsIntegratedFind', support);
},
get supportsDocumentFonts() {
let support = FirefoxCom.requestSync('supportsDocumentFonts');
return shadow(this, 'supportsDocumentFonts', support);
},
get supportsDocumentColors() {
let support = FirefoxCom.requestSync('supportsDocumentColors');
return shadow(this, 'supportsDocumentColors', support);
},
get supportedMouseWheelZoomModifierKeys() {
let support = FirefoxCom.requestSync('supportedMouseWheelZoomModifierKeys');
return shadow(this, 'supportedMouseWheelZoomModifierKeys', support);
},
};
// l10n.js for Firefox extension expects services to be set.
document.mozL10n.setExternalLocalizerServices({
getLocale() {
return FirefoxCom.requestSync('getLocale', null);
},
getStrings(key) {
return FirefoxCom.requestSync('getStrings', key);
Fix inconsistent spacing and trailing commas in objects in `web/` files, so we can enable the `comma-dangle` and `object-curly-spacing` ESLint rules later on http://eslint.org/docs/rules/comma-dangle http://eslint.org/docs/rules/object-curly-spacing Given that we currently have quite inconsistent object formatting, fixing this in in *one* big patch probably wouldn't be feasible (since I cannot imagine anyone wanting to review that); hence I've opted to try and do this piecewise instead. *Please note:* This patch was created automatically, using the ESLint `--fix` command line option. In a couple of places this caused lines to become too long, and I've fixed those manually; please refer to the interdiff below for the only hand-edits in this patch. ```diff diff --git a/web/pdf_thumbnail_view.js b/web/pdf_thumbnail_view.js index 002dbf29..1de4e530 100644 --- a/web/pdf_thumbnail_view.js +++ b/web/pdf_thumbnail_view.js @@ -420,8 +420,8 @@ var PDFThumbnailView = (function PDFThumbnailViewClosure() { setPageLabel: function PDFThumbnailView_setPageLabel(label) { this.pageLabel = (typeof label === 'string' ? label : null); - this.l10n.get('thumb_page_title', { page: this.pageId, }, 'Page {{page}}'). - then((msg) => { + this.l10n.get('thumb_page_title', { page: this.pageId, }, + 'Page {{page}}').then((msg) => { this.anchor.title = msg; }); diff --git a/web/secondary_toolbar.js b/web/secondary_toolbar.js index 160e0410..6495fc5e 100644 --- a/web/secondary_toolbar.js +++ b/web/secondary_toolbar.js @@ -65,7 +65,8 @@ class SecondaryToolbar { { element: options.printButton, eventName: 'print', close: true, }, { element: options.downloadButton, eventName: 'download', close: true, }, { element: options.viewBookmarkButton, eventName: null, close: true, }, - { element: options.firstPageButton, eventName: 'firstpage', close: true, }, + { element: options.firstPageButton, eventName: 'firstpage', + close: true, }, { element: options.lastPageButton, eventName: 'lastpage', close: true, }, { element: options.pageRotateCwButton, eventName: 'rotatecw', close: false, }, @@ -76,7 +77,7 @@ class SecondaryToolbar { { element: options.cursorHandToolButton, eventName: 'switchcursortool', eventDetails: { tool: CursorTool.HAND, }, close: true, }, { element: options.documentPropertiesButton, - eventName: 'documentproperties', close: true, } + eventName: 'documentproperties', close: true, }, ]; this.items = { firstPage: options.firstPageButton, ```
2017-06-01 19:46:12 +09:00
},
});
export {
DownloadManager,
FirefoxCom,
};