Skip to content

Instantly share code, notes, and snippets.

@st98
Created July 24, 2015 17:11
Show Gist options
  • Save st98/aab6d26f1312c1043e76 to your computer and use it in GitHub Desktop.
Save st98/aab6d26f1312c1043e76 to your computer and use it in GitHub Desktop.
Clipboard API で遊ぶよ! https://w3c.github.io/clipboard-apis/
if (!(String.prototype.startsWith)) {
String.prototype.startsWith = function (s) {
return this.slice(0, s.length) === s;
};
}
function output(url) {
var out = document.getElementById('output');
var img = new Image();
img.src = url;
img.addEventListener('load', function () {
out.appendChild(img);
}, false);
}
function processBlob(blob) {
var reader = new FileReader();
reader.addEventListener('load', function (ev) {
output(ev.target.result);
}, false);
reader.readAsDataURL(blob);
}
function handler(ev) {
var items = ev.clipboardData.items;
Array.prototype.forEach.call(items, function (item) {
if (!item.type.startsWith('image/')) {
return;
}
var blob = item.getAsFile();
processBlob(blob);
});
}
document.addEventListener('paste', handler, false);
<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8">
<style>
img, canvas {
display: block;
border: 3px dotted #aaa;
}
</style>
<title>Clipboard API で遊ぶよ!</title>
</head>
<body>
<h1>Clipboard API で遊ぶよ!</h1>
<h2>説明</h2>
<p><kbd>PrintScreen</kbd> を押した後、<kbd>Ctrl + V</kbd> とかで貼り付けてみるといいと思います。</p>
<h2>出力</h2>
<div id="output"></div>
<script src="clipboard.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment