Skip to content

Commit

Permalink
Send metadatachange message to parent frame in PDF viewer
Browse files Browse the repository at this point in the history
When visiting `http://<VIA_DOMAIN>/<PDF_URL>` then the PDF viewer is
displayed inside an iframe. In order for the top frame rendered by the
`proxy` view to reflect the document title and PDF URL the PDF viewer
iframe needs to send the same `metadatachange` message that ViaHTML
sends.

The "real" PDF title is not available until the document has loaded, and
is often incorrect anyway. Therefore we send the filename of the PDF
extracted from the URL if available, or the domain from which the PDF
was loaded otherwise.
  • Loading branch information
robertknight committed May 4, 2021
1 parent 58d3b22 commit 822ff19
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions via/templates/pdf_viewer.html.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,25 @@
// URL of the Hypothesis client's boot script.
window.CLIENT_EMBED_URL = {{ client_embed_url | tojson }};
// When this PDF is hosted inside an iframe, notify the parent frame of
// the PDF's metadata. ViaHTML sends the same message for HTML documents.
//
// The PDF filename is used as a title because that is immediately available
// before the PDF has loaded.
if (window.parent !== window) {
const pdfURL = new URL(window.PDF_URL);
const pathSegments = pdfURL.pathname.split('/').filter(s => s !== '');
const filename = pathSegments.length > 0 ? pathSegments[pathSegments.length - 1] : pdfURL.hostname;
window.parent.postMessage({
type: 'metadatachange',
metadata: {
location: pdfURL.href,
title: filename
},
}, '*');
}
</script>

<script src="{{ static_url("via:static/js/pdfjs-init.min.js") }}"></script>
Expand Down

0 comments on commit 822ff19

Please sign in to comment.