forked from mozilla/pdf.js
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chrome extension: Added migration assistent
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
Showing
6 changed files
with
235 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); |