11 changed files with 499 additions and 26 deletions
@ -0,0 +1,3 @@
@@ -0,0 +1,3 @@
|
||||
parsererror { |
||||
display: none; |
||||
} |
@ -0,0 +1,136 @@
@@ -0,0 +1,136 @@
|
||||
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
||||
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ |
||||
/* |
||||
Copyright 2012 Mozilla Foundation |
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); |
||||
you may not use this file except in compliance with the License. |
||||
You may obtain a copy of the License at |
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software |
||||
distributed under the License is distributed on an "AS IS" BASIS, |
||||
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 */ |
||||
|
||||
'use strict'; |
||||
|
||||
var VIEWER_URL = chrome.extension.getURL('content/web/viewer.html'); |
||||
var BASE_URL = VIEWER_URL.replace(/[^\/]+$/, ''); |
||||
|
||||
function getViewerURL(pdf_url) { |
||||
return VIEWER_URL + '?file=' + encodeURIComponent(pdf_url); |
||||
} |
||||
|
||||
function showViewer(url) { |
||||
// Cancel page load and empty document.
|
||||
window.stop(); |
||||
document.body.textContent = ''; |
||||
|
||||
replaceDocumentWithViewer(url); |
||||
} |
||||
function makeLinksAbsolute(doc) { |
||||
normalize('href', 'link[href]'); |
||||
normalize('src', 'style[src],script[src]'); |
||||
|
||||
function normalize(attribute, selector) { |
||||
var nodes = doc.querySelectorAll(selector); |
||||
for (var i=0; i<nodes.length; ++i) { |
||||
var node = nodes[i]; |
||||
var newAttribute = makeAbsolute(node.getAttribute(attribute)); |
||||
node.setAttribute(attribute, newAttribute); |
||||
} |
||||
} |
||||
function makeAbsolute(url) { |
||||
if (url.indexOf('://') !== -1) return url; |
||||
return BASE_URL + url; |
||||
} |
||||
} |
||||
function replaceDocumentWithViewer(url) { |
||||
var x = new XMLHttpRequest(); |
||||
x.open('GET', VIEWER_URL); |
||||
x.responseType = 'document'; |
||||
x.onload = function() { |
||||
// Resolve all relative URLs
|
||||
makeLinksAbsolute(x.response); |
||||
|
||||
// Remove all <script> elements (added back later).
|
||||
// I assumed that no inline script tags exist.
|
||||
var scripts = [], script; |
||||
|
||||
// new Worker('chrome-extension://..../pdf.js') fails, despite having
|
||||
// the correct permissions. Fix it:
|
||||
script = document.createElement('script'); |
||||
script.onload = loadNextScript; |
||||
script.src = chrome.extension.getURL('patch-worker.js'); |
||||
scripts.push(script); |
||||
|
||||
while (x.response.scripts.length) { |
||||
script = x.response.scripts[0]; |
||||
var newScript = document.createElement('script'); |
||||
newScript.onload = loadNextScript; |
||||
newScript.src = script.src; |
||||
script.parentNode.removeChild(script); |
||||
scripts.push(newScript); |
||||
} |
||||
|
||||
// Replace document with viewer
|
||||
var docEl = document.adoptNode(x.response.documentElement); |
||||
document.replaceChild(docEl, document.documentElement); |
||||
// Force Chrome to render content
|
||||
// (without this line, the layout is broken and querySelector
|
||||
// fails to find elements, even when they appear in the doc)
|
||||
document.body.innerHTML += ''; |
||||
|
||||
// Load all scripts
|
||||
loadNextScript(); |
||||
|
||||
function loadNextScript() { |
||||
if (scripts.length > 0) |
||||
document.head.appendChild(scripts.shift()); |
||||
else |
||||
renderPDF(url); |
||||
} |
||||
}; |
||||
x.send(); |
||||
} |
||||
function renderPDF(url) { |
||||
var args = { |
||||
BASE_URL: BASE_URL, |
||||
pdf_url: url |
||||
}; |
||||
// The following technique is explained at
|
||||
// http://stackoverflow.com/a/9517879/938089
|
||||
var script = document.createElement('script'); |
||||
script.textContent = |
||||
'(function(args) {' + |
||||
' PDFJS.workerSrc = args.BASE_URL + PDFJS.workerSrc;' + |
||||
' window.DEFAULT_URL = args.pdf_url;' + |
||||
' window.IMAGE_DIR = args.BASE_URL + window.IMAGE_DIR;' + |
||||
'})(' + JSON.stringify(args) + ');'; |
||||
document.head.appendChild(script); |
||||
|
||||
// Trigger domready
|
||||
if (document.readyState === 'complete') { |
||||
var event = document.createEvent('Event'); |
||||
event.initEvent('DOMContentLoaded', true, true); |
||||
document.dispatchEvent(event); |
||||
} |
||||
} |
||||
|
||||
|
||||
// Activate the content script only once per frame (until reload)
|
||||
if (!window.hasRun) { |
||||
window.hasRun = true; |
||||
chrome.extension.onMessage.addListener(function listener(message) { |
||||
if (message && message.type === 'showPDFViewer' && |
||||
message.url === location.href) { |
||||
chrome.extension.onMessage.removeListener(listener); |
||||
showViewer(message.url); |
||||
} |
||||
}); |
||||
} |
@ -0,0 +1,135 @@
@@ -0,0 +1,135 @@
|
||||
/* |
||||
Copyright 2013 Rob Wu <gwnRob@gmail.com> |
||||
Licensed under the Apache License, Version 2.0 (the "License"); |
||||
you may not use this file except in compliance with the License. |
||||
You may obtain a copy of the License at |
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software |
||||
distributed under the License is distributed on an "AS IS" BASIS, |
||||
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. |
||||
*/ |
||||
// Target: Chrome 20+
|
||||
|
||||
// W3-compliant Worker proxy.
|
||||
// This module replaces the global Worker object.
|
||||
// When invoked, the default Worker object is called.
|
||||
// If this call fails with SECURITY_ERR, the script is fetched
|
||||
// using async XHR, and transparently proxies all calls and
|
||||
// setters/getters to the new Worker object.
|
||||
// Note: This script does not magically circumvent the Same origin policy.
|
||||
|
||||
(function() { |
||||
'use strict'; |
||||
var Worker_ = window.Worker; |
||||
var URL = window.URL || window.webkitURL; |
||||
// Create dummy worker for the following purposes:
|
||||
// 1. Don't override the global Worker object if the fallback isn't
|
||||
// going to work (future API changes?)
|
||||
// 2. Use it to trigger early validation of postMessage calls
|
||||
// Note: Blob constructor is supported since Chrome 20, but since
|
||||
// some of the used Chrome APIs are only supported as of Chrome 20,
|
||||
// I don't bother adding a BlobBuilder fallback.
|
||||
var dummyWorker = new Worker_( |
||||
URL.createObjectURL(new Blob([], {type: 'text/javascript'}))); |
||||
window.Worker = function Worker(scriptURL) { |
||||
if (arguments.length === 0) { |
||||
throw new TypeError('Not enough arguments'); |
||||
} |
||||
try { |
||||
return new Worker_(scriptURL); |
||||
} catch (e) { |
||||
if (e.code === 18/*DOMException.SECURITY_ERR*/) { |
||||
return new WorkerXHR(scriptURL); |
||||
} else { |
||||
throw e; |
||||
} |
||||
} |
||||
}; |
||||
// Bind events and replay queued messages
|
||||
function bindWorker(worker, workerURL) { |
||||
if (worker._terminated) { |
||||
return; |
||||
} |
||||
worker.Worker = new Worker_(workerURL); |
||||
worker.Worker.onerror = worker._onerror; |
||||
worker.Worker.onmessage = worker._onmessage; |
||||
var o; |
||||
while ( (o = worker._replayQueue.shift()) ) { |
||||
worker.Worker[o.method].apply(worker.Worker, o.arguments); |
||||
} |
||||
while ( (o = worker._messageQueue.shift()) ) { |
||||
worker.Worker.postMessage.apply(worker.Worker, o); |
||||
} |
||||
} |
||||
function WorkerXHR(scriptURL) { |
||||
var worker = this; |
||||
var x = new XMLHttpRequest(); |
||||
x.responseType = 'blob'; |
||||
x.onload = function() { |
||||
// http://stackoverflow.com/a/10372280/938089
|
||||
var workerURL = URL.createObjectURL(x.response); |
||||
bindWorker(worker, workerURL); |
||||
}; |
||||
x.open('GET', scriptURL); |
||||
x.send(); |
||||
worker._replayQueue = []; |
||||
worker._messageQueue = []; |
||||
} |
||||
WorkerXHR.prototype = { |
||||
constructor: Worker_, |
||||
terminate: function() { |
||||
if (!this._terminated) { |
||||
this._terminated = true; |
||||
if (this.Worker) |
||||
this.Worker.terminate(); |
||||
} |
||||
}, |
||||
postMessage: function(message, transfer) { |
||||
if (!(this instanceof WorkerXHR)) |
||||
throw new TypeError('Illegal invocation'); |
||||
if (this.Worker) { |
||||
this.Worker.postMessage.apply(this.Worker, arguments); |
||||
} else { |
||||
// Trigger validation:
|
||||
dummyWorker.postMessage(message); |
||||
// Alright, push the valid message to the queue.
|
||||
this._messageQueue.push(arguments); |
||||
} |
||||
} |
||||
}; |
||||
// Implement the EventTarget interface
|
||||
[ |
||||
'addEventListener', |
||||
'removeEventListener', |
||||
'dispatchEvent' |
||||
].forEach(function(method) { |
||||
WorkerXHR.prototype[method] = function() { |
||||
if (!(this instanceof WorkerXHR)) { |
||||
throw new TypeError('Illegal invocation'); |
||||
} |
||||
if (this.Worker) { |
||||
this.Worker[method].apply(this.Worker, arguments); |
||||
} else { |
||||
this._replayQueue.push({method: method, arguments: arguments}); |
||||
} |
||||
}; |
||||
}); |
||||
Object.defineProperties(WorkerXHR.prototype, { |
||||
onmessage: { |
||||
get: function() {return this._onmessage || null;}, |
||||
set: function(func) { |
||||
this._onmessage = typeof func === 'function' ? func : null; |
||||
} |
||||
}, |
||||
onerror: { |
||||
get: function() {return this._onerror || null;}, |
||||
set: function(func) { |
||||
this._onerror = typeof func === 'function' ? func : null; |
||||
} |
||||
} |
||||
}); |
||||
})(); |
@ -0,0 +1,69 @@
@@ -0,0 +1,69 @@
|
||||
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
||||
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ |
||||
/* |
||||
Copyright 2012 Mozilla Foundation |
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); |
||||
you may not use this file except in compliance with the License. |
||||
You may obtain a copy of the License at |
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software |
||||
distributed under the License is distributed on an "AS IS" BASIS, |
||||
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, isPdfDownloadable */ |
||||
|
||||
'use strict'; |
||||
|
||||
// The onHeadersReceived event is not generated for local resources.
|
||||
// Fortunately, local PDF files will have the .pdf extension, so there's
|
||||
// no need to detect the Content-Type
|
||||
// Unfortunately, the omnibox won't show the URL.
|
||||
// Unfortunately, this method will not work for pages in incognito mode,
|
||||
// unless "incognito":"split" is used AND http:/crbug.com/224094 is fixed.
|
||||
|
||||
// Keeping track of incognito tab IDs will become obsolete when
|
||||
// "incognito":"split" can be used.
|
||||
var incognitoTabIds = []; |
||||
chrome.windows.getAll({ populate: true }, function(windows) { |
||||
windows.forEach(function(win) { |
||||
if (win.incognito) { |
||||
win.tabs.forEach(function(tab) { |
||||
incognitoTabIds.push(tab.id); |
||||
}); |
||||
} |
||||
}); |
||||
}); |
||||
chrome.tabs.onCreated.addListener(function(tab) { |
||||
if (tab.incognito) incognitoTabIds.push(tab.id); |
||||
}); |
||||
chrome.tabs.onRemoved.addListener(function(tabId) { |
||||
var index = incognitoTabIds.indexOf(tabId); |
||||
if (index !== -1) incognitoTabIds.splice(index, 1); |
||||
}); |
||||
|
||||
chrome.webRequest.onBeforeRequest.addListener( |
||||
function(details) { |
||||
if (isPdfDownloadable(details)) // Defined in pdfHandler.js
|
||||
return; |
||||
|
||||
if (incognitoTabIds.indexOf(details.tabId) !== -1) |
||||
return; // Doesn't work in incognito mode, so don't redirect.
|
||||
|
||||
var viewerPage = 'content/web/viewer.html'; |
||||
var url = chrome.extension.getURL(viewerPage) + |
||||
'?file=' + encodeURIComponent(details.url); |
||||
return { redirectUrl: url }; |
||||
}, |
||||
{ |
||||
urls: [ |
||||
'file://*/*.pdf', |
||||
'file://*/*.PDF' |
||||
], |
||||
types: ['main_frame', 'sub_frame'] |
||||
}, |
||||
['blocking']); |
Loading…
Reference in new issue