Skip to content
This repository has been archived by the owner on Dec 4, 2017. It is now read-only.

Commit

Permalink
Stop using FileSaver.js to avoid having the file opened both as a tab…
Browse files Browse the repository at this point in the history
… and as a download.
  • Loading branch information
fqueze committed Jan 31, 2013
1 parent a6a3074 commit 8e333a0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
2 changes: 0 additions & 2 deletions static/sidebar.htm
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
</style>
<link rel="manifest" type="text/json" href="manifest.json"/>
<link rel="stylesheet" href="./sidebar.css"/>
<!-- FileSaver polyfill to save incoming blobs as file. Until we support FileSaver, bug 648998. -->
<script src="https://raw.github.com/eligrey/FileSaver.js/master/FileSaver.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="./media.js" type="text/javascript"></script>
<script src="./personaHandler.js" type="text/javascript"></script>
Expand Down
24 changes: 23 additions & 1 deletion static/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,32 @@ function insertChatMessage(win, from, message) {

var filename = "default.txt";

function handleFile(win, blob) {
var url = win.URL.createObjectURL(blob);

// If the file extension is known, just open the file in a new tab.
const ext = ["jpg", "jpeg", "gif", "png", "pdf", "txt"];
if (ext.indexOf(filename.split(".").pop().toLowerCase()) != -1)
win.open(url);
else {
// Otherwise offer to save the file.
// We create a fake link to use its .download property that lets
// us set the filename that will be shown to the user in the
// download dialog.
var a = win.document.createElement("a");
a.href = url;
a.download = filename;
var event = win.document.createEvent("MouseEvents");
event.initMouseEvent("click", true, false, win, 0, 0, 0, 0, 0,
false, false, false, false, 0, null);
a.dispatchEvent(event); // false if event was cancelled
}
}

function gotChat(win, evt) {
if (evt.data instanceof Blob) {
// for file transfer.
saveAs(evt.data, filename);
handleFile(win, evt.data);
} else {
// either an incoming file or chat.
try {
Expand Down

0 comments on commit 8e333a0

Please sign in to comment.