diff options
Diffstat (limited to 'chromium/chrome/browser/resources/pdf/pdf_scripting_api.js')
-rw-r--r-- | chromium/chrome/browser/resources/pdf/pdf_scripting_api.js | 185 |
1 files changed, 101 insertions, 84 deletions
diff --git a/chromium/chrome/browser/resources/pdf/pdf_scripting_api.js b/chromium/chrome/browser/resources/pdf/pdf_scripting_api.js index 0ab77f41f3f..5957e13c178 100644 --- a/chromium/chrome/browser/resources/pdf/pdf_scripting_api.js +++ b/chromium/chrome/browser/resources/pdf/pdf_scripting_api.js @@ -6,7 +6,7 @@ * Turn a dictionary received from postMessage into a key event. * * @param {Object} dict A dictionary representing the key event. - * @return {Event} A key event. + * @return {!Event} A key event. */ function DeserializeKeyEvent(dict) { const e = document.createEvent('Event'); @@ -53,66 +53,83 @@ const LoadState = { * Create a new PDFScriptingAPI. This provides a scripting interface to * the PDF viewer so that it can be customized by things like print preview. * - * @param {Window} window the window of the page containing the pdf viewer. - * @param {Object} plugin the plugin element containing the pdf viewer. - * @constructor */ -function PDFScriptingAPI(window, plugin) { - this.loadState_ = LoadState.LOADING; - this.pendingScriptingMessages_ = []; - this.setPlugin(plugin); +class PDFScriptingAPI { + /** + * @param {Window} window the window of the page containing the pdf viewer. + * @param {Object} plugin the plugin element containing the pdf viewer. + */ + constructor(window, plugin) { + this.loadState_ = LoadState.LOADING; + this.pendingScriptingMessages_ = []; + this.setPlugin(plugin); - window.addEventListener('message', event => { - if (event.origin != 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai' && - event.origin != 'chrome://print') { - console.error( - 'Received message that was not from the extension: ' + event); - return; - } - switch (event.data.type) { - case 'viewport': - /** - * @type {{ - * pageX: number, - * pageY: number, - * pageWidth: number, - * viewportWidth: number, - * viewportHeight: number - * }} - */ - const viewportData = event.data; - if (this.viewportChangedCallback_) { - this.viewportChangedCallback_( - viewportData.pageX, viewportData.pageY, viewportData.pageWidth, - viewportData.viewportWidth, viewportData.viewportHeight); - } - break; - case 'documentLoaded': { - const data = /** @type {{load_state: LoadState}} */ (event.data); - this.loadState_ = data.load_state; - if (this.loadCallback_) { - this.loadCallback_(this.loadState_ == LoadState.SUCCESS); - } - break; + /** @private {Function} */ + this.viewportChangedCallback_; + + /** @private {Function} */ + this.loadCallback_; + + /** @private {Function} */ + this.selectedTextCallback_; + + /** @private {Function} */ + this.keyEventCallback_; + + /** @private {Object} */ + this.plugin_; + + window.addEventListener('message', event => { + if (event.origin != + 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai' && + event.origin != 'chrome://print') { + console.error( + 'Received message that was not from the extension: ' + event); + return; } - case 'getSelectedTextReply': { - const data = /** @type {{selectedText: string}} */ (event.data); - if (this.selectedTextCallback_) { - this.selectedTextCallback_(data.selectedText); - this.selectedTextCallback_ = null; + switch (event.data.type) { + case 'viewport': + /** + * @type {{ + * pageX: number, + * pageY: number, + * pageWidth: number, + * viewportWidth: number, + * viewportHeight: number + * }} + */ + const viewportData = event.data; + if (this.viewportChangedCallback_) { + this.viewportChangedCallback_( + viewportData.pageX, viewportData.pageY, viewportData.pageWidth, + viewportData.viewportWidth, viewportData.viewportHeight); + } + break; + case 'documentLoaded': { + const data = /** @type {{load_state: LoadState}} */ (event.data); + this.loadState_ = data.load_state; + if (this.loadCallback_) { + this.loadCallback_(this.loadState_ == LoadState.SUCCESS); + } + break; } - break; - } - case 'sendKeyEvent': - if (this.keyEventCallback_) { - this.keyEventCallback_(DeserializeKeyEvent(event.data.keyEvent)); + case 'getSelectedTextReply': { + const data = /** @type {{selectedText: string}} */ (event.data); + if (this.selectedTextCallback_) { + this.selectedTextCallback_(data.selectedText); + this.selectedTextCallback_ = null; + } + break; } - break; - } - }, false); -} + case 'sendKeyEvent': + if (this.keyEventCallback_) { + this.keyEventCallback_(DeserializeKeyEvent(event.data.keyEvent)); + } + break; + } + }, false); + } -PDFScriptingAPI.prototype = { /** * Send a message to the extension. If messages try to get sent before there * is a plugin element set, then we queue them up and send them later (this @@ -121,13 +138,13 @@ PDFScriptingAPI.prototype = { * @param {Object} message The message to send. * @private */ - sendMessage_: function(message) { + sendMessage_(message) { if (this.plugin_) { this.plugin_.postMessage(message, '*'); } else { this.pendingScriptingMessages_.push(message); } - }, + } /** * Sets the plugin element containing the PDF viewer. The element will usually @@ -135,7 +152,7 @@ PDFScriptingAPI.prototype = { * * @param {Object} plugin the plugin element containing the PDF viewer. */ - setPlugin: function(plugin) { + setPlugin(plugin) { this.plugin_ = plugin; if (this.plugin_) { @@ -147,16 +164,16 @@ PDFScriptingAPI.prototype = { this.sendMessage_(this.pendingScriptingMessages_.shift()); } } - }, + } /** * Sets the callback which will be run when the PDF viewport changes. * * @param {Function} callback the callback to be called. */ - setViewportChangedCallback: function(callback) { + setViewportChangedCallback(callback) { this.viewportChangedCallback_ = callback; - }, + } /** * Sets the callback which will be run when the PDF document has finished @@ -164,20 +181,20 @@ PDFScriptingAPI.prototype = { * * @param {Function} callback the callback to be called. */ - setLoadCallback: function(callback) { + setLoadCallback(callback) { this.loadCallback_ = callback; if (this.loadState_ != LoadState.LOADING && this.loadCallback_) { this.loadCallback_(this.loadState_ == LoadState.SUCCESS); } - }, + } /** * Sets a callback that gets run when a key event is fired in the PDF viewer. * @param {Function} callback the callback to be called with a key event. */ - setKeyEventCallback: function(callback) { + setKeyEventCallback(callback) { this.keyEventCallback_ = callback; - }, + } /** * Resets the PDF viewer into print preview mode. @@ -187,7 +204,7 @@ PDFScriptingAPI.prototype = { * @param {Array<number>} pageNumbers an array of the page numbers. * @param {boolean} modifiable whether or not the document is modifiable. */ - resetPrintPreviewMode: function(url, grayscale, pageNumbers, modifiable) { + resetPrintPreviewMode(url, grayscale, pageNumbers, modifiable) { this.loadState_ = LoadState.LOADING; this.sendMessage_({ type: 'resetPrintPreviewMode', @@ -196,14 +213,14 @@ PDFScriptingAPI.prototype = { pageNumbers: pageNumbers, modifiable: modifiable }); - }, + } /** * Hide the toolbars after a delay. */ - hideToolbars: function() { + hideToolbars() { this.sendMessage_({type: 'hideToolbars'}); - }, + } /** * Load a page into the document while in print preview mode. @@ -211,22 +228,22 @@ PDFScriptingAPI.prototype = { * @param {string} url the url of the pdf page to load. * @param {number} index the index of the page to load. */ - loadPreviewPage: function(url, index) { + loadPreviewPage(url, index) { this.sendMessage_({type: 'loadPreviewPage', url: url, index: index}); - }, + } /** @param {boolean} darkMode Whether the page is in dark mode. */ - darkModeChanged: function(darkMode) { + darkModeChanged(darkMode) { this.sendMessage_({type: 'darkModeChanged', darkMode: darkMode}); - }, + } /** * Select all the text in the document. May only be called after document * load. */ - selectAll: function() { + selectAll() { this.sendMessage_({type: 'selectAll'}); - }, + } /** * Get the selected text in the document. The callback will be called with the @@ -236,40 +253,40 @@ PDFScriptingAPI.prototype = { * @return {boolean} true if the function is successful, false if there is an * outstanding request for selected text that has not been answered. */ - getSelectedText: function(callback) { + getSelectedText(callback) { if (this.selectedTextCallback_) { return false; } this.selectedTextCallback_ = callback; this.sendMessage_({type: 'getSelectedText'}); return true; - }, + } /** * Print the document. May only be called after document load. */ - print: function() { + print() { this.sendMessage_({type: 'print'}); - }, + } /** * Send a key event to the extension. * * @param {Event} keyEvent the key event to send to the extension. */ - sendKeyEvent: function(keyEvent) { + sendKeyEvent(keyEvent) { this.sendMessage_( {type: 'sendKeyEvent', keyEvent: SerializeKeyEvent(keyEvent)}); - }, + } /** * @param {number} scrollX The amount to horizontally scroll in pixels. * @param {number} scrollY The amount to vertically scroll in pixels. */ - scrollPosition: function(scrollX, scrollY) { + scrollPosition(scrollX, scrollY) { this.sendMessage_({type: 'scrollPosition', x: scrollX, y: scrollY}); - }, -}; + } +} /** * Creates a PDF viewer with a scripting interface. This is basically 1) an |