Greasemonkey\JavaScript Copy to Clipboard button

前端 未结 2 1298
南方客
南方客 2020-12-16 06:09

I am trying to write a JavaScript script to add to greasemonkey that adds a button after an element. The onClick for this button should copy the parents element text to the

2条回答
  •  不思量自难忘°
    2020-12-16 06:44

    If you took the time to read the full article, the author states this doesn't work for Firefox...
    Actually, I think it doesn't even work for IE, as it does nothing related to the clipboard!

    There is a technique using Flash, because by default, Firefox inhibits clipboard access for security reasons.
    Otherwise, the classical way to do copy is:

    var tc = textToCopy.replace(/\n\n/g, '\n');
    if (window.clipboardData) // IE
    {
      window.clipboardData.setData("Text", tc);
    }
    else
    {
      unsafeWindow.netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
      const clipboardHelper = Components.classes
          ["@mozilla.org/widget/clipboardhelper;1"].
          getService(Components.interfaces.nsIClipboardHelper);
      clipboardHelper.copyString(tc);
    }
    

    after enabling copy (for a given site).

提交回复
热议问题