cocos复制文字到系统粘贴板

cocos creator 里面需要将固定文本复制到系统粘贴板:

测试2.4和3.4都可以使用,具体需要微调

记录一下

下面代码为creator 2.4版本:

带有一点微信平台的,安卓和ios的还没时间测试就不放上来了

//复制到系统剪切板
        webCopyString: function (str) {
            console.log('复制ing---cc.sys.platform=', cc.sys.platform);

            if (cc.sys.platform == cc.sys.WECHAT_GAME) {

                // console.log('复制---微信小游戏--cc.sys.platform=', cc.sys.platform);
                wx.setClipboardData({
                    data: str, //公众号id
                    success: function(res) {
                        // wx.getClipboardData({
                        //     success: function(res) {
                        //         console.log("复制成功:", res.data);
                        //         return true;
                        //     }
                        // });
                        console.log("success复制成功:", res.data);
                        return true;
                    }
                });

                return;
            }
        

            var input = str + '';
            const el = document.createElement('textarea');
            el.value = input;
            el.setAttribute('readonly', '');
            // el.style.contain = 'strict';
            el.style.position = 'absolute';
            el.style.left = '-9999px';
            el.style.fontSize = '12pt'; // Prevent zooming on iOS
        
            const selection = getSelection();
            var originalRange = null;
            if (selection.rangeCount > 0) {
                originalRange = selection.getRangeAt(0);
            }
            document.body.appendChild(el);
            el.select();
            el.selectionStart = 0;
            el.selectionEnd = input.length;
        
            var success = false;
            try {
                success = document.execCommand('copy');
            }
            catch (err) {

            }
        
            document.body.removeChild(el);
        
            if (originalRange) {
                selection.removeAllRanges();
                selection.addRange(originalRange);
            }

            if(success){
                console.log("复制成功");
            }
            else{
                console.log("复制失败");
            }
            return success;
        },
        

creator3.4版本测试正常,ts版本加了一个回调:



    //复制到系统剪切板
    public static webCopyString(str: string, cb?: Function) {

        var input = str + '';
        const el = document.createElement('textarea');
        el.value = input;
        el.setAttribute('readonly', '');
        // el.style.contain = 'strict';
        el.style.position = 'absolute';
        el.style.left = '-9999px';
        el.style.fontSize = '12pt'; // Prevent zooming on iOS

        const selection = getSelection()!;
        var originalRange = null;
        if (selection.rangeCount > 0) {
            originalRange = selection.getRangeAt(0);
        }
        document.body.appendChild(el);
        el.select();
        el.selectionStart = 0;
        el.selectionEnd = input.length;

        var success = false;
        try {
            success = document.execCommand('copy');
        }
        catch (err) {

        }

        document.body.removeChild(el);

        if (originalRange) {
            selection.removeAllRanges();
            selection.addRange(originalRange);
        }

        cb && cb(success);
        if (success) {
            console.log("复制成功");
        }
        else {
            console.log("复制失败");
        }
        return success;
    }

3.4调用:

        Utils.webCopyString("https://www.taptap.com/", (res: boolean) => {
            if (res) {
                console.log("友情链接已复制到系统,请通过社交软件粘贴发送");
            }
            else {
                console.log("当前平台邀请失败");
            }
        })

你可能感兴趣的:(cocos,typescript,工具,javascript,cocos,creator,html5)