Skip to content

Instantly share code, notes, and snippets.

@seank-com
Last active October 8, 2015 08:37
Show Gist options
  • Save seank-com/3306352 to your computer and use it in GitHub Desktop.
Save seank-com/3306352 to your computer and use it in GitHub Desktop.
Save visible image tags to app data directory then update HTML with ms-appdata uris and save HTML to app data directory.
function SaveImage(strm, localfolder, filename) {
return localfolder.createFileAsync(filename, Windows.Storage.CreationCollisionOption.replaceExisting).then(function (file) {
return file.openAsync(Windows.Storage.FileAccessMode.readWrite);
}).then(function (ras) {
var os = ras.getOutputStreamAt(0),
is = strm.getInputStreamAt(0);
return Windows.Storage.Streams.RandomAccessStream.copyAndCloseAsync(is, os);
});
}
function SaveSplash() {
window.msWriteProfilerMark("start-render");
var body = document.body,
appdata = Windows.Storage.ApplicationData.current,
localfolder = appdata.localFolder,
canvas = document.createElement("canvas"),
ctx = canvas.getContext("2d"),
imgs = document.body.getElementsByTagName("img"),
size = imgs.length,
promises = [],
clientWidth = body.clientWidth,
clientHeight = body.clientHeight,
i = 0, box, img, blob, strm, dataReader, cnt = 1, filename;
for (var i = 0; i < size; i++) {
img = imgs.item(i);
try {
box = img.getBoundingClientRect();
} catch (e) {
}
if (!!box &&
box.left <= clientWidth &&
box.top <= clientHeight &&
box.left + box.width >= 0 &&
box.top + box.height >= 0) {
canvas.width = box.width;
canvas.height = box.height;
ctx.drawImage(img, 0, 0, box.width, box.height);
blob = canvas.msToBlob();
strm = blob.msDetachStream();
filename = "splash" + cnt + ".png";
cnt += 1;
img.src = "ms-appdata:///local/" + filename;
promises.push(SaveImage(strm, localfolder, filename));
} else {
img.src = "";
}
}
return WinJS.Promise.join(promises).then(function () {
window.msWriteProfilerMark("start-encode");
blob = body.innerHTML;
window.msWriteProfilerMark("start-write");
return localfolder.createFileAsync("splash.htm", Windows.Storage.CreationCollisionOption.replaceExisting);
}).then(function (file) {
return Windows.Storage.FileIO.writeTextAsync(file, blob);
});
}
// ...
app.oncheckpoint = function (args) {
window.msWriteProfilerMark("start-suspend");
args.setPromise(SaveSplash().then(function () {
window.msWriteProfilerMark("stop-suspend");
}));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment