Skip to content

Commit

Permalink
#2440 added the flag [useInlineScripts] and removed a part of the C…
Browse files Browse the repository at this point in the history
…SP auto-detection
  • Loading branch information
stephanrauh committed Jul 27, 2024
1 parent 02acbd8 commit 7aaaef1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 24 deletions.
2 changes: 1 addition & 1 deletion projects/ngx-extended-pdf-viewer/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -576,4 +576,4 @@
- 21.0.0-alpha.11 #2440 stopped loading the JS file in an inline script, thus restoring compatiblity to CSP; merged the pdf.mjs and the viewer.mjs files so there's one request less; #376 simplified the way the `workerSrc()` parameter is passed to pdf.js; delete `globalThis.pdfJsLib` after using it to pass the objects of pdf.js to the viewer; detect CSP and load the `op-chaining-support.js` as an inline script if possible to avoid an additional request
- 21.0.0-alpha.12 #2452 fixed printing (it still depended on `window.PDFViewerApplication`); fixed two Sonarqube findings; #8 / #2445 get rid of wrong HTML issue reports in Chrome
- 21.0.0-alpha.13 #2458 allow `minZoom` and `maxZoom` to be non-numerical values, but only if they are identical
- 21.0.0-alpha.14 address Sonarqube issues; reverted the additions of #1659 because pdf.js has introduced the flag `enableHWA` that does the same as my `activateWillReadFrequentlyFlag` did; removed several outdated attributes from `pdfDefaultOptions` #2464 deactivate the zoom buttons when reaching the maximum/minimum zoom level; updated to pdf.js 4.5
- 21.0.0-alpha.14 address Sonarqube issues; reverted the additions of #1659 because pdf.js has introduced the flag `enableHWA` that does the same as my `activateWillReadFrequentlyFlag` did; removed several outdated attributes from `pdfDefaultOptions` #2464 deactivate the zoom buttons when reaching the maximum/minimum zoom level; updated to pdf.js 4.5; added the flag `[useInlineScripts]` and removed a part of the CSP auto-detection
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,9 @@ export class NgxExtendedPdfViewerComponent implements OnInit, OnChanges, OnDestr

public _showSidebarButton: ResponsiveVisibility = true;

@Input()
public useInlineScripts = true;

public viewerPositionTop = '32px';

/** pdf.js can show signatures, but fails to verify them. So they are switched off by default.
Expand Down Expand Up @@ -986,7 +989,7 @@ export class NgxExtendedPdfViewerComponent implements OnInit, OnChanges, OnDestr
if (isPlatformBrowser(this.platformId)) {
this.addTranslationsUnlessProvidedByTheUser();
await this.waitUntilOldComponentIsGone();
await this.pdfScriptLoaderService.ensurePdfJsHasBeenLoaded();
await this.pdfScriptLoaderService.ensurePdfJsHasBeenLoaded(this.useInlineScripts);
this.formSupport.registerFormSupportWithPdfjs(this.ngZone, this.pdfScriptLoaderService.PDFViewerApplication);
this.keyboardManager.registerKeyboardListener(this.pdfScriptLoaderService.PDFViewerApplication);
this.doInitPDFViewer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export class PDFScriptLoaderService implements OnDestroy {
});
}

private addScriptOpChainingSupport(): Promise<boolean> {
if (this.isCSPApplied()) {
private addScriptOpChainingSupport(useInlineScripts: boolean): Promise<boolean> {
if (!useInlineScripts || this.isCSPApplied()) {
return new Promise((resolve) => {
const script = this.createScriptElement(pdfDefaultOptions.assetsFolder + '/op-chaining-support.js');
script.onload = () => {
Expand Down Expand Up @@ -133,18 +133,7 @@ new (function () {
if (this.isCSPAppliedViaMetaTag()) {
return true;
}
try {
eval('1');
return false;
} catch (e) {
if (e instanceof EvalError) {
console.log('CSP is applied');
return true;
} else {
console.log('An unexpected error occurred', e);
return false;
}
}
return false;
}

private createScriptElement(sourcePath: string): HTMLScriptElement {
Expand All @@ -170,7 +159,7 @@ new (function () {
return assets + artifactPath + versionSuffix + es5 + suffix;
}

private async loadViewer(): Promise<void> {
private async loadViewer(useInlineScripts: boolean): Promise<void> {
return new Promise((resolve) => {
const viewerPath = this.getPdfJsPath('viewer');
const listener = (event: CustomEvent) => {
Expand Down Expand Up @@ -203,12 +192,12 @@ new (function () {
});
}

public async ensurePdfJsHasBeenLoaded(): Promise<boolean> {
public async ensurePdfJsHasBeenLoaded(useInlineScripts: boolean): Promise<boolean> {
if (this.PDFViewerApplication) {
return true;
}
this._needsES5 = await this.needsES5();
await this.loadViewer();
this._needsES5 = await this.needsES5(useInlineScripts);
await this.loadViewer(useInlineScripts);
return this.PDFViewerApplication !== undefined;
}

Expand Down Expand Up @@ -272,7 +261,7 @@ new (function () {
return false;
}

private async needsES5(): Promise<boolean> {
private async needsES5(useInlineScripts: boolean): Promise<boolean> {
if (typeof window === 'undefined') {
// server-side rendering
return false;
Expand All @@ -286,15 +275,15 @@ new (function () {
this._needsES5 = true;
return true;
}
this._needsES5 = !(await this.ngxExtendedPdfViewerCanRunModernJSCode());
this._needsES5 = !(await this.ngxExtendedPdfViewerCanRunModernJSCode(useInlineScripts));
}
return this._needsES5;
}

private ngxExtendedPdfViewerCanRunModernJSCode(): Promise<boolean> {
private ngxExtendedPdfViewerCanRunModernJSCode(useInlineScripts: boolean): Promise<boolean> {
return new Promise((resolve) => {
const support = (<any>globalThis).ngxExtendedPdfViewerCanRunModernJSCode;
support !== undefined ? resolve(support) : resolve(this.addScriptOpChainingSupport());
support !== undefined ? resolve(support) : resolve(this.addScriptOpChainingSupport(useInlineScripts));
});
}
}

0 comments on commit 7aaaef1

Please sign in to comment.