且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Greasemonkey \JavaScript复制到剪贴板按钮

更新时间:2023-11-02 23:00:52

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).