mirror of
https://github.com/zen-browser/pdf.js.git
synced 2025-07-08 01:10:08 +02:00
<object> / <embed> support in Chromium extension
This commit is contained in:
parent
ae92b6f96f
commit
3fdd334a73
3 changed files with 116 additions and 8 deletions
|
@ -25,15 +25,108 @@ function getViewerURL(pdf_url) {
|
|||
return VIEWER_URL + '?file=' + encodeURIComponent(pdf_url);
|
||||
}
|
||||
|
||||
document.addEventListener('beforeload', function(event) {
|
||||
var elem = event.target;
|
||||
if (elem.nodeName.toUpperCase() !== 'EMBED') {
|
||||
// (un)prefixed property names
|
||||
var createShadowRoot, shadowRoot;
|
||||
if (typeof Element.prototype.createShadowRoot !== 'undefined') {
|
||||
// Chrome 35+
|
||||
createShadowRoot = 'createShadowRoot';
|
||||
shadowRoot = 'shadowRoot';
|
||||
} else if (typeof Element.prototype.webkitCreateShadowRoot !== 'undefined') {
|
||||
// Chrome 25 - 34
|
||||
createShadowRoot = 'webkitCreateShadowRoot';
|
||||
shadowRoot = 'webkitShadowRoot';
|
||||
try {
|
||||
document.createElement('embed').webkitCreateShadowRoot();
|
||||
} catch (e) {
|
||||
// Only supported since Chrome 33.
|
||||
createShadowRoot = shadowRoot = '';
|
||||
}
|
||||
}
|
||||
|
||||
// Only observe the document if we can make use of Shadow DOM.
|
||||
if (createShadowRoot) {
|
||||
if ('animation' in document.documentElement.style) {
|
||||
document.addEventListener('animationstart', onAnimationStart, true);
|
||||
} else {
|
||||
document.addEventListener('webkitAnimationStart', onAnimationStart, true);
|
||||
}
|
||||
}
|
||||
|
||||
function onAnimationStart(event) {
|
||||
if (event.animationName === 'pdfjs-detected-object-or-embed') {
|
||||
watchObjectOrEmbed(event.target);
|
||||
}
|
||||
}
|
||||
|
||||
// Called for every <object> or <embed> element in the page.
|
||||
// It does not trigger any Mutation observers, but it may modify the
|
||||
// shadow DOM rooted under the given element.
|
||||
// Calling this function multiple times for the same element is safe, i.e.
|
||||
// it has no side effects.
|
||||
function watchObjectOrEmbed(elem) {
|
||||
var mimeType = elem.type;
|
||||
if (mimeType && 'application/pdf' !== mimeType.toLowerCase()) {
|
||||
return;
|
||||
}
|
||||
if (!/^application\/pdf$/i.test(elem.type)) {
|
||||
// <embed src> <object data>
|
||||
var srcAttribute = 'src' in elem ? 'src' : 'data';
|
||||
var path = elem[srcAttribute];
|
||||
if (!mimeType && !/\.pdf($|[?#])/i.test(path)) {
|
||||
return;
|
||||
}
|
||||
event.preventDefault();
|
||||
elem.type = 'text/html';
|
||||
elem.src = getViewerURL(elem.src);
|
||||
}, true);
|
||||
|
||||
if (elem[shadowRoot]) {
|
||||
// If the element already has a shadow root, assume that we've already
|
||||
// seen this element.
|
||||
return;
|
||||
}
|
||||
elem[createShadowRoot]();
|
||||
|
||||
function updateViewerFrame() {
|
||||
var path = elem[srcAttribute];
|
||||
if (!path) {
|
||||
elem[shadowRoot].textContent = '';
|
||||
} else {
|
||||
elem[shadowRoot].innerHTML =
|
||||
// Set display: inline-block; to the host element (<embed>/<object>) to
|
||||
// ensure that the dimensions defined on the host element are applied to
|
||||
// the iframe (http://crbug.com/358648).
|
||||
// The styles are declared in the shadow DOM to allow page authors to
|
||||
// override these styles (e.g. .style.display = 'none';).
|
||||
'<style>\n' +
|
||||
// Chrome 35+
|
||||
':host { display: inline-block; }\n' +
|
||||
// Chrome 33 and 34 (not 35+ because of http://crbug.com/351248)
|
||||
'*:not(style):not(iframe) { display: inline-block; }\n' +
|
||||
'iframe { width: 100%; height: 100%; border: 0; }\n' +
|
||||
'</style>' +
|
||||
'<iframe allowfullscreen></iframe>';
|
||||
elem[shadowRoot].lastChild.src = getEmbeddedViewerURL(path);
|
||||
}
|
||||
}
|
||||
|
||||
updateViewerFrame();
|
||||
|
||||
// Watch for page-initiated changes of the src/data attribute.
|
||||
var srcObserver = new MutationObserver(updateViewerFrame);
|
||||
srcObserver.observe(elem, {
|
||||
attributes: true,
|
||||
childList: false,
|
||||
characterData: false,
|
||||
attributeFilter: [srcAttribute]
|
||||
});
|
||||
}
|
||||
|
||||
// Get the viewer URL, provided that the path is a valid URL.
|
||||
function getEmbeddedViewerURL(path) {
|
||||
var fragment = /^([^#]*)(#.*)?$/.exec(path);
|
||||
path = fragment[1];
|
||||
fragment = fragment[2] || '';
|
||||
|
||||
// Resolve relative path to document.
|
||||
var a = document.createElement('a');
|
||||
a.href = document.baseURI;
|
||||
a.href = path;
|
||||
path = a.href;
|
||||
return getViewerURL(path) + fragment;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue