|
| 1 | +/* Copyright 2024 Mozilla Foundation |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +/** |
| 17 | + * This class manages the interaction of extracting the text content of the page |
| 18 | + * and passing it back to the external service. |
| 19 | + */ |
| 20 | +class PdfTextExtractor { |
| 21 | + /** @type {PDFViewer} */ |
| 22 | + #pdfViewer; |
| 23 | + |
| 24 | + #externalServices; |
| 25 | + |
| 26 | + /** |
| 27 | + * @type {?Promise<string>} |
| 28 | + */ |
| 29 | + #textPromise; |
| 30 | + |
| 31 | + #pendingRequests = new Set(); |
| 32 | + |
| 33 | + constructor(externalServices) { |
| 34 | + this.#externalServices = externalServices; |
| 35 | + |
| 36 | + window.addEventListener("requestTextContent", ({ detail }) => { |
| 37 | + this.extractTextContent(detail.requestId); |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * The PDF viewer is required to get the page text. |
| 43 | + * |
| 44 | + * @param {PDFViewer | null} |
| 45 | + */ |
| 46 | + setViewer(pdfViewer) { |
| 47 | + this.#pdfViewer = pdfViewer; |
| 48 | + if (this.#pdfViewer && this.#pendingRequests.size) { |
| 49 | + // Handle any pending requests that came in while things were loading. |
| 50 | + for (const pendingRequest of this.#pendingRequests) { |
| 51 | + this.extractTextContent(pendingRequest); |
| 52 | + } |
| 53 | + this.#pendingRequests.clear(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Builds up all of the text from a PDF. |
| 59 | + * |
| 60 | + * @param {number} requestId |
| 61 | + */ |
| 62 | + async extractTextContent(requestId) { |
| 63 | + if (!this.#pdfViewer) { |
| 64 | + this.#pendingRequests.add(requestId); |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + if (!this.#textPromise) { |
| 69 | + const textPromise = (this.#textPromise = this.#pdfViewer.getAllText()); |
| 70 | + |
| 71 | + // After the text resolves, cache the text for a little bit in case |
| 72 | + // multiple consumers call it. |
| 73 | + textPromise.then(() => { |
| 74 | + setTimeout(() => { |
| 75 | + if (this.#textPromise === textPromise) { |
| 76 | + this.#textPromise = null; |
| 77 | + } |
| 78 | + }, 5000); |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + this.#externalServices.reportText({ |
| 83 | + text: await this.#textPromise, |
| 84 | + requestId, |
| 85 | + }); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +export { PdfTextExtractor }; |
0 commit comments