From ec956d97c9e255cdd685e7a64c4956ecb5aa37e3 Mon Sep 17 00:00:00 2001
From: Rob Wu <gwnRob@gmail.com>
Date: Wed, 17 Jul 2013 23:25:31 +0200
Subject: [PATCH] [CRX] Delay render of viewer if needed...

Sometimes, the viewer did not render for PDF files in an iframe,
because document.readyState not reaching "complete".

Deferring window.stop() until the root element is placed in the
document fixes the problem (typically a few ten milliseconds).
---
 extensions/chrome/insertviewer.js | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/extensions/chrome/insertviewer.js b/extensions/chrome/insertviewer.js
index 6d39a1c31..82e106ead 100644
--- a/extensions/chrome/insertviewer.js
+++ b/extensions/chrome/insertviewer.js
@@ -27,6 +27,12 @@ function getViewerURL(pdf_url) {
 }
 
 function showViewer(url) {
+  if (document.documentElement === null) {
+    // If the root element hasn't been rendered yet, delay the next operation.
+    // Otherwise, document.readyState will get stuck in "interactive".
+    setTimeout(showViewer, 0, url);
+    return;
+  }
   // Cancel page load and empty document.
   window.stop();
   document.body.textContent = '';