Use the fetchData helper function in more cases
				
					
				
			- Extend the `fetchData` helper function to also support fetching of "blob" data. - Use the `fetchData` helper function more in the code-base, when fetching non-PDF data. Given that the Fetch API isn't supported for all protocols, this should improve compatibility for the PDF.js library.
This commit is contained in:
		
							parent
							
								
									d679078beb
								
							
						
					
					
						commit
						fd7a7e2859
					
				@ -399,6 +399,8 @@ async function fetchData(url, type = "text") {
 | 
				
			|||||||
    switch (type) {
 | 
					    switch (type) {
 | 
				
			||||||
      case "arraybuffer":
 | 
					      case "arraybuffer":
 | 
				
			||||||
        return response.arrayBuffer();
 | 
					        return response.arrayBuffer();
 | 
				
			||||||
 | 
					      case "blob":
 | 
				
			||||||
 | 
					        return response.blob();
 | 
				
			||||||
      case "json":
 | 
					      case "json":
 | 
				
			||||||
        return response.json();
 | 
					        return response.json();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -419,6 +421,7 @@ async function fetchData(url, type = "text") {
 | 
				
			|||||||
        let data;
 | 
					        let data;
 | 
				
			||||||
        switch (type) {
 | 
					        switch (type) {
 | 
				
			||||||
          case "arraybuffer":
 | 
					          case "arraybuffer":
 | 
				
			||||||
 | 
					          case "blob":
 | 
				
			||||||
          case "json":
 | 
					          case "json":
 | 
				
			||||||
            data = request.response;
 | 
					            data = request.response;
 | 
				
			||||||
            break;
 | 
					            break;
 | 
				
			||||||
 | 
				
			|||||||
@ -27,7 +27,12 @@ import {
 | 
				
			|||||||
  Util,
 | 
					  Util,
 | 
				
			||||||
  warn,
 | 
					  warn,
 | 
				
			||||||
} from "../../shared/util.js";
 | 
					} from "../../shared/util.js";
 | 
				
			||||||
import { getColorValues, getRGB, PixelsPerInch } from "../display_utils.js";
 | 
					import {
 | 
				
			||||||
 | 
					  fetchData,
 | 
				
			||||||
 | 
					  getColorValues,
 | 
				
			||||||
 | 
					  getRGB,
 | 
				
			||||||
 | 
					  PixelsPerInch,
 | 
				
			||||||
 | 
					} from "../display_utils.js";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function bindEvents(obj, element, names) {
 | 
					function bindEvents(obj, element, names) {
 | 
				
			||||||
  for (const name of names) {
 | 
					  for (const name of names) {
 | 
				
			||||||
@ -116,12 +121,7 @@ class ImageManager {
 | 
				
			|||||||
      let image;
 | 
					      let image;
 | 
				
			||||||
      if (typeof rawData === "string") {
 | 
					      if (typeof rawData === "string") {
 | 
				
			||||||
        data.url = rawData;
 | 
					        data.url = rawData;
 | 
				
			||||||
 | 
					        image = await fetchData(rawData, "blob");
 | 
				
			||||||
        const response = await fetch(rawData);
 | 
					 | 
				
			||||||
        if (!response.ok) {
 | 
					 | 
				
			||||||
          throw new Error(response.statusText);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        image = await response.blob();
 | 
					 | 
				
			||||||
      } else {
 | 
					      } else {
 | 
				
			||||||
        image = data.file = rawData;
 | 
					        image = data.file = rawData;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
				
			|||||||
@ -15,10 +15,10 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/** @typedef {import("./interfaces").IL10n} IL10n */
 | 
					/** @typedef {import("./interfaces").IL10n} IL10n */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import { fetchData, shadow } from "pdfjs-lib";
 | 
				
			||||||
import { FluentBundle, FluentResource } from "fluent-bundle";
 | 
					import { FluentBundle, FluentResource } from "fluent-bundle";
 | 
				
			||||||
import { DOMLocalization } from "fluent-dom";
 | 
					import { DOMLocalization } from "fluent-dom";
 | 
				
			||||||
import { L10n } from "./l10n.js";
 | 
					import { L10n } from "./l10n.js";
 | 
				
			||||||
import { shadow } from "pdfjs-lib";
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * @implements {IL10n}
 | 
					 * @implements {IL10n}
 | 
				
			||||||
@ -32,14 +32,14 @@ class ConstL10n extends L10n {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  static async *#generateBundles(lang) {
 | 
					  static async *#generateBundles(lang) {
 | 
				
			||||||
    let text;
 | 
					    const text =
 | 
				
			||||||
    if (typeof PDFJSDev === "undefined") {
 | 
					      typeof PDFJSDev === "undefined"
 | 
				
			||||||
      const url = new URL(`./locale/${lang}/viewer.ftl`, window.location.href);
 | 
					        ? await fetchData(
 | 
				
			||||||
      const data = await fetch(url);
 | 
					            new URL(`./locale/${lang}/viewer.ftl`, window.location.href),
 | 
				
			||||||
      text = await data.text();
 | 
					            /* type = */ "text"
 | 
				
			||||||
    } else {
 | 
					          )
 | 
				
			||||||
      text = PDFJSDev.eval("DEFAULT_FTL");
 | 
					        : PDFJSDev.eval("DEFAULT_FTL");
 | 
				
			||||||
    }
 | 
					
 | 
				
			||||||
    const resource = new FluentResource(text);
 | 
					    const resource = new FluentResource(text);
 | 
				
			||||||
    const bundle = new FluentBundle(lang);
 | 
					    const bundle = new FluentBundle(lang);
 | 
				
			||||||
    const errors = bundle.addResource(resource);
 | 
					    const errors = bundle.addResource(resource);
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user