Skip to content

Instantly share code, notes, and snippets.

@7cc
Created July 3, 2013 17:55
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 7cc/5921006 to your computer and use it in GitHub Desktop.
Save 7cc/5921006 to your computer and use it in GitHub Desktop.
for MDN
/*
make btn#selectPre
click -> select pre-content
remake - if webkit support mouseenter
*/
;(function(d){
if(!window.getSelection){
return
}
var btn = d.createElement("button")
btn.id = "selectPre"
btn.textContent = "select"
btn.addEventListener("click", selectPre, false)
function selectPre(){
window.getSelection().selectAllChildren(this.parentNode)
}
var pres = d.getElementsByTagName("pre")
for(var i=pres.length; i--;){
pres[i].addEventListener("mouseover", addBtn, false)
//pres[i].addEventListener("mouseout", remBtn, false) // if mouseleave
}
function addBtn(e){
if(this === addBtn.ele) return // not to addBtn if already
//console.log(e.target, e.currentTarget,e.relatedTarget)
this.appendChild(btn)
return addBtn.ele = this
}
/*
function remBtn(){
btn.parentNode.removeChild(btn)
}
*/
})(document)
/*
CSS
pre{
position: relative
}
#selectPre{
position: absolute;
top: 0;
right: 0;
border: none;
padding: 2px 3px;
font-family: consolas
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment