Last active
June 11, 2017 11:10
-
-
Save aneelkkhatri/7e303d8bc1bccb33eaf5d50146e62076 to your computer and use it in GitHub Desktop.
Opener (using file extension); e.g. https://rawgit.com/aneelkkhatri/7e303d8bc1bccb33eaf5d50146e62076/raw/opener.html#https://gist.githubusercontent.com/aneelkkhatri/c95493bc5363526dc7afd0d7d25e5bc8/raw/test.html
This file contains hidden or 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
<html> | |
<head> | |
<title></title> | |
<script type="text/javascript" src="https://rawgit.com/aneelkkhatri/2e197eaddeb2285cbf958374354ba2d1/raw/XHRHelper.js"></script> | |
<script type="text/javascript"> | |
function getExtensionToMimeMap(callback) { | |
const EXTENSION_TO_MIME_TYPE = {}; | |
XHRHelper.request({ | |
url: 'https://cdn.rawgit.com/jshttp/mime-db/master/db.json', | |
success: function(xhr) { | |
var mimes = JSON.parse(xhr.responseText); | |
for (var type in mimes) { | |
var extensions = mimes[type].extensions || []; | |
for (var i = extensions.length - 1; i >= 0; i--) { | |
var extension = extensions[i]; | |
EXTENSION_TO_MIME_TYPE[extension] = type; | |
}; | |
} | |
callback(EXTENSION_TO_MIME_TYPE); | |
} | |
}); | |
} | |
</script> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
getExtensionToMimeMap(function(EXTENSION_TO_MIME_TYPE) { | |
var url = window.location.hash.substr(1); | |
var extension = url.substr(url.lastIndexOf('.')+1); | |
var mimeType = EXTENSION_TO_MIME_TYPE[extension]; | |
console.log(extension); | |
if (!mimeType) { | |
alert('Could not detect the mime type. Make sure the url file has a proper extension.'); | |
} | |
else XHRHelper.request({ | |
url: url, | |
success: function(xhr) { | |
// var link = 'data:' + mimeType + ';base64,' + btoa(xhr.responseText); | |
var blob = new Blob(Array.from(xhr.responseText), {type: mimeType}); | |
var link = URL.createObjectURL(blob); | |
document.location.href = link; | |
} | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment