-
-
Save greatghoul/563d8cadc8ffcc0edd6d0615596a302b to your computer and use it in GitHub Desktop.
a qrcode bookmarklet(二维码小书签)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//a qrcode bookmarklet(二维码小书签) | |
//javascript:(function(d){var b=d.createElement("textarea"),c,f=!!d.all,a,e="http://qrcode.kaywa.com/img.php?s=5&d=%s";(c=d.getElementById("_qrcode__"))?(b=c,a=b.style,a.display=""):(a=b.style,b.id="_qrcode__",d.body[f?"attachEvent":"addEventListener"]((f?"on":"")+"click",function(){a.display="none"},!0),a.zIndex=9999,a.position="fixed",a.top=0,a.left=0,a.width="100%",a.height="100%",d.body.appendChild(b));c=function(){var a,b=d.selection,c=d.activeElement,e=top.getSelection,f=c&&c.selectionEnd;f?a=c.value.substring(c.selectionStart, f):e&&(a=e()+"");b&&(a=b.createRange().text);return a=a||location.href}();b.title=c;e=e.replace("%s",encodeURIComponent(c));a.color='#fff';a.background="rgba(0, 0, 0, 0.3) url("+e+") no-repeat center";b.select()})(document); | |
//original code | |
(function(doc){ | |
var qrcode = doc.createElement('textarea'), tmp, | |
ie = !!doc.all, style, txt, | |
qrimage = 'http://qrcode.kaywa.com/img.php?s=5&d=%s', | |
id = '_qrcode__'; | |
//获取选中文字 | |
var getSelectext = function(){ | |
var text, | |
IEselection = doc.selection, | |
activeEle = doc.activeElement, | |
getSelection = top.getSelection, | |
selectionEnd = activeEle && activeEle.selectionEnd; | |
selectionEnd ? | |
(text = activeEle.value.substring(activeEle.selectionStart, selectionEnd)) : | |
getSelection && (text = getSelection() + ''); | |
IEselection && (text = IEselection.createRange().text); | |
text = text || location.href; | |
return text; | |
}; | |
if(tmp = doc.getElementById(id)){ | |
qrcode = tmp; | |
style = qrcode.style; | |
style.display = ''; | |
}else{ | |
style = qrcode.style; | |
qrcode.id = id; | |
doc.body[ie ? 'attachEvent' : 'addEventListener']((ie ? 'on' : '') + 'click', function(){ | |
style.display = 'none'; | |
}, true); | |
style.zIndex = 9999; | |
style.position = 'fixed'; | |
style.top = 0; | |
style.left = 0; | |
style.width = '100%'; | |
style.height = '100%'; | |
doc.body.appendChild(qrcode); | |
} | |
txt = getSelectext(); | |
qrcode.title = txt; | |
qrimage = qrimage.replace('%s', encodeURIComponent(txt)); | |
style.color = '#fff'; | |
style.background = 'rgba(0, 0, 0, 0.3) url(' + qrimage + ') no-repeat center'; | |
qrcode.select(); | |
})(document) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment