简体   繁体   English

bookmark.exe中的document.execCommand('cut'/'copy')被拒绝

[英]document.execCommand(‘cut’/‘copy’) was denied in bookmarklet

I am working on a bookmarklet that makes an href link of the current browser tab and copies it to the clipboard. 我正在开发一个bookmarklet,它创建当前浏览器选项卡的href链接并将其复制到剪贴板。 This bookmarklet works in Safari: 这个书签在Safari中有效:

javascript:
!function(a){
var%20b=document.createElement("textarea"),
c=document.getSelection();
b.textContent=a,document.body.appendChild(b),
c.removeAllRanges(),b.select(),
document.execCommand("copy"),
c.removeAllRanges(),
document.body.removeChild(b)}
('<a%20title="'+document.title+'"%20href="'+document.location.href+'">'+document.title+'</a>');

But in Firefox 65, I get the error "document.execCommand('cut'/'copy') was denied because it was not called from inside a short running user-generated event handler." 但是在Firefox 65中,我收到错误“document.execCommand('cut'/'copy')被拒绝,因为它没有从用户生成的短暂事件处理程序中调用。” In looking at Copying to clipboard with document.execCommand('copy') fails with big texts I'm trying to generate the html of the link before the function to solve the issue pointed out in the answer. 在查看使用document.execCommand('copy')复制到剪贴板时失败并显示大文本我试图在函数之前生成链接的html,以解决答案中指出的问题。 But, with the code below, I get a new browser tab with the text "true" and no copied link to the clipboard. 但是,使用下面的代码,我得到一个新的浏览器选项卡,文本为“true”,没有复制到剪贴板的链接。

javascript:
const text = ('<a%20title="'+document.title+'"%20href="'+document.location.href+'">'+document.title+'</a>');
!function(a){
var%20b=document.createElement("textarea"),
c=document.getSelection();
b.textContent=a,document.body.appendChild(b),
c.removeAllRanges(),
b.select(),
document.execCommand("copy"),
c.removeAllRanges(),
document.body.removeChild(b)}('text');

Is this a timing issue with the generation of the href link? 这是生成href链接的时间问题吗? Or something else? 或者是其他东西?

Your problem is not the same than in the other Q/A : In your case, you don't have any user-triggered event. 您的问题与其他Q / A不同 :在您的情况下,您没有任何用户触发的事件。

So no, it is not a timing issue, it's just that you need such an event. 所以不,这不是时间问题,只是你需要这样的事件。

To force it, you could show a splash screen, requiring that the bookmarklet's user clicks on the page. 要强制它,您可以显示启动画面,要求书签的用户点击页面。 From this click event you'd call execCommand('copy') . 从这个点击事件中你可以调用execCommand('copy')

javascript:(function(a){
  var splash = document.createElement('div'),
    msg = document.createElement('span');
  splash.style='position:fixed;top:0;left:0;width:100vw;height:100vh;display:flex;justify-content:center;align-items:center;background:#FFF;z-index:999999';
  msg.textContent = 'click me';
  splash.append(msg);
  // wait for the click event
  splash.onclick = evt => {
    var b=document.createElement("textarea"),
    c=document.getSelection();
    b.textContent=a,
    document.body.appendChild(b),
    c.removeAllRanges(),
    b.select(),
    document.execCommand("copy"),
    c.removeAllRanges(),
    document.body.removeChild(b),
    document.body.removeChild(splash);
  };
  document.body.append(splash);
})

Here is a live example of what happens (obviously not as a bookmarklet): 以下是发生的事件的实例(显然不是书签):

 (function(a){ var splash = document.createElement('div'), msg = document.createElement('span'); splash.style='position:fixed;top:0;left:0;width:100vw;height:100vh;display:flex;justify-content:center;align-items:center;background:#FFF;z-index:999999'; msg.textContent = 'click me'; splash.append(msg); // wait for the click event splash.onclick = evt => { var b=document.createElement("textarea"), c=document.getSelection(); b.textContent=a, document.body.appendChild(b), c.removeAllRanges(), b.select(), document.execCommand("copy"), c.removeAllRanges(), document.body.removeChild(b), document.body.removeChild(splash); }; document.body.append(splash); }) ('<a%20title="'+document.title+'"%20href="'+document.location.href+'">'+document.title+'</a>'); 
 <textarea>You can paste here to check what's been copied</textarea> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firefox - document.execCommand(&#39;cut&#39;/&#39;copy&#39;) 被拒绝,因为它不是从短期运行的用户生成的事件处理程序中调用的 - Firefox - document.execCommand(‘cut’/‘copy’) was denied because it was not called from inside a short running user-generated event handler 期望一个赋值或函数调用,而是看到一个表达式。 在document.execCommand(“ copy”);上; 用于书签 - Expected an assignment or function call and instead saw an expression. on document.execCommand(“copy”); for a bookmarklet 在 Google Chrome 中重新启用 document.execCommand(“cut”) - Reenabling document.execCommand(“cut”) in Google Chrome document.execCommand复制除一部分外的所有内容 - document.execCommand Copy All But One Part 在手机中使用document.execCommand(&#39;copy&#39;) - Using document.execCommand('copy') in mobile document.execCommand("copy") 有时会失败,但并非总是如此 - document.execCommand("copy") is failing sometimes and not always document.execCommand(&quot;copy&quot;) 不适用于所有浏览器 - document.execCommand("copy") not working on all browser 禁用document.execCommand(“copy”)为false - Disabling document.execCommand(“copy”) to false document.execCommand('copy') 命令在 Chrome 中不起作用 - document.execCommand('copy') Command not working in Chrome document.execCommand(&#39;copy&#39;) 不适用于 Chrome - document.execCommand('copy') not working on Chrome
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM