I found this article which discusses another way to get around cross-origin if you’re still on the same domain (vs subdomain):
I created copies of the four linked javascript files and added document.domain = 'mydomain.com'
to them.
I modified my viewer to look like this:
<!doctype html>
<html moznomarginboxes mozdisallowselectionprint>
<head>
<base href="{% static 'pdfjsexpress/public/ui/' %}">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<link rel="stylesheet" href="style.css">
<title>WebViewer UI</title>
</head>
<body>
<div id="app"></div>
<div id="line-connector-wrapper">
<div id="line-connector-root"></div>
</div>
<script>
// Proxy whether we're running in production and set the domain to enable cross origin iframe access
// https://pdfjs.community/t/storing-staticfiles-on-a-separate-subdomain/828
// https://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy
if (window.location.href.startsWith('https')) {
document.domain = 'onuptick.com'
}
</script>
<script src="../core/CoreControls.crossdomain.js"></script>
<script src="../core/pdfjs/PDFJSDocumentType.crossdomain.js"></script>
<script src="../core/pdfjs/UIConfig.crossdomain.js"></script>
<script src="webviewer-ui.min.crossdomain.js"></script>
</body>
</html>
And added this useEffect
into my react component just before the useEffect call to initialise the WebViewer:
useEffect(() => {
if (crossOrigin) {
document.domain = 'onuptick.com' // https://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy
}
})
However, this didn’t work. I get the same error.
So I looked at the iframe src that the pdfjs.express library was creating, and opened that in a tab directly to eliminate the cross tab communication and see whether the error remains. So to be clear that means I’ve got a tab open that is the /core/ui/index.html with the #d=
pointing to a PDF file.
I get the same result “Error: Setting up fake worker failed: “Cannot read property ‘WorkerMessageHandler’ of undefined”.”
So the error is happening in PDF.js / Express somewhere, not the interframe communication.
This means the links you’ve posted aren’t relevant as I can’t use postMessage (because there’s no frame involved). It’s just that the /ui/index.html is on a different domain to the scripts it’s loading (because staticfiles are hosted on a different subdomain).
Any ideas?