概要

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

ソースコード

Bookmarklet版

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

参考リンク

コメント