同时适用于HTTP和HTTPS环境的解决方案
@Joachim Lous提供的剪贴板API解决方案对我很有帮助。但是,这在使用http而不是https的localhost上无法工作。为了解决这个问题,我使用了一个“copyToClipboard”函数(从this SO answer改编而来),它作为一个全能的包装函数,通过使用巧妙的“视口外隐藏文本区域”技巧考虑了http环境。
易读版本:
javascript:
function copyToClipboard(textToCopy) {
if (navigator.clipboard && window.isSecureContext) {
return navigator.clipboard.writeText(textToCopy);
} else {
let textArea = document.createElement("textarea");
textArea.value = textToCopy;
textArea.style.position = "fixed";
textArea.style.left = "-999999px";
textArea.style.top = "-999999px";
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
return new Promise((res, rej) => {
document.execCommand('copy') ? res() : rej();
textArea.remove();
});
}
};
var element = document.querySelector(".myClass");
var text = element.textContent || element.value;
copyToClipboard(text);
只需将“.myClass”替换为您的选择器。
书签小程序的压缩版本:
javascript:void function(){var a=document.querySelector(".myClass"),b=a.textContent||a.value;(function(a){if(navigator.clipboard&&window.isSecureContext)return navigator.clipboard.writeText(a);else{let b=document.createElement("textarea");return b.value=a,b.style.position="fixed",b.style.left="-999999px",b.style.top="-999999px",document.body.appendChild(b),b.focus(),b.select(),new Promise((a,c)=>{document.execCommand("copy")?a():c(),b.remove()})}})(b)}();
编辑:
以下是一个解决方案,适用于那些希望从页面动态选择文本的人。
javascript:void function(){function a(a){if(navigator.clipboard&&window.isSecureContext)return navigator.clipboard.writeText(a);else{let b=document.createElement("textarea");return b.value=a,b.style.position="fixed",b.style.left="-999999px",b.style.top="-999999px",document.body.appendChild(b),b.focus(),b.select(),new Promise((a,c)=>{document.execCommand("copy")?a():c(),b.remove()})}}function b(a){c.style.pointerEvents="none";var b=document.elementFromPoint(a.clientX,a.clientY);return c.style.pointerEvents="auto",b}var c=document.createElement("div");Object.assign(c.style,{position:"fixed",top:0,left:0,width:"100vw",height:"100vh",zIndex:99999999,background:"transparent",cursor:"crosshair"}),document.body.append(c);document.addEventListener("mousemove",function(a){var d=b(a);if(d){var e=d.getBoundingClientRect();Object.assign(c.style,{background:"rgba(0, 128, 255, 0.25)",outline:"1px solid rgba(0, 128, 255, 0.5)",top:""+e.top+"px",left:""+e.left+"px",width:""+e.width+"px",height:""+e.height+"px"})}}),c.addEventListener("click",function(d){var e=b(d),f=e.textContent||e.value;f=f.replace(/\n[ \n]+\n/g,"\n").replace(/\n\n+/g,"\n\n").replace(/^\n+|\n+$/g,""),f.match("\n")||(f=f.replace(/^ +| +$/,"")),a(f),document.body.removeChild(c)})}();
只需将其复制并粘贴到书签的“URL”部分,如下所示:
;null;
中,null;
的目的是什么? - Kevin Fegan//
会有问题。我认为/* comment */
应该可以正常工作。 - Kevin Feganwindow.alert(“ sometext”);
,alert(“ sometext”);
,window.confirm(“ sometext”);
,confirm(“ sometext”);
,window.prompt(“sometext”,“defaultText”);
或prompt(“ sometext”,“defaultText”);
),则在您写入剪贴板的代码之前一定要小心。如果您不使用警报或提示,或者在写入剪贴板的代码之后使用它们,则无需担心恢复焦点,您可以忽略....focus();
的内容。 - Kevin Fegan