pdfjs-viewer-element 4.0.0 → 4.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1 +1 @@
|
|
|
1
|
-
var e=new URL("./pdf.worker.min.mjs",""+import.meta.url).href,t=new URL("./pdf.mjs",""+import.meta.url).href,i=new URL("./viewer.mjs",""+import.meta.url).href,r=new URL("./viewer.css",""+import.meta.url).href,s=new URL("./paper-and-ink.css",""+import.meta.url).href,a={src:"",iframeTitle:"PDF viewer window",page:"",search:"",phrase:"",zoom:"",pagemode:"none",locale:"",viewerCssTheme:"AUTOMATIC",workerSrc:e,debuggerSrc:"./debugger.mjs",cMapUrl:"../web/cmaps/",iccUrl:"../web/iccs/",imageResourcesPath:"./images/",sandboxBundleSrc:"../build/pdf.sandbox.mjs",standardFontDataUrl:"../web/standard_fonts/",wasmUrl:"../web/wasm/",localeSrcTemplate:"https://cdn.jsdelivr.net/gh/mozilla-l10n/firefox-l10n@main/{locale}/toolkit/toolkit/pdfviewer/viewer.ftl"},n={AUTOMATIC:0,LIGHT:1,DARK:2},o=class extends HTMLElement{constructor(){super(),this.initPromise=Promise.resolve({}),this.viewerStyles=new Set,this.optionsToSet={},this.applyIframeHash=async()=>{const e=this.iframe?.contentWindow;if(!e)return;const t=this.getIframeLocationHash();e.location.hash!==t&&await new Promise(i=>{let r=!1;const s=()=>{r||(r=!0,e.removeEventListener("hashchange",s),i())};e.addEventListener("hashchange",s),e.location.hash=t,queueMicrotask(()=>{e.location.hash===t&&s()})})},this.applyLocaleAtRuntime=async()=>{const e=this.iframe.contentWindow?.PDFViewerApplication,t=this.iframe.contentDocument;if(!e||!t)return!1;await e.initializedPromise;const i=e.externalServices?.createL10n;if("function"!=typeof i)return!1;const r=await i();if(!r)return!1;await(e.l10n?.destroy?.()),e.l10n=r;const s=e.appConfig?.appContainer||t.documentElement;return!!s&&("function"==typeof r.getDirection&&t.documentElement?.setAttribute("dir",r.getDirection()),"function"==typeof r.translate&&await r.translate(s),!0)},this.injectLocaleData=async()=>{const e=this.iframe.contentDocument,t=this.getAttribute("locale");if(!t)return void this.cleanupLocaleResource();const i=await(import("./locale-kKW4I85A.js"));if(!Object.keys(JSON.parse(i.default)).includes(t))return void this.cleanupLocaleResource();const r=this.formatTemplate(this.getAttribute("locale-src-template")||a.localeSrcTemplate,{locale:t}),s={[String(t)]:r},n=e.createElement("link");n.rel="resource",n.type="application/l10n",this.cleanupLocaleResource(),this.localeResourceUrl=URL.createObjectURL(new Blob([JSON.stringify(s)],{type:"application/json"})),n.href=this.localeResourceUrl,this.iframe.contentDocument?.head.appendChild(n),this.localeResourceLink=n},this.onViewerAppCreated=()=>new Promise(e=>{const t=this.iframe.contentWindow;if(t.PDFViewerApplication)return e(t.PDFViewerApplication);let i;Object.defineProperty(t,"PDFViewerApplication",{get:()=>i,set(r){i=r,e(r),delete t.PDFViewerApplication,t.PDFViewerApplication=r},configurable:!0})}),this.applyViewerOptions=()=>{const e=this.iframe.contentWindow?.PDFViewerApplicationOptions;e?.set("workerSrc",this.getAttribute("worker-src")||a.workerSrc),e?.set("debuggerSrc",this.getAttribute("debugger-src")||a.debuggerSrc),e?.set("cMapUrl",this.getAttribute("c-map-url")||a.cMapUrl),e?.set("iccUrl",this.getAttribute("icc-url")||a.iccUrl),e?.set("imageResourcesPath",this.getAttribute("image-resources-path")||a.imageResourcesPath),e?.set("sandboxBundleSrc",this.getAttribute("sandbox-bundle-src")||a.sandboxBundleSrc),e?.set("standardFontDataUrl",this.getAttribute("standard-font-data-url")||a.standardFontDataUrl),e?.set("wasmUrl",this.getAttribute("wasm-url")||a.wasmUrl),e?.set("defaultUrl",this.getFullPath(this.getAttribute("src")||a.src)),e?.set("disablePreferences",!0),e?.set("eventBusDispatchToDOM",!0),e?.set("localeProperties",{lang:this.getAttribute("locale")||a.locale}),e?.set("viewerCssTheme",this.getCssThemeOption());for(const[t,i]of Object.entries(this.optionsToSet))e?.set(t,i);this.optionsToSet={}},this.getIframeLocationHash=()=>{const e={page:this.getAttribute("page")||a.page,zoom:this.getAttribute("zoom")||a.zoom,pagemode:this.getAttribute("pagemode")||a.pagemode,search:this.getAttribute("search")||a.search,phrase:this.getAttribute("phrase")||a.phrase,locale:this.getAttribute("locale")||a.locale};return"#"+Object.entries(e).map(([e,t])=>`${e}=${t}`).join("&")},this.buildViewerEntry=async()=>new Promise(async e=>{const t=await(import("./_virtual_pdfjs-viewer-html-Db40JDMA.js")),i=window.location.origin,a=["default-src 'none'",`script-src 'self' 'wasm-unsafe-eval' ${i}`,`script-src-elem ${i}`,`worker-src ${i} blob:`,`style-src ${i} 'unsafe-inline'`,`img-src ${i} blob: data:`,"media-src blob:",`font-src ${i} data:`,"connect-src * blob: data:","base-uri 'none'","form-action 'none'"].join("; "),n=t.default.replace(/<meta\s+http-equiv="Content-Security-Policy"[^>]*>/i,`<meta http-equiv="Content-Security-Policy" content="${a};" />`).replace("</head>",`\n <link rel="stylesheet" href="${r}">\n <link rel="stylesheet" href="${s}">\n ${Array.from(this.viewerStyles).map(e=>`<style>${e}</style>`).join("\n")}\n </head>`);this.iframe.addEventListener("load",()=>e(),{once:!0}),this.iframe.srcdoc=n}),this.setupViewerApp=async()=>{const e=await this.onViewerAppCreated();return this.applyViewerOptions(),await(e?.initializedPromise),this.applyViewerTheme(),this.applyQueuedRuntimeStyles(),{viewerApp:e}},this.buildViewerApp=async()=>{await this.applyIframeHash();const e=this.setupViewerApp();return await this.injectLocaleData(),await this.injectScript(t),await this.injectScript(i),await e},this.attachShadow({mode:"open"}).innerHTML='\n <style>:host{width:100%;display:block;overflow:hidden}:host iframe{height:100%}</style>\n <iframe frameborder="0" width="100%" loading="lazy"></iframe>\n '}static get observedAttributes(){return["src","locale","viewer-css-theme","worker-src","debugger-src","c-map-url","icc-url","image-resources-path","sandbox-bundle-src","standard-font-data-url","wasm-url","page","search","phrase","zoom","pagemode","iframe-title"]}formatTemplate(e,t){return e.replace(/\{(\w+)\}/g,(e,i)=>{if(!(i in t))throw new Error(`Missing param: ${i}`);return String(t[i])})}getFullPath(e){if(!e)return e;try{return new URL(e,window.location.href).href}catch{return e}}getCssThemeOption(){const e=this.getAttribute("viewer-css-theme");return Object.keys(n).includes(e)?n[e]:n[a.viewerCssTheme]}applyViewerTheme(){const e=this.getCssThemeOption();(this.iframe.contentWindow?.PDFViewerApplicationOptions)?.set("viewerCssTheme",e);const t=this.iframe.contentDocument;if(!t?.documentElement)return;const i=e===n.LIGHT?"light":e===n.DARK?"dark":"";i?t.documentElement.style.setProperty("color-scheme",i):t.documentElement.style.removeProperty("color-scheme")}appendRuntimeStyle(e){const t=this.iframe?.contentDocument;if(!t?.head||!e)return;if(Array.from(t.querySelectorAll("style")).some(t=>t.textContent===e))return;const i=t.createElement("style");i.setAttribute("data-pdfjs-viewer-runtime-style","true"),i.textContent=e,t.head.appendChild(i)}applyQueuedRuntimeStyles(){this.viewerStyles.forEach(e=>{this.appendRuntimeStyle(e)})}injectScript(e,t="module"){const i=this.iframe.contentDocument;if(!i)return Promise.resolve();if(!i.head){const e=i.createElement("head");i.documentElement?.prepend(e)}return new Promise((r,s)=>{let a="";const n=e=>{a=` (CSP ${e.effectiveDirective||e.violatedDirective||"unknown-directive"} blocked ${e.blockedURI||"unknown-uri"})`},o=()=>{i.removeEventListener("securitypolicyviolation",n)};i.addEventListener("securitypolicyviolation",n);const c=i.createElement("script");c.type=t,c.src=e,c.addEventListener("load",()=>{o(),r()},{once:!0}),c.addEventListener("error",()=>{o(),s(new Error(`Unable to load script: ${e}${a}`))},{once:!0}),i.head?.appendChild(c)})}cleanupLocaleResource(){this.localeResourceLink&&(this.localeResourceLink.remove(),this.localeResourceLink=void 0),this.localeResourceUrl&&(URL.revokeObjectURL(this.localeResourceUrl),this.localeResourceUrl=void 0)}async connectedCallback(){this.iframe=this.shadowRoot?.querySelector("iframe"),this.iframe.setAttribute("title",this.getAttribute("iframe-title")||a.iframeTitle),this.initPromise=(async()=>(await this.buildViewerEntry(),await this.buildViewerApp()))()}disconnectedCallback(){for(this.cleanupLocaleResource(),this.iframe.src="about:blank";this.firstChild;)this.removeChild(this.firstChild)}async attributeChangedCallback(e,t,i){if(t===i)return;if(!this.iframe)return;const r={"debugger-src":{key:"debuggerSrc",fallback:a.debuggerSrc},"c-map-url":{key:"cMapUrl",fallback:a.cMapUrl},"icc-url":{key:"iccUrl",fallback:a.iccUrl},"image-resources-path":{key:"imageResourcesPath",fallback:a.imageResourcesPath},"sandbox-bundle-src":{key:"sandboxBundleSrc",fallback:a.sandboxBundleSrc},"standard-font-data-url":{key:"standardFontDataUrl",fallback:a.standardFontDataUrl},"wasm-url":{key:"wasmUrl",fallback:a.wasmUrl}};if("worker-src"!==e){if(e in r){const t=this.iframe.contentWindow?.PDFViewerApplicationOptions,{key:s,fallback:a}=r[e];return void t?.set(s,i||a)}switch(e){case"src":{const e=this.iframe.contentWindow?.PDFViewerApplication;if(e){await e.initializedPromise;const t=this.getFullPath(i||a.src);t&&e.open({url:t})}return}case"locale":return(this.iframe.contentWindow?.PDFViewerApplicationOptions)?.set("localeProperties",{lang:i||a.locale}),this.cleanupLocaleResource(),await this.injectLocaleData(),await this.applyLocaleAtRuntime()||(this.initPromise=(async()=>(await this.buildViewerEntry(),await this.buildViewerApp()))(),await this.initPromise),void await this.applyIframeHash();case"iframe-title":return void this.iframe.setAttribute("title",i||a.iframeTitle);case"viewer-css-theme":return void this.applyViewerTheme();default:await this.applyIframeHash()}}else(this.iframe.contentWindow?.PDFViewerApplicationOptions)?.set("workerSrc",i||a.workerSrc)}async injectViewerStyles(e){e&&(this.viewerStyles.add(e),this.appendRuntimeStyle(e))}async setViewerOptions(e={}){return this.optionsToSet=e,await this.initPromise,{viewerOptions:this.iframe.contentWindow?.PDFViewerApplicationOptions}}};window.customElements.get("pdfjs-viewer-element")||window.customElements.define("pdfjs-viewer-element",o);export{o as PdfjsViewerElement,o as default,n as ViewerCssTheme};
|
|
1
|
+
var e=new URL("./pdf.worker.min.mjs",""+import.meta.url).href,t=new URL("./pdf.mjs",""+import.meta.url).href,i=new URL("./viewer.mjs",""+import.meta.url).href,r=new URL("./viewer.css",""+import.meta.url).href,s=new URL("./paper-and-ink.css",""+import.meta.url).href,a={src:"",iframeTitle:"PDF viewer window",page:"",search:"",phrase:"",zoom:"",pagemode:"none",locale:"",viewerCssTheme:"AUTOMATIC",workerSrc:e,debuggerSrc:"./debugger.mjs",cMapUrl:"../web/cmaps/",iccUrl:"../web/iccs/",imageResourcesPath:"./images/",sandboxBundleSrc:"../build/pdf.sandbox.mjs",standardFontDataUrl:"../web/standard_fonts/",wasmUrl:"../web/wasm/",localeSrcTemplate:"https://cdn.jsdelivr.net/gh/mozilla-l10n/firefox-l10n@main/{locale}/toolkit/toolkit/pdfviewer/viewer.ftl"},n={AUTOMATIC:0,LIGHT:1,DARK:2},o=class extends HTMLElement{constructor(){super(),this.initPromise=Promise.resolve({}),this.viewerStyles=new Set,this.optionsToSet={},this.applyIframeHash=async()=>{const e=this.iframe?.contentWindow;if(!e)return;const t=this.getIframeLocationHash();e.location.hash!==t&&await new Promise(i=>{let r=!1;const s=()=>{r||(r=!0,e.removeEventListener("hashchange",s),i())};e.addEventListener("hashchange",s),e.location.hash=t,queueMicrotask(()=>{e.location.hash===t&&s()})})},this.applyLocaleAtRuntime=async()=>{const e=this.iframe.contentWindow?.PDFViewerApplication,t=this.iframe.contentDocument;if(!e||!t)return!1;await e.initializedPromise;const i=e.externalServices?.createL10n;if("function"!=typeof i)return!1;const r=await i();if(!r)return!1;await(e.l10n?.destroy?.()),e.l10n=r;const s=e.appConfig?.appContainer||t.documentElement;return!!s&&("function"==typeof r.getDirection&&t.documentElement?.setAttribute("dir",r.getDirection()),"function"==typeof r.translate&&await r.translate(s),!0)},this.injectLocaleData=async()=>{const e=this.iframe.contentDocument,t=this.getAttribute("locale");if(!t)return void this.cleanupLocaleResource();const i=await(import("./locale-kKW4I85A.js"));if(!Object.keys(JSON.parse(i.default)).includes(t))return void this.cleanupLocaleResource();const r=this.formatTemplate(this.getAttribute("locale-src-template")||a.localeSrcTemplate,{locale:t}),s={[String(t)]:r},n=e.createElement("link");n.rel="resource",n.type="application/l10n",this.cleanupLocaleResource(),this.localeResourceUrl=URL.createObjectURL(new Blob([JSON.stringify(s)],{type:"application/json"})),n.href=this.localeResourceUrl,this.iframe.contentDocument?.head.appendChild(n),this.localeResourceLink=n},this.onViewerAppCreated=()=>new Promise(e=>{const t=this.iframe.contentWindow;if(t.PDFViewerApplication)return e(t.PDFViewerApplication);let i;Object.defineProperty(t,"PDFViewerApplication",{get:()=>i,set(r){i=r,e(r),delete t.PDFViewerApplication,t.PDFViewerApplication=r},configurable:!0})}),this.applyViewerOptions=()=>{const e=this.iframe.contentWindow?.PDFViewerApplicationOptions;e?.set("workerSrc",this.getAttribute("worker-src")||a.workerSrc),e?.set("debuggerSrc",this.getAttribute("debugger-src")||a.debuggerSrc),e?.set("cMapUrl",this.getAttribute("c-map-url")||a.cMapUrl),e?.set("iccUrl",this.getAttribute("icc-url")||a.iccUrl),e?.set("imageResourcesPath",this.getAttribute("image-resources-path")||a.imageResourcesPath),e?.set("sandboxBundleSrc",this.getAttribute("sandbox-bundle-src")||a.sandboxBundleSrc),e?.set("standardFontDataUrl",this.getAttribute("standard-font-data-url")||a.standardFontDataUrl),e?.set("wasmUrl",this.getAttribute("wasm-url")||a.wasmUrl),e?.set("defaultUrl",this.getFullPath(this.getAttribute("src")||a.src)),e?.set("disablePreferences",!0),e?.set("eventBusDispatchToDOM",!0),e?.set("localeProperties",{lang:this.getAttribute("locale")||a.locale}),e?.set("viewerCssTheme",this.getCssThemeOption());for(const[t,i]of Object.entries(this.optionsToSet))e?.set(t,i);this.optionsToSet={}},this.getIframeLocationHash=()=>{const e={page:this.getAttribute("page")||a.page,zoom:this.getAttribute("zoom")||a.zoom,pagemode:this.getAttribute("pagemode")||a.pagemode,search:this.getAttribute("search")||a.search,phrase:this.getAttribute("phrase")||a.phrase,locale:this.getAttribute("locale")||a.locale};return"#"+Object.entries(e).map(([e,t])=>`${e}=${t}`).join("&")},this.buildViewerEntry=async()=>new Promise(async e=>{const n=await(import("./_virtual_pdfjs-viewer-html-Db40JDMA.js")),o=e=>{try{const t=new URL(e,window.location.href);if("http:"===t.protocol||"https:"===t.protocol)return t.origin}catch{}return""},c=e=>Array.from(new Set(e.filter(Boolean))).join(" "),l=c(["'self'",window.location.origin,o(t),o(i)]),h=c(["'self'",window.location.origin,o(r),o(s)]),p=["default-src 'none'",`script-src ${l} 'wasm-unsafe-eval'`,`script-src-elem ${l}`,`worker-src ${c(["'self'","blob:",window.location.origin,o(this.getAttribute("worker-src")||a.workerSrc)])}`,`style-src ${h} 'unsafe-inline'`,`img-src ${h} blob: data:`,"media-src blob:",`font-src ${h} data:`,"connect-src * blob: data:","base-uri 'none'","form-action 'none'"].join("; "),u=n.default.replace(/<meta\s+http-equiv="Content-Security-Policy"[^>]*>/i,`<meta http-equiv="Content-Security-Policy" content="${p};" />`).replace("</head>",`\n <link rel="stylesheet" href="${r}">\n <link rel="stylesheet" href="${s}">\n ${Array.from(this.viewerStyles).map(e=>`<style>${e}</style>`).join("\n")}\n </head>`);this.iframe.addEventListener("load",()=>e(),{once:!0}),this.iframe.srcdoc=u}),this.setupViewerApp=async()=>{const e=await this.onViewerAppCreated();return this.applyViewerOptions(),await(e?.initializedPromise),this.applyViewerTheme(),this.applyQueuedRuntimeStyles(),{viewerApp:e}},this.buildViewerApp=async()=>{await this.applyIframeHash();const e=this.setupViewerApp();return await this.injectLocaleData(),await this.injectScript(t),await this.injectScript(i),await e},this.attachShadow({mode:"open"}).innerHTML='\n <style>:host{width:100%;display:block;overflow:hidden}:host iframe{height:100%}</style>\n <iframe frameborder="0" width="100%" loading="lazy"></iframe>\n '}static get observedAttributes(){return["src","locale","viewer-css-theme","worker-src","debugger-src","c-map-url","icc-url","image-resources-path","sandbox-bundle-src","standard-font-data-url","wasm-url","page","search","phrase","zoom","pagemode","iframe-title"]}formatTemplate(e,t){return e.replace(/\{(\w+)\}/g,(e,i)=>{if(!(i in t))throw new Error(`Missing param: ${i}`);return String(t[i])})}getFullPath(e){if(!e)return e;try{return new URL(e,window.location.href).href}catch{return e}}getCssThemeOption(){const e=this.getAttribute("viewer-css-theme");return Object.keys(n).includes(e)?n[e]:n[a.viewerCssTheme]}applyViewerTheme(){const e=this.getCssThemeOption();(this.iframe.contentWindow?.PDFViewerApplicationOptions)?.set("viewerCssTheme",e);const t=this.iframe.contentDocument;if(!t?.documentElement)return;const i=e===n.LIGHT?"light":e===n.DARK?"dark":"";i?t.documentElement.style.setProperty("color-scheme",i):t.documentElement.style.removeProperty("color-scheme")}appendRuntimeStyle(e){const t=this.iframe?.contentDocument;if(!t?.head||!e)return;if(Array.from(t.querySelectorAll("style")).some(t=>t.textContent===e))return;const i=t.createElement("style");i.setAttribute("data-pdfjs-viewer-runtime-style","true"),i.textContent=e,t.head.appendChild(i)}applyQueuedRuntimeStyles(){this.viewerStyles.forEach(e=>{this.appendRuntimeStyle(e)})}injectScript(e,t="module"){const i=this.iframe.contentDocument;if(!i)return Promise.resolve();if(!i.head){const e=i.createElement("head");i.documentElement?.prepend(e)}return new Promise((r,s)=>{let a="";const n=e=>{a=` (CSP ${e.effectiveDirective||e.violatedDirective||"unknown-directive"} blocked ${e.blockedURI||"unknown-uri"})`},o=()=>{i.removeEventListener("securitypolicyviolation",n)};i.addEventListener("securitypolicyviolation",n);const c=i.createElement("script");c.type=t,c.src=e,c.addEventListener("load",()=>{o(),r()},{once:!0}),c.addEventListener("error",()=>{o(),s(new Error(`Unable to load script: ${e}${a}`))},{once:!0}),i.head?.appendChild(c)})}cleanupLocaleResource(){this.localeResourceLink&&(this.localeResourceLink.remove(),this.localeResourceLink=void 0),this.localeResourceUrl&&(URL.revokeObjectURL(this.localeResourceUrl),this.localeResourceUrl=void 0)}async connectedCallback(){this.iframe=this.shadowRoot?.querySelector("iframe"),this.iframe.setAttribute("title",this.getAttribute("iframe-title")||a.iframeTitle),this.initPromise=(async()=>(await this.buildViewerEntry(),await this.buildViewerApp()))()}disconnectedCallback(){for(this.cleanupLocaleResource(),this.iframe.src="about:blank";this.firstChild;)this.removeChild(this.firstChild)}async attributeChangedCallback(e,t,i){if(t===i)return;if(!this.iframe)return;const r={"debugger-src":{key:"debuggerSrc",fallback:a.debuggerSrc},"c-map-url":{key:"cMapUrl",fallback:a.cMapUrl},"icc-url":{key:"iccUrl",fallback:a.iccUrl},"image-resources-path":{key:"imageResourcesPath",fallback:a.imageResourcesPath},"sandbox-bundle-src":{key:"sandboxBundleSrc",fallback:a.sandboxBundleSrc},"standard-font-data-url":{key:"standardFontDataUrl",fallback:a.standardFontDataUrl},"wasm-url":{key:"wasmUrl",fallback:a.wasmUrl}};if("worker-src"!==e){if(e in r){const t=this.iframe.contentWindow?.PDFViewerApplicationOptions,{key:s,fallback:a}=r[e];return void t?.set(s,i||a)}switch(e){case"src":{const e=this.iframe.contentWindow?.PDFViewerApplication;if(e){await e.initializedPromise;const t=this.getFullPath(i||a.src);t&&e.open({url:t})}return}case"locale":return(this.iframe.contentWindow?.PDFViewerApplicationOptions)?.set("localeProperties",{lang:i||a.locale}),this.cleanupLocaleResource(),await this.injectLocaleData(),await this.applyLocaleAtRuntime()||(this.initPromise=(async()=>(await this.buildViewerEntry(),await this.buildViewerApp()))(),await this.initPromise),void await this.applyIframeHash();case"iframe-title":return void this.iframe.setAttribute("title",i||a.iframeTitle);case"viewer-css-theme":return void this.applyViewerTheme();default:await this.applyIframeHash()}}else(this.iframe.contentWindow?.PDFViewerApplicationOptions)?.set("workerSrc",i||a.workerSrc)}async injectViewerStyles(e){e&&(this.viewerStyles.add(e),this.appendRuntimeStyle(e))}async setViewerOptions(e={}){return this.optionsToSet=e,await this.initPromise,{viewerOptions:this.iframe.contentWindow?.PDFViewerApplicationOptions}}};window.customElements.get("pdfjs-viewer-element")||window.customElements.define("pdfjs-viewer-element",o);export{o as PdfjsViewerElement,o as default,n as ViewerCssTheme};
|