大家好我在地址栏上使用以下代码,并通过输出数据正常工作。
javascript: alert(document.getElementsByTagName("html")[0].innerHTML );
我如何更改代码以便将输出数据复制到剪贴板,以便我可以将其粘贴到某个地方?我试过以下但没有奏效!提前谢谢。
没有将数据复制到剪贴板:
javascript: alert(document.getElementsByTagName("html")[0].innerHTML ,document.execCommand("copy"));
答案 0 :(得分:2)
您应该使用clipboard.js
来解决此问题,并提供跨浏览器兼容性。
在这里阅读更多
https://clipboardjs.com/
您可以使用
复制任何内容clipboard.copy("Your Content");
我在原始js代码的基础上添加了剥离版本的clipboard.js。 然后使用将其转换为JavaScript书签 https://mrcoles.com/bookmarklet/ 并且缩小了。 只需添加以下代码作为浏览器书签栏的链接,查看功能或运行示例代码。
<a href="javascript:(function()%7Bvar%20clipboard%3D%7B%7D%3Bclipboard.copy%3Dfunction()%7Bfunction%20t()%7Bn%3D!1%2Co%3Dnull%2Cr%26%26window.getSelection().removeAllRanges()%2Cr%3D!1%7Dfunction%20e()%7Bvar%20t%3Ddocument.getSelection()%3Bif(!document.queryCommandEnabled(%22copy%22)%26%26t.isCollapsed)%7Bvar%20e%3Ddocument.createRange()%3Be.selectNodeContents(document.body)%2Ct.removeAllRanges()%2Ct.addRange(e)%2Cr%3D!0%7D%7Dvar%20n%3D!1%2Co%3Dnull%2Cr%3D!1%3Breturn%20document.addEventListener(%22copy%22%2Cfunction(t)%7Bif(n)%7Bfor(var%20e%20in%20o)t.clipboardData.setData(e%2Co%5Be%5D)%3Bt.preventDefault()%7D%7D)%2Cfunction(r)%7Breturn%20new%20Promise(function(i%2Ca)%7Bfunction%20c(n)%7Btry%7Bif(document.execCommand(%22copy%22))t()%2Ci()%3Belse%7Bif(n)throw%20t()%2Cnew%20Error(%22Unable%20to%20copy.%20Perhaps%20it's%20not%20available%20in%20your%20browser%3F%22)%3Be()%2Cc(!0)%7D%7Dcatch(o)%7Bt()%2Ca(o)%7D%7Dn%3D!0%2C%22string%22%3D%3Dtypeof%20r%3Fo%3D%7B%22text%2Fplain%22%3Ar%7D%3Ar%20instanceof%20Node%3Fo%3D%7B%22text%2Fhtml%22%3A(new%20XMLSerializer).serializeToString(r)%7D%3Ar%20instanceof%20Object%3Fo%3Dr%3Aa(%22Invalid%20data%20type.%20Must%20be%20string%2C%20DOM%20node%2C%20or%20an%20object%20mapping%20MIME%20types%20to%20strings.%22)%2Cc(!1)%7D)%7D%7D()%2Cclipboard.paste%3Dfunction()%7Bvar%20t%2Ce%2Cn%3D!1%3Breturn%20document.addEventListener(%22paste%22%2Cfunction(o)%7Bif(n)%7Bn%3D!1%2Co.preventDefault()%3Bvar%20r%3Dt%3Bt%3Dnull%2Cr(o.clipboardData.getData(e))%7D%7D)%2Cfunction(o)%7Breturn%20new%20Promise(function(r%2Ci)%7Bn%3D!0%2Ct%3Dr%2Ce%3Do%7C%7C%22text%2Fplain%22%3Btry%7Bdocument.execCommand(%22paste%22)%7C%7C(n%3D!1%2Ci(new%20Error(%22Unable%20to%20paste.%20Pasting%20only%20works%20in%20Internet%20Explorer%20at%20the%20moment.%22)))%7Dcatch(a)%7Bn%3D!1%2Ci(new%20Error(a))%7D%7D)%7D%7D()%2C%22undefined%22%3D%3Dtypeof%20ClipboardEvent%26%26%22undefined%22!%3Dtypeof%20window.clipboardData%26%26%22undefined%22!%3Dtypeof%20window.clipboardData.setData%26%26(!function(t)%7Bfunction%20e(t%2Ce)%7Breturn%20function()%7Bt.apply(e%2Carguments)%7D%7Dfunction%20n(t)%7Bif(%22object%22!%3Dtypeof%20this)throw%20new%20TypeError(%22Promises%20must%20be%20constructed%20via%20new%22)%3Bif(%22function%22!%3Dtypeof%20t)throw%20new%20TypeError(%22not%20a%20function%22)%3Bthis._state%3Dnull%2Cthis._value%3Dnull%2Cthis._deferreds%3D%5B%5D%2Cu(t%2Ce(r%2Cthis)%2Ce(i%2Cthis))%7Dfunction%20o(t)%7Bvar%20e%3Dthis%3Breturn%20null%3D%3D%3Dthis._state%3Fvoid%20this._deferreds.push(t)%3Avoid%20s(function()%7Bvar%20n%3De._state%3Ft.onFulfilled%3At.onRejected%3Bif(null%3D%3D%3Dn)return%20void(e._state%3Ft.resolve%3At.reject)(e._value)%3Bvar%20o%3Btry%7Bo%3Dn(e._value)%7Dcatch(r)%7Breturn%20void%20t.reject(r)%7Dt.resolve(o)%7D)%7Dfunction%20r(t)%7Btry%7Bif(t%3D%3D%3Dthis)throw%20new%20TypeError(%22A%20promise%20cannot%20be%20resolved%20with%20itself.%22)%3Bif(t%26%26(%22object%22%3D%3Dtypeof%20t%7C%7C%22function%22%3D%3Dtypeof%20t))%7Bvar%20n%3Dt.then%3Bif(%22function%22%3D%3Dtypeof%20n)return%20void%20u(e(n%2Ct)%2Ce(r%2Cthis)%2Ce(i%2Cthis))%7Dthis._state%3D!0%2Cthis._value%3Dt%2Ca.call(this)%7Dcatch(o)%7Bi.call(this%2Co)%7D%7Dfunction%20i(t)%7Bthis._state%3D!1%2Cthis._value%3Dt%2Ca.call(this)%7Dfunction%20a()%7Bfor(var%20t%3D0%2Ce%3Dthis._deferreds.length%3Be%3Et%3Bt%2B%2B)o.call(this%2Cthis._deferreds%5Bt%5D)%3Bthis._deferreds%3Dnull%7Dfunction%20c(t%2Ce%2Cn%2Co)%7Bthis.onFulfilled%3D%22function%22%3D%3Dtypeof%20t%3Ft%3Anull%2Cthis.onRejected%3D%22function%22%3D%3Dtypeof%20e%3Fe%3Anull%2Cthis.resolve%3Dn%2Cthis.reject%3Do%7Dfunction%20u(t%2Ce%2Cn)%7Bvar%20o%3D!1%3Btry%7Bt(function(t)%7Bo%7C%7C(o%3D!0%2Ce(t))%7D%2Cfunction(t)%7Bo%7C%7C(o%3D!0%2Cn(t))%7D)%7Dcatch(r)%7Bif(o)return%3Bo%3D!0%2Cn(r)%7D%7Dvar%20s%3Dn.immediateFn%7C%7C%22function%22%3D%3Dtypeof%20setImmediate%26%26setImmediate%7C%7Cfunction(t)%7BsetTimeout(t%2C1)%7D%2Cl%3DArray.isArray%7C%7Cfunction(t)%7Breturn%22%5Bobject%20Array%5D%22%3D%3D%3DObject.prototype.toString.call(t)%7D%3Bn.prototype%5B%22catch%22%5D%3Dfunction(t)%7Breturn%20this.then(null%2Ct)%7D%2Cn.prototype.then%3Dfunction(t%2Ce)%7Bvar%20r%3Dthis%3Breturn%20new%20n(function(n%2Ci)%7Bo.call(r%2Cnew%20c(t%2Ce%2Cn%2Ci))%7D)%7D%2Cn.all%3Dfunction()%7Bvar%20t%3DArray.prototype.slice.call(1%3D%3D%3Darguments.length%26%26l(arguments%5B0%5D)%3Farguments%5B0%5D%3Aarguments)%3Breturn%20new%20n(function(e%2Cn)%7Bfunction%20o(i%2Ca)%7Btry%7Bif(a%26%26(%22object%22%3D%3Dtypeof%20a%7C%7C%22function%22%3D%3Dtypeof%20a))%7Bvar%20c%3Da.then%3Bif(%22function%22%3D%3Dtypeof%20c)return%20void%20c.call(a%2Cfunction(t)%7Bo(i%2Ct)%7D%2Cn)%7Dt%5Bi%5D%3Da%2C0%3D%3D%3D--r%26%26e(t)%7Dcatch(u)%7Bn(u)%7D%7Dif(0%3D%3D%3Dt.length)return%20e(%5B%5D)%3Bfor(var%20r%3Dt.length%2Ci%3D0%3Bi%3Ct.length%3Bi%2B%2B)o(i%2Ct%5Bi%5D)%7D)%7D%2Cn.resolve%3Dfunction(t)%7Breturn%20t%26%26%22object%22%3D%3Dtypeof%20t%26%26t.constructor%3D%3D%3Dn%3Ft%3Anew%20n(function(e)%7Be(t)%7D)%7D%2Cn.reject%3Dfunction(t)%7Breturn%20new%20n(function(e%2Cn)%7Bn(t)%7D)%7D%2Cn.race%3Dfunction(t)%7Breturn%20new%20n(function(e%2Cn)%7Bfor(var%20o%3D0%2Cr%3Dt.length%3Br%3Eo%3Bo%2B%2B)t%5Bo%5D.then(e%2Cn)%7D)%7D%2C%22undefined%22!%3Dtypeof%20module%26%26module.exports%3Fmodule.exports%3Dn%3At.Promise%7C%7C(t.Promise%3Dn)%7D(this)%2Cclipboard.copy%3Dfunction(t)%7Breturn%20new%20Promise(function(e%2Cn)%7Bif(%22string%22!%3Dtypeof%20t%26%26!(%22text%2Fplain%22in%20t))throw%20new%20Error(%22You%20must%20provide%20a%20text%2Fplain%20type.%22)%3Bvar%20o%3D%22string%22%3D%3Dtypeof%20t%3Ft%3At%5B%22text%2Fplain%22%5D%2Cr%3Dwindow.clipboardData.setData(%22Text%22%2Co)%3Br%3Fe()%3An(new%20Error(%22Copying%20was%20rejected.%22))%7D)%7D%2Cclipboard.paste%3Dfunction()%7Breturn%20new%20Promise(function(t%2Ce)%7Bvar%20n%3Dwindow.clipboardData.getData(%22Text%22)%3Bn%3Ft(n)%3Ae(new%20Error(%22Pasting%20was%20rejected.%22))%7D)%7D)%3Bclipboard.copy(document.getElementsByTagName(%22html%22)%5B0%5D.innerHTML)%3Balert(document.getElementsByTagName(%22html%22)%5B0%5D.innerHTML)%7D)()">Click here</a>
&#13;
所需代码如下。
javascript:(function()%7Bvar%20clipboard%3D%7B%7D%3Bclipboard.copy%3Dfunction()%7Bfunction%20t()%7Bn%3D!1%2Co%3Dnull%2Cr%26%26window.getSelection().removeAllRanges()%2Cr%3D!1%7Dfunction%20e()%7Bvar%20t%3Ddocument.getSelection()%3Bif(!document.queryCommandEnabled(%22copy%22)%26%26t.isCollapsed)%7Bvar%20e%3Ddocument.createRange()%3Be.selectNodeContents(document.body)%2Ct.removeAllRanges()%2Ct.addRange(e)%2Cr%3D!0%7D%7Dvar%20n%3D!1%2Co%3Dnull%2Cr%3D!1%3Breturn%20document.addEventListener(%22copy%22%2Cfunction(t)%7Bif(n)%7Bfor(var%20e%20in%20o)t.clipboardData.setData(e%2Co%5Be%5D)%3Bt.preventDefault()%7D%7D)%2Cfunction(r)%7Breturn%20new%20Promise(function(i%2Ca)%7Bfunction%20c(n)%7Btry%7Bif(document.execCommand(%22copy%22))t()%2Ci()%3Belse%7Bif(n)throw%20t()%2Cnew%20Error(%22Unable%20to%20copy.%20Perhaps%20it's%20not%20available%20in%20your%20browser%3F%22)%3Be()%2Cc(!0)%7D%7Dcatch(o)%7Bt()%2Ca(o)%7D%7Dn%3D!0%2C%22string%22%3D%3Dtypeof%20r%3Fo%3D%7B%22text%2Fplain%22%3Ar%7D%3Ar%20instanceof%20Node%3Fo%3D%7B%22text%2Fhtml%22%3A(new%20XMLSerializer).serializeToString(r)%7D%3Ar%20instanceof%20Object%3Fo%3Dr%3Aa(%22Invalid%20data%20type.%20Must%20be%20string%2C%20DOM%20node%2C%20or%20an%20object%20mapping%20MIME%20types%20to%20strings.%22)%2Cc(!1)%7D)%7D%7D()%2Cclipboard.paste%3Dfunction()%7Bvar%20t%2Ce%2Cn%3D!1%3Breturn%20document.addEventListener(%22paste%22%2Cfunction(o)%7Bif(n)%7Bn%3D!1%2Co.preventDefault()%3Bvar%20r%3Dt%3Bt%3Dnull%2Cr(o.clipboardData.getData(e))%7D%7D)%2Cfunction(o)%7Breturn%20new%20Promise(function(r%2Ci)%7Bn%3D!0%2Ct%3Dr%2Ce%3Do%7C%7C%22text%2Fplain%22%3Btry%7Bdocument.execCommand(%22paste%22)%7C%7C(n%3D!1%2Ci(new%20Error(%22Unable%20to%20paste.%20Pasting%20only%20works%20in%20Internet%20Explorer%20at%20the%20moment.%22)))%7Dcatch(a)%7Bn%3D!1%2Ci(new%20Error(a))%7D%7D)%7D%7D()%2C%22undefined%22%3D%3Dtypeof%20ClipboardEvent%26%26%22undefined%22!%3Dtypeof%20window.clipboardData%26%26%22undefined%22!%3Dtypeof%20window.clipboardData.setData%26%26(!function(t)%7Bfunction%20e(t%2Ce)%7Breturn%20function()%7Bt.apply(e%2Carguments)%7D%7Dfunction%20n(t)%7Bif(%22object%22!%3Dtypeof%20this)throw%20new%20TypeError(%22Promises%20must%20be%20constructed%20via%20new%22)%3Bif(%22function%22!%3Dtypeof%20t)throw%20new%20TypeError(%22not%20a%20function%22)%3Bthis._state%3Dnull%2Cthis._value%3Dnull%2Cthis._deferreds%3D%5B%5D%2Cu(t%2Ce(r%2Cthis)%2Ce(i%2Cthis))%7Dfunction%20o(t)%7Bvar%20e%3Dthis%3Breturn%20null%3D%3D%3Dthis._state%3Fvoid%20this._deferreds.push(t)%3Avoid%20s(function()%7Bvar%20n%3De._state%3Ft.onFulfilled%3At.onRejected%3Bif(null%3D%3D%3Dn)return%20void(e._state%3Ft.resolve%3At.reject)(e._value)%3Bvar%20o%3Btry%7Bo%3Dn(e._value)%7Dcatch(r)%7Breturn%20void%20t.reject(r)%7Dt.resolve(o)%7D)%7Dfunction%20r(t)%7Btry%7Bif(t%3D%3D%3Dthis)throw%20new%20TypeError(%22A%20promise%20cannot%20be%20resolved%20with%20itself.%22)%3Bif(t%26%26(%22object%22%3D%3Dtypeof%20t%7C%7C%22function%22%3D%3Dtypeof%20t))%7Bvar%20n%3Dt.then%3Bif(%22function%22%3D%3Dtypeof%20n)return%20void%20u(e(n%2Ct)%2Ce(r%2Cthis)%2Ce(i%2Cthis))%7Dthis._state%3D!0%2Cthis._value%3Dt%2Ca.call(this)%7Dcatch(o)%7Bi.call(this%2Co)%7D%7Dfunction%20i(t)%7Bthis._state%3D!1%2Cthis._value%3Dt%2Ca.call(this)%7Dfunction%20a()%7Bfor(var%20t%3D0%2Ce%3Dthis._deferreds.length%3Be%3Et%3Bt%2B%2B)o.call(this%2Cthis._deferreds%5Bt%5D)%3Bthis._deferreds%3Dnull%7Dfunction%20c(t%2Ce%2Cn%2Co)%7Bthis.onFulfilled%3D%22function%22%3D%3Dtypeof%20t%3Ft%3Anull%2Cthis.onRejected%3D%22function%22%3D%3Dtypeof%20e%3Fe%3Anull%2Cthis.resolve%3Dn%2Cthis.reject%3Do%7Dfunction%20u(t%2Ce%2Cn)%7Bvar%20o%3D!1%3Btry%7Bt(function(t)%7Bo%7C%7C(o%3D!0%2Ce(t))%7D%2Cfunction(t)%7Bo%7C%7C(o%3D!0%2Cn(t))%7D)%7Dcatch(r)%7Bif(o)return%3Bo%3D!0%2Cn(r)%7D%7Dvar%20s%3Dn.immediateFn%7C%7C%22function%22%3D%3Dtypeof%20setImmediate%26%26setImmediate%7C%7Cfunction(t)%7BsetTimeout(t%2C1)%7D%2Cl%3DArray.isArray%7C%7Cfunction(t)%7Breturn%22%5Bobject%20Array%5D%22%3D%3D%3DObject.prototype.toString.call(t)%7D%3Bn.prototype%5B%22catch%22%5D%3Dfunction(t)%7Breturn%20this.then(null%2Ct)%7D%2Cn.prototype.then%3Dfunction(t%2Ce)%7Bvar%20r%3Dthis%3Breturn%20new%20n(function(n%2Ci)%7Bo.call(r%2Cnew%20c(t%2Ce%2Cn%2Ci))%7D)%7D%2Cn.all%3Dfunction()%7Bvar%20t%3DArray.prototype.slice.call(1%3D%3D%3Darguments.length%26%26l(arguments%5B0%5D)%3Farguments%5B0%5D%3Aarguments)%3Breturn%20new%20n(function(e%2Cn)%7Bfunction%20o(i%2Ca)%7Btry%7Bif(a%26%26(%22object%22%3D%3Dtypeof%20a%7C%7C%22function%22%3D%3Dtypeof%20a))%7Bvar%20c%3Da.then%3Bif(%22function%22%3D%3Dtypeof%20c)return%20void%20c.call(a%2Cfunction(t)%7Bo(i%2Ct)%7D%2Cn)%7Dt%5Bi%5D%3Da%2C0%3D%3D%3D--r%26%26e(t)%7Dcatch(u)%7Bn(u)%7D%7Dif(0%3D%3D%3Dt.length)return%20e(%5B%5D)%3Bfor(var%20r%3Dt.length%2Ci%3D0%3Bi%3Ct.length%3Bi%2B%2B)o(i%2Ct%5Bi%5D)%7D)%7D%2Cn.resolve%3Dfunction(t)%7Breturn%20t%26%26%22object%22%3D%3Dtypeof%20t%26%26t.constructor%3D%3D%3Dn%3Ft%3Anew%20n(function(e)%7Be(t)%7D)%7D%2Cn.reject%3Dfunction(t)%7Breturn%20new%20n(function(e%2Cn)%7Bn(t)%7D)%7D%2Cn.race%3Dfunction(t)%7Breturn%20new%20n(function(e%2Cn)%7Bfor(var%20o%3D0%2Cr%3Dt.length%3Br%3Eo%3Bo%2B%2B)t%5Bo%5D.then(e%2Cn)%7D)%7D%2C%22undefined%22!%3Dtypeof%20module%26%26module.exports%3Fmodule.exports%3Dn%3At.Promise%7C%7C(t.Promise%3Dn)%7D(this)%2Cclipboard.copy%3Dfunction(t)%7Breturn%20new%20Promise(function(e%2Cn)%7Bif(%22string%22!%3Dtypeof%20t%26%26!(%22text%2Fplain%22in%20t))throw%20new%20Error(%22You%20must%20provide%20a%20text%2Fplain%20type.%22)%3Bvar%20o%3D%22string%22%3D%3Dtypeof%20t%3Ft%3At%5B%22text%2Fplain%22%5D%2Cr%3Dwindow.clipboardData.setData(%22Text%22%2Co)%3Br%3Fe()%3An(new%20Error(%22Copying%20was%20rejected.%22))%7D)%7D%2Cclipboard.paste%3Dfunction()%7Breturn%20new%20Promise(function(t%2Ce)%7Bvar%20n%3Dwindow.clipboardData.getData(%22Text%22)%3Bn%3Ft(n)%3Ae(new%20Error(%22Pasting%20was%20rejected.%22))%7D)%7D)%3Bclipboard.copy(document.getElementsByTagName(%22html%22)%5B0%5D.innerHTML)%3Balert(document.getElementsByTagName(%22html%22)%5B0%5D.innerHTML)%7D)()
答案 1 :(得分:0)
javascript: var temp = document.createElement("textarea");
document.body.appendChild(temp);
temp.value = document.getElementsByTagName("html")[0].innerHTML;
temp.select();
document.execCommand('copy');
temp.remove()
您正在寻找的代码是您在执行&#34; copy&#34;之前选择文本的地方。但遗憾的是,即使这样也不适合您,因为所有document.execCommand(&#39; copy&#39;)调用都必须作为用户操作的直接结果发生,例如:点击事件处理程序这是一种措施,可以防止用户剪贴板在他们不期望的情况下弄乱。根据我对你的帖子的理解,你正在寻找一个小脚本,你可以粘贴在地址栏中并复制内容。 查看this了解详情。