Skip to content

Instantly share code, notes, and snippets.

@BcRikko
Created July 1, 2015 04:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BcRikko/f7dc1af77447e976b6ff to your computer and use it in GitHub Desktop.
Save BcRikko/f7dc1af77447e976b6ff to your computer and use it in GitHub Desktop.
JSでファイルを出力する(Chrome Extensionsで使えないfileSystem APIの代用として)
<!doctype html>
<html lang="ja">
<head>
<meta chraset="utf-8">
<title>ファイル出力</title>
</head>
<body>
<span id="export-link"></span>
<script src="index.js"></script>
</body>
</html>
var tasks = [
{ task: 'task1', isDone: true, tags: ['project1', 'memo'] },
{ task: 'task2', isDone: false, tags: ['project1', 'memo'] },
{ task: 'task3', isDone: false, tags: ['project2', 'asap'] }
];
var data = JSON.stringify(tasks);
var a = document.createElement('a');
a.textContent = 'export'
a.download = 'tasks.json';
a.href = window.URL.createObjectURL(new Blob([data], { type: 'text/plain' }));
a.dataset.downloadurl = ['text/plain', a.download, a.href].join(':');
var exportLink = document.getElementById('export-link');
exportLink.appendChild(a);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment