Skip to content

Commit

Permalink
Chrome extension: Added migration assistent
Browse files Browse the repository at this point in the history
This commit brings an update assistent to the Chrome extension:
- Title change: Suffix "(old)"
- Options page -> Show how to update
- PDF Viewer: Show option to update, or view PDF file instead.
- Background: Detects when the extension has been upgraded.
              When an upgrade has been detected, the old tabs will
              be replaced with the new viewer, and the extension will
              uninstall itself.

No other changes were made.
  • Loading branch information
Rob--W committed May 5, 2013
1 parent 1f9b28f commit 67b286c
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 13 deletions.
121 changes: 121 additions & 0 deletions extensions/chrome/background-migrate.js
@@ -0,0 +1,121 @@
// Created by Rob W <gwnRob@gmail.com>
// This file is a part of a migration tool to help users to upgrade to the
// official version of the PDF.js Chrome extension.
//
// https://chrome.google.com/webstore/detail/oemmndcbldboiebfnladdacbdfmadadm
// https://github.com/mozilla/pdf.js/issues/3042

/* globals chrome, pdf_webRequestListener */
'use strict';

// Check if other extension has already been installed by checking if a
// resource is available (publicly visible through `web_accessible_resources`)
var url = 'chrome-extension://oemmndcbldboiebfnladdacbdfmadadm/patch-worker.js';

var _failCount = 0;
function failed() {
// Clear console after 20 failed requests.
if (++_failCount % 20 === 0) {
console.clear();
}
}

function checkIfUpgraded(callback) {
var x = new XMLHttpRequest();
x.open('GET', url);
x.timeout = 1000;
x.onload = function() {
callback(true);
};
x.onerror = function() {
failed();
callback(false);
};
x.send();
}
// Synchronous request.
// Typically takes 1-2 ms for failure, and 2-3 ms for success
function checkIfUpgradedSync() {
try {
// Note: When this request fails, readyState stays at 0
// and Chrome's devtools show the request as "pending"
// even though the request has already been finished.
var x = new XMLHttpRequest();
x.timeout = 1000;
x.open('GET', url, false);
x.send();
return true;
} catch (e) {
failed();
return false;
}
}

// Replace existing old pdf viewers with original URL.
function migrateOldPDFViewers(callback) {
var totalTabs = 1;
var hasFinished = false;
var checkIfFinished = function() {
if (--totalTabs <= 0 && hasFinished === true) {
callback();
}
};
chrome.extension.getViews({type:'tab'}).forEach(function(window) {
var originalURL = window.location.search.match(/^\?file=([^=]*)/);
if (originalURL) {
++totalTabs;
window.addEventListener('unload', checkIfFinished, true);
window.location.href = decodeURIComponent(originalURL[1]);
}
});
hasFinished = true;
checkIfFinished();
}

// Should be called only once - Replaces existing PDF Viewer tabs with original
// URLs (the updated PDF viewer can view PDFs without modifying the URL),
// and uninstall the extension.
var _deactivated = false;
function deactive_extension() {
if (_deactivated) return; _deactivated = true;
chrome.webRequest.onBeforeRequest.removeListener(pdf_webRequestListener);
migrateOldPDFViewers(function() {
// Farewell
chrome.management.uninstallSelf();
});
}


// Light-weight checks if extension has been upgraded
var _poller = 0;
var _pollCount = 0;
var _pollDurationMS = 10*60*1000;
var _pollDelayMS = 5*1000;

var _bgPoller = 0;
var _bgPollDelayMS = 60*1000;

function checkIfViewerShouldBeDeactivated() {
checkIfUpgraded(function(isUpgraded) {
if (isUpgraded) {
unwatchUpdateStatus();
clearInterval(_bgPoller);
deactive_extension();
}
});
}
function watchUpdateStatus() {
_pollCount = Math.ceil(_pollDurationMS / _pollDelayMS);
if (_poller) return;
_poller = setInterval(function() {
checkIfViewerShouldBeDeactivated();
if (--_pollCount <= 0) {
unwatchUpdateStatus();
}
}, _pollDelayMS);
}
function unwatchUpdateStatus() {
clearInterval(_poller);
_poller = 0;
}
_bgPoller = setInterval(checkIfViewerShouldBeDeactivated, _bgPollDelayMS);
11 changes: 8 additions & 3 deletions extensions/chrome/manifest.json
@@ -1,8 +1,8 @@
{
"manifest_version": 2,
"name": "PDF Viewer",
"name": "PDF Viewer (old)",
"version": "PDFJSSCRIPT_VERSION",
"description": "Uses HTML5 to display PDF files directly in Chrome.",
"description": "This PDF Viewer is deprecated. Visit the Chrone Web Store to get the official version.",
"icons": {
"128": "icon128.png",
"48": "icon48.png",
Expand All @@ -19,5 +19,10 @@
],
"background": {
"page": "pdfHandler.html"
}
},
"options_page": "update-pdf-viewer.html",
"web_accessible_resources": [
"update-pdf-viewer.html",
"content/web/viewer.html"
]
}
1 change: 1 addition & 0 deletions extensions/chrome/pdfHandler.html
Expand Up @@ -14,4 +14,5 @@
See the License for the specific language governing permissions and
limitations under the License.
-->
<script src="background-migrate.js"></script>
<script src="pdfHandler.js"></script>
27 changes: 17 additions & 10 deletions extensions/chrome/pdfHandler.js
Expand Up @@ -15,24 +15,31 @@ 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 */
/* globals chrome, checkIfUpgradedSync, deactivate_extension */

'use strict';

function isPdfDownloadable(details) {
return details.url.indexOf('pdfjs.action=download') >= 0;
}

function pdf_webRequestListener(details) {
if (isPdfDownloadable(details))
return;

if (checkIfUpgradedSync()) {
// Upgraded, the new viewer will take care of the pdf file.
deactivate_extension();
return;
}

var viewerPage = 'update-pdf-viewer.html';
var url = chrome.extension.getURL(viewerPage) +
'?file=' + encodeURIComponent(details.url);
return { redirectUrl: url };
}
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
if (isPdfDownloadable(details))
return;

var viewerPage = 'content/web/viewer.html';
var url = chrome.extension.getURL(viewerPage) +
'?file=' + encodeURIComponent(details.url);
return { redirectUrl: url };
},
pdf_webRequestListener,
{
urls: [
'http://*/*.pdf',
Expand Down
61 changes: 61 additions & 0 deletions extensions/chrome/update-pdf-viewer.html
@@ -0,0 +1,61 @@
<!DOCTYPE html>
<!--
Migration assistent of PDF Viewer, created by Rob W <gwnRob@gmail.com>.
See also https://github.com/mozilla/pdf.js/issues/3042
-->
<html>
<head>
<meta charset="utf-8">
<title>Update PDF Viewer</title>
<style>
body {
margin: 3rem 1.5rem;
text-align: center;
}
#upgrade-message {
display: inline-block;
padding-left: 128px;
min-height: 128px;
line-height: 128px;
background: url("icon128.png") no-repeat;
}
#update-request {
line-height: 100%;
display: inline-block;
font-size: 1.5rem;
}
#update-request small {
font-size: 1rem;
}
#viewer-link {
display: inline-block;
max-width: 70%;
word-break: break-word;
vertical-align: top;
}
#whats-new-link {
display: inline-block;
margin-left: -1rem;
-webkit-transform: translateX(2rem);
transform: translateX(2rem);
font-size: 0.9rem;
vertical-align: middle;
}
</style>
</head>
<body>
<div id="upgrade-message">
<p id="update-request">
An official version of <strong>PDF Viewer</strong> has been published.<br>
Visit <a href="https://chrome.google.com/webstore/detail/pdf-viewer/oemmndcbldboiebfnladdacbdfmadadm" id="cws-link">PDF Viewer in the Chrome Web Store</a> to upgrade now.
<a href="https://github.com/mozilla/pdf.js/pull/3017" id="whats-new-link" title="PR: Chrome extension - highly improved">What's new?</a><br>
<small>(after adding the new version, the old version will uninstall itself)</small>
</p>
</div>

<p id="not-now">
Not now, view <a id="viewer-link">placeholder.pdf</a>
</p>
<script src="update-pdf-viewer.js"></script>
</body>
</html>
27 changes: 27 additions & 0 deletions extensions/chrome/update-pdf-viewer.js
@@ -0,0 +1,27 @@
// Migration assistent for PDF Viewer, created by Rob W <gwnRob@gmail.com>
// See also https://github.com/mozilla/pdf.js/issues/3042
/* globals chrome */
'use strict';

function getViewerURL() {
var viewerPage = 'content/web/viewer.html';
var url = chrome.extension.getURL(viewerPage) +
location.search + location.hash;
return url;
}
function getOriginalPDFUrl() {
var match = /^\?file=(.*)/.exec(location.search);
return match ? decodeURIComponent(match[1]) : 'unknown.pdf';
}
if (location.search) {
document.getElementById('viewer-link').href = getViewerURL();
document.getElementById('viewer-link').textContent = getOriginalPDFUrl();
} else {
document.getElementById('not-now').textContent = '';
}

document.getElementById('cws-link').onmousedown = function() {
chrome.extension.getBackgroundPage().watchUpdateStatus();
console.log('Started to watch update progress after clicking CWS link');
};
chrome.extension.getBackgroundPage().checkIfViewerShouldBeDeactivated();

0 comments on commit 67b286c

Please sign in to comment.