Skip to content

Instantly share code, notes, and snippets.

@hokein
Created February 26, 2013 08:45
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save hokein/5037045 to your computer and use it in GitHub Desktop.
Save hokein/5037045 to your computer and use it in GitHub Desktop.
This is the chrome sample app which writing the image file from chrome.tabs.captureVisibleTab call to HTML5 file system.
<!DOCTYPE html>
<html>
<head>
<script src="main.js"></script>
</head>
<body>
<p>Hello World!</p>
<button id="showButton" >Click me</button>
<div id="screenshot"></div>
</body>
</html>
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
var file_system;
function base64ToBinary(imgUrl) {
var BASE64_MARKER = ';base64,';
var base64Index = imgUrl.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
var base64 = imgUrl.substring(base64Index);
var raw = window.atob(base64);
var rawLength = raw.length;
var array = new Uint8Array(new ArrayBuffer(rawLength));
for (i = 0; i < rawLength; ++i) {
array[i] = raw.charCodeAt(i);
}
return array;
}
function readImage(file_name) {
file_system.root.getFile(file_name, {}, function(fileEntry){
a = fileEntry;
var div = document.getElementById("screenshot");
div.innerHTML = '<img src="'+fileEntry.toURL()+ '">';
}, function(){ console.log("Can not open " + file_name) });
};
function onInitFs(fs) {
file_system = fs;
chrome.tabs.captureVisibleTab({"format":"jpeg"}, function(imgUrl) {
file_system.root.getFile("capture.jpeg", {create: true}, function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = function(e) {
console.log("Write successfully")
};
fileWriter.onerror = function(e) {
console.log("Write error!")
};
content = base64ToBinary(imgUrl);
bob = new Blob([content]);
fileWriter.write(bob);
});
});
});
};
function init() {
window.requestFileSystem(window.TEMPORARY, 5*1024*1024, onInitFs);
document.getElementById("showButton").onclick = function() {readImage("capture.jpeg")};
}
document.addEventListener('DOMContentLoaded', init);
{
"manifest_version": 2,
"name": "Write Image File to HTM5 Filesystem Sample",
"version": "1",
"permissions": [
"tabs"
],
"app": {
"launch": {
"local_path": "index.html"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment