This repository has been archived by the owner on Oct 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Currently scripts in documents are allowed, so we need to sandbox them so they can't go and read other documents. We can't put the document in a sandboxed iframe because that breaks clean pagination and reflow. Currently, meta sandbox is poorly supported, as are Service Workers (which would also be a heavyweight solution). So we open the window from a sandboxed iframe, because no-allow-same- origin trickles down to the opened window. We then position that iframe over the print button, because of popup blockers. This commit also fixes printing of non-html files.
- Loading branch information
Showing
7 changed files
with
203 additions
and
102 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
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,25 @@ | ||
<!-- | ||
* Firetext Editor | ||
* Copyright (C) Codexa Organization. | ||
--> | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
|
||
<!-- Lib --> | ||
<script type="text/javascript" src="../../scripts/lib/kamino.js"></script> | ||
<script type="text/javascript" src="../../scripts/lib/message_channel.js"></script> | ||
|
||
<!-- Message Communication Proxy --> | ||
<script type="text/javascript" src="../../scripts/messages.js"></script> | ||
|
||
<!-- Main Script --> | ||
<script type="text/javascript" src="scripts/printButton.js"></script> | ||
|
||
<!-- Styles --> | ||
<link rel="stylesheet" type="text/css" href="style/printButton.css"/> | ||
</head> | ||
<body> | ||
</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,107 @@ | ||
window.addEventListener('DOMContentLoaded', function() { | ||
// WARNING: DO NOT REPLACE, THIS STRING IS REPLACED WITH THE ORIGIN AUTOMATICALLY WHEN LOADED FROM editorProxy.js | ||
var mainOrigin = "[ORIGIN_OF_MAIN_DOCUMENT]"; | ||
|
||
// Proxy for communication with parent page | ||
var parentMessageProxy; | ||
|
||
window.addEventListener("message", function(e){ | ||
if(e.origin !== mainOrigin) { | ||
throw new Error("origin did not match"); | ||
} | ||
if(e.data.command === "init" && e.ports.length) { | ||
// register port | ||
parentMessageProxy = new MessageProxy(e.ports[0]); | ||
parentMessageProxy.getPort().start(); | ||
} | ||
}, false); | ||
|
||
window.addEventListener('click', function() { | ||
var win = window.open(URL.createObjectURL(new Blob([ | ||
"<script>", | ||
"window.addEventListener('message', function(evt) {", | ||
" document.open('text/html', 'replace');", | ||
" document.write(evt.data.content);", | ||
" document.close();", | ||
"});", | ||
"</script>", | ||
], {type: 'text/html'}))); | ||
var key = parentMessageProxy.registerMessageHandler(function(e){ | ||
win.postMessage({ | ||
content: [ | ||
'<!DOCTYPE html>', | ||
'<html moznomarginboxes>', | ||
'<head>', | ||
' <meta charset="utf-8">', | ||
' <title>' + e.data.filename.replace(/</g, '<') + e.data.filetype + '</title>', | ||
' <style>', | ||
' #firetext_print_notice {', | ||
' border: 2px solid;', | ||
' font-size: xx-large;', | ||
' margin: 20px;', | ||
' padding: 20px;', | ||
' border-radius: 8px;', | ||
' font-family: sans-serif;', | ||
' }', | ||
' @media print {', | ||
' #firetext_print_notice {', | ||
' display: none;', | ||
' }', | ||
' }', | ||
' </style>', | ||
'</head>', | ||
'<body>', | ||
'', | ||
e.data.content, | ||
"", | ||
"<script>", | ||
"var onAfterPrint = function(mql) {", | ||
" if(!mql.matches) {", | ||
" window.close();", | ||
" }", | ||
"};", | ||
"var mql = window.matchMedia('print');", | ||
"if(", | ||
" navigator.userAgent.indexOf('Chrome') !== -1 &&", | ||
" // In both Firefox and IE the window.print() dialog is less featureful than menu -> print,", | ||
" // (preview and page settings,) so we allow the user to select the latter if they want.", | ||
" // Also, they don't support the following (they have onafterprint), but they might in the future.", | ||
" navigator.userAgent.indexOf('Android') === -1", | ||
" // Chrome Android returns from printing before it's done and crashes if we window.close().", | ||
" // Also match Android tablet and WebView Android because they're probably the same.", | ||
") {", | ||
" mql.addListener(onAfterPrint);", | ||
"}", | ||
"var onAutoPrintUnsupported = function() {", | ||
" // The browser doesn't support window.print() (or window.print() is non-blocking).", | ||
" // So we ask the user that, if the browser supports printing, they print manually.", | ||
" var notice = document.createElement('div');", | ||
" notice.id = 'firetext_print_notice';", | ||
" notice.textContent = " + JSON.stringify(e.data['automatic-printing-failed']) + ";", | ||
" document.body.insertBefore(notice, document.body.firstChild);", | ||
" mql.removeListener(onAfterPrint);", | ||
" onAutoPrintUnsupported = function() {};", | ||
"};", | ||
"var t0 = Date.now();", | ||
"setTimeout(function() {", | ||
" if(Date.now() - t0 < 200) {", | ||
" onAutoPrintUnsupported();", | ||
" }", | ||
"});", | ||
"try {", | ||
" window.print();", | ||
"} catch(e) {", | ||
" onAutoPrintUnsupported();", | ||
"}", | ||
"</script>", | ||
"</body>", | ||
"</html>", | ||
].join('\n') | ||
}, '*'); | ||
}, null, true); | ||
parentMessageProxy.getPort().postMessage({ | ||
command: "print-button-pressed", | ||
key: key | ||
}); | ||
}); | ||
}); |
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,4 @@ | ||
html { | ||
height: 100%; | ||
cursor: pointer; | ||
} |
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