clipboardData Emulator

emulate clipboardData in non-IE browser

As of 2014-05-16. See the latest version.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name       clipboardData Emulator
// @namespace  com.ayanamist.clipboardData
// @version    0.4
// @description  emulate clipboardData in non-IE browser
// @include http*
// @run-at         document-start
// @grant unsafeWindow
// ==/UserScript==

(function () {
    'use strict';
    if (unsafeWindow.window.clipboardData) {
        return;
    }
    var textareaId = 'clipboardEmu';
    unsafeWindow.window.clipboardData = {
        clearData: function () {
            return true;
        },
        getData: function () {
            return '';
        },
        setData: function (format, data) {
            // Prevent multiple textarea instance.
            var clipboard = document.getElementById(textareaId);
            if (!clipboard) {
                clipboard = document.createElement("textarea");
                clipboard.style.float = 'left';
                clipboard.style.position = 'fixed';
                clipboard.style.left = 0;
                clipboard.style.top = 0;
                clipboard.style.width = '400px';
                clipboard.style.height = '300px';
                clipboard.style.zIndex = 9999;
                clipboard.id = textareaId;
                clipboard.readOnly = true;
                document.getElementsByTagName('body')[0].appendChild(clipboard);
            }
            clipboard.textContent = data;
            clipboard.focus();
            clipboard.select();
            var closeClipboard = function () {
                clipboard.parentNode.removeChild(clipboard);
                clipboard = null;
            };
            clipboard.onkeydown = function (evt) {
                if(evt.which == 27){ // Esc
                    closeClipboard();
    			}
            };
            clipboard.oncut = clipboard.oncopy = function () {
                setTimeout(closeClipboard, 0);
                return true;
            };
            return true;
        },
        files: [],
    };
})();