contentscript.js 7.6 KB

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