From ec5ef58b843fbb143c036066bceb09f13ce8ebe1 Mon Sep 17 00:00:00 2001 From: Rob Wu Date: Wed, 17 Jul 2013 18:57:18 +0200 Subject: [PATCH] [CRX] Improved navigation detection. A user reported that the PDF Viewer is not rendered on Dropbox, (Chrome on Mac OS X). This is apparently caused by the fact that the PDF file is loaded in an iframe in such a way that the tabs.onUpdated event is not triggered. This patch switches to the webNavigation event API, which improves the reliability of the navigation detection. Unfortunately Opera 15 does not support the webNavigation API, so the old (tabs.onUpdated) method is used (feature-detection is used, so whenever Opera decides to implement this API, it will profit from it). --- extensions/chrome/manifest.json | 3 ++- extensions/chrome/pdfHandler.js | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/extensions/chrome/manifest.json b/extensions/chrome/manifest.json index 26dea5de1..e36274708 100644 --- a/extensions/chrome/manifest.json +++ b/extensions/chrome/manifest.json @@ -11,7 +11,8 @@ "permissions": [ "webRequest", "webRequestBlocking", "", - "tabs" + "tabs", + "webNavigation" ], "content_scripts": [{ "matches": [ diff --git a/extensions/chrome/pdfHandler.js b/extensions/chrome/pdfHandler.js index 28ba1a953..e4028f3dc 100644 --- a/extensions/chrome/pdfHandler.js +++ b/extensions/chrome/pdfHandler.js @@ -62,6 +62,28 @@ function insertPDFJSForTab(tabId, url) { * @param {string} url The URL of the pdf file. */ function activatePDFJSForTab(tabId, url) { + if (!chrome.webNavigation) { + // Opera... does not support the webNavigation API. + activatePDFJSForTabFallbackForOpera(tabId, url); + return; + } + var listener = function webNavigationEventListener(details) { + if (details.tabId === tabId) { + insertPDFJSForTab(tabId, url); + chrome.webNavigation.onCommitted.removeListener(listener); + } + }; + var urlFilter = { + url: [{ urlEquals: url }] + }; + chrome.webNavigation.onCommitted.addListener(listener, urlFilter); +} + +/** + * Fallback for Opera. + * @see activatePDFJSForTab + **/ +function activatePDFJSForTabFallbackForOpera(tabId, url) { chrome.tabs.onUpdated.addListener(function listener(_tabId) { if (tabId === _tabId) { insertPDFJSForTab(tabId, url);