概要

preタグで囲まれたソースコードを簡単にコピーする

ソースコード

Bookmarklet版

  • 名前
    • pre2textarea
  • アドレス(YUI Compressor で圧縮)
    (function(){window.URL=window.URL||window.webkitURL;function h(l,k,m){var j=new Blob([l],{type:k+";charset=UTF-8"}),i=document.createElement("a");i.innerHTML="Download: "+m;i.setAttribute("download",m);i.href=window.URL.createObjectURL(j);return i}function d(j){var i=j.match(/public(?:\s|final)+(?:class|interface|enum)\s+([^<{\s]+)/m);return(i?i[1]:"Unknown")+".java"}function e(i){var j=document.createElement("div");j.innerHTML=i.innerHTML.replace(/<br[ \/]*>/gi,"\n").replace(/<.*?>/gm,"");return j.childNodes[0].nodeValue.replace(/\xA0/g," ")}function c(j){var i=j.downloadLink;j.parentNode.replaceChild(j.originalPre,j);if(i!=null&&i.parentNode){window.URL.revokeObjectURL(i.href);i.parentNode.removeChild(i)}}var f=function(l){var k=function(m){switch(m.type){case"keydown":if(m.keyCode=="27"){c(this)}break;case"dblclick":c(this)}},j=this.getBoundingClientRect(),i=document.createElement("textarea");i.addEventListener("keydown",k,false);i.addEventListener("dblclick",k,false);i.style.width=j.width+"px";i.style.height=(j.height>240?240:j.height)+"px";i.originalPre=this;i.value=e(this);this.tx=i;this.parentNode.replaceChild(this.tx,this);this.tx.focus();this.tx.select();i.downloadLink=h(i.value,"text/plain",d(i.value));i.parentNode.insertBefore(i.downloadLink,i.nextSibling);i.downloadLink.click()},g=document.getElementsByTagName("pre"),b=0,a=g.length;for(;b<a;b++){g[b].addEventListener("dblclick",f,false)}}());

メモ: ローカルにドロップして保存(ファイル名付き)

function makeDraggableDownloadLink(text, mimeType, fileName) {
  var blob = new Blob([text], {type: mimeType}),
         a = document.createElement('a');
  a.appendChild(document.createTextNode("download"));
  a.setAttribute('download', fileName);
  window.URL = window.URL || window.webkitURL;
  a.href = window.URL.createObjectURL(blob);
  a.addEventListener("dragstart", function(e) {
    e.dataTransfer.setData("DownloadURL", [mimeType,fileName,this.href].join(':'));
  }, false);
  return a;
}

メモ: 直接クリップボードにコピー

//ie, chrome-extension???
document.execCommand("copy");

メモ: Clipboard API

  • Clipboard APIが使用できるChromeOpera 12.50の場合、コピー、カットすると自動的に元のpreに戻すことが可能
var listener = function(e) {
  var tx = document.createElement("textarea"),
    rect = this.getBoundingClientRect(),
    name, ret,
  closeListener = function(e) {
    switch(e.type) {
    case 'copy':
    case 'cut':
      var dataTransfer = e.clipboardData;
      dataTransfer.items.add(this.innerText, 'text/plain');
      cleanup(this);
      e.preventDefault();
      break;
    case 'keydown':
      if(e.keyCode=="27") { // ESC
        cleanup(this);
      }
      break;
    case 'dblclick':
      cleanup(this);
    }
  };
  tx.addEventListener('copy',     closeListener, false);
  tx.addEventListener('cut',      closeListener, false);
  tx.addEventListener("keydown",  closeListener, false);
  tx.addEventListener("dblclick", closeListener, false);
// ...

参考リンク

コメント