-
Notifications
You must be signed in to change notification settings - Fork 0
/
contentscript.js
214 lines (190 loc) · 7.36 KB
/
contentscript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
Copyright 2014 Mozilla Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/* globals chrome, CSS */
'use strict';
var VIEWER_URL = chrome.extension.getURL('content/web/viewer.html');
function getViewerURL(pdf_url) {
return VIEWER_URL + '?file=' + encodeURIComponent(pdf_url);
}
if (CSS.supports('animation', '0s')) {
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.
// This may change the type, src/data attributes and/or the child nodes of the
// element. This function only affects elements for the first call. Subsequent
// invocations have no effect.
function watchObjectOrEmbed(elem) {
var mimeType = elem.type;
if (mimeType && 'application/pdf' !== mimeType.toLowerCase()) {
return;
}
// <embed src> <object data>
var srcAttribute = 'src' in elem ? 'src' : 'data';
var path = elem[srcAttribute];
if (!mimeType && !/\.pdf($|[?#])/i.test(path)) {
return;
}
if (elem.tagName === 'EMBED' && elem.name === 'plugin' &&
elem.parentNode === document.body &&
elem.parentNode.childElementCount === 1 && elem.src === location.href) {
// This page is most likely Chrome's default page that embeds a PDF file.
// The fact that the extension's background page did not intercept and
// redirect this PDF request means that this PDF cannot be opened by PDF.js,
// e.g. because it is a response to a POST request (as in #6174).
// A reduced test case to test PDF response to POST requests is available at
// https://robwu.nl/pdfjs/issue6174/.
// Until #4483 is fixed, POST requests should be ignored.
return;
}
if (elem.__I_saw_this_element) {
return;
}
elem.__I_saw_this_element = true;
var tagName = elem.tagName.toUpperCase();
var updateEmbedOrObject;
if (tagName === 'EMBED') {
updateEmbedOrObject = updateEmbedElement;
} else if (tagName === 'OBJECT') {
updateEmbedOrObject = updateObjectElement;
} else {
return;
}
var lastSrc;
var isUpdating = false;
function updateViewerFrame() {
if (!isUpdating) {
isUpdating = true;
try {
if (lastSrc !== elem[srcAttribute]) {
updateEmbedOrObject(elem);
lastSrc = elem[srcAttribute];
}
} finally {
isUpdating = false;
}
}
}
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]
});
}
// Display the PDF Viewer in an <embed>.
function updateEmbedElement(elem) {
if (elem.type === 'text/html' && elem.src.lastIndexOf(VIEWER_URL, 0) === 0) {
// The viewer is already shown.
return;
}
// The <embed> tag needs to be removed and re-inserted before any src changes
// are effective.
var parentNode = elem.parentNode;
var nextSibling = elem.nextSibling;
if (parentNode) {
parentNode.removeChild(elem);
}
elem.type = 'text/html';
elem.src = getEmbeddedViewerURL(elem.src);
if (parentNode) {
parentNode.insertBefore(elem, nextSibling);
}
}
// Display the PDF Viewer in an <object>.
function updateObjectElement(elem) {
// <object> elements are terrible. Experiments (in49.0.2623.75) show that the
// following happens:
// - When fallback content is shown (e.g. because the built-in PDF Viewer is
// disabled), updating the "data" attribute has no effect. Not surprising
// considering that HTMLObjectElement::m_useFallbackContent is not reset
// once it is set to true. Source:
// WebKit/Source/core/html/HTMLObjectElement.cpp#378 (rev 749fe30d676b6c14).
// - When the built-in PDF Viewer plugin is enabled, updating the "data"
// attribute reloads the content (provided that the type was correctly set).
// - When <object type=text/html data="chrome-extension://..."> is used
// (tested with a data-URL, data:text/html,<object...>, the extension's
// origin whitelist is not set up, so the viewer can't load the PDF file.
// - The content of the <object> tag may be affected by <param> tags.
//
// To make sure that our solution works for all cases, we will insert a frame
// as fallback content and force the <object> tag to render its fallback
// content.
var iframe = elem.firstElementChild;
if (!iframe || !iframe.__inserted_by_pdfjs) {
iframe = createFullSizeIframe();
elem.textContent = '';
elem.appendChild(iframe);
iframe.__inserted_by_pdfjs = true;
}
iframe.src = getEmbeddedViewerURL(elem.data);
// Some bogus content type that is not handled by any plugin.
elem.type = 'application/not-a-pee-dee-eff-type';
// Force the <object> to reload and render its fallback content.
elem.data += '';
// Usually the browser renders plugin content in this tag, which is completely
// oblivious of styles such as padding, but we insert and render child nodes,
// so force padding to be zero to avoid undesired dimension changes.
elem.style.padding = '0';
// <object> and <embed> elements have a "display:inline" style by default.
// Despite this property, when a plugin is loaded in the tag, the tag is
// treated like "display:inline-block". However, when the browser does not
// render plugin content, the <object> tag does not behave like that, and as
// a result the width and height is ignored.
// Force "display:inline-block" to make sure that the width/height as set by
// web pages is respected.
// (<embed> behaves as expected with the default display value, but setting it
// to display:inline-block doesn't hurt).
elem.style.display = 'inline-block';
}
// Create an <iframe> element without borders that takes the full width and
// height.
function createFullSizeIframe() {
var iframe = document.createElement('iframe');
iframe.style.background = 'none';
iframe.style.border = 'none';
iframe.style.borderRadius = 'none';
iframe.style.boxShadow = 'none';
iframe.style.cssFloat = 'none';
iframe.style.display = 'block';
iframe.style.height = '100%';
iframe.style.margin = '0';
iframe.style.maxHeight = 'none';
iframe.style.maxWidth = 'none';
iframe.style.position = 'static';
iframe.style.transform = 'none';
iframe.style.visibility = 'visible';
iframe.style.width = '100%';
return iframe;
}
// 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;
}