Greasemonkey\JavaScript Copy to Clipboard button

前端 未结 2 1295
南方客
南方客 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).

    0 讨论(0)
  • 2020-12-16 06:53

    Are you sure your example works? It does not in my browser. But take a look at the following page: http://www.jeffothy.com/weblog/clipboard-copy/

    0 讨论(0)
提交回复
热议问题