# Biblioteca externa - PDFJS

## Atualização

-   Origem: [https://github.com/mozilla/pdf.js/releases](https://github.com/mozilla/pdf.js/releases)
-   A biblioteca possui um controle para evitar Cross Origin Requests, portanto ao atualizar é necessário desabilitar removendo fora o código da função validateFileURL:

### viewer.mjs:

```diff
 const PDFViewerApplication = {
     ...
     async run(config) {
         ...
         file = params.get("file") ?? _app_options_js__WEBPACK_IMPORTED_MODULE_2__.AppOptions.get("defaultUrl");
-        validateFileURL(file);
         const fileInput = this._openFileInput = document.createElement
         ("input");
         ....
- {
-  const HOSTED_VIEWER_ORIGINS = ["null", "http://mozilla.github.io", "https://mozilla.github.io"];
-  var validateFileURL = function (file) {
-    if (!file) {
-      return;
-    }
-    try {
-      const viewerOrigin = new URL(window.location.href).origin || "null";
-      if (HOSTED_VIEWER_ORIGINS.includes(viewerOrigin)) {
-        return;
-      }
-      const fileOrigin = new URL(file, window.location.href).origin;
-      if (fileOrigin !== viewerOrigin) {
-        throw new Error("file origin does not match viewer's");
-      }
-    } catch (ex) {
-      PDFViewerApplication._documentError("pdfjs-loading-error", {
-        message: ex.message
-      });
-      throw ex;
-    }
-  };
}
```

-   Tambem há uma chamada ao frame parent para disparo de eventos, este deve ser removido e trocado por um postMessage

```diff
 function webViewerLoad() {
  const config = getViewerConfiguration();
-  const event = new CustomEvent("webviewerloaded", {
-    bubbles: true,
-    cancelable: true,
-    detail: {
-      source: window
-    }
-  });
  try {
-    parent.document.dispatchEvent(event);
+    const id = location.search.match(/govbrId=([^&]+)/)[1];
+    parent.postMessage(['__pdfjs_webviewerloaded', id], '*');
+    window.onmessage(async(event) => {
+      if(Array.isArray(event.data) && event.data[0] === '__govbr_pdfjs_init' && event.data[1] === id){
+        if(/^https:\/\/.*\.govbr(\.cloud|\.com\.br)|^http:\/\/localhost/.test(event?.origin || '')){
+          const config = event.data[2];
+          const script = document.createElement('script');
+          script.src = config.urlCDN + '/genexus/pdfjs/' + config.versaoArquivo + '/GOVBRPDFJSWidgetAssinatura.mjs';
+          script.type = 'module';
+          script.onload = async () => {
+            window['__govbr_wdgt'] = new GOVBRPDFJSWidgetAssinatura(PDFViewerApplication, config, id)
+            await __govbr_wdgt.init();
+            window.PDFViewerApplicationOptions.set('locale', 'pt-BR')
+            parent.postMessage(['__pdfjs_wdgt_pronto', id], '*');
+          }
+          document.head.append(script);
+        }
+        else {
+          console.log('Domínio inválido - ' + event?.origin)
+        }
+      }
+    });
   } catch (ex) {
     console.error(`webviewerloaded: ${ex}`);
     document.dispatchEvent(event);
   }
   _app_js__WEBPACK_IMPORTED_MODULE_3__.PDFViewerApplication.run(config);
 }:
```
