Copy Code to Clipboard II


#1

Hi guys,
have my copy and paste solution working fine on iOS and PC/Mac
but it doesn’t work on android at all…
Anybody has an idea on that issue?

Here is the sample project:
https://playcanvas.com/project/616061/overview/copy-text-to-clipboard

var Html = pc.createScript(‘html’);

Html.prototype.initialize = function() {

document.body.onselectstart = function(e) { if (e.target.nodeName != "INPUT" && e.target.nodeName != "TEXTAREA") { e.preventDefault(); return false; } return true; };
document.body.ondragstart = function(e) { if (e.target.nodeName != "INPUT" && e.target.nodeName != "TEXTAREA") { e.preventDefault(); return false; } return true; };
    
// ##########################################

var bottomMenuBar = document.createElement('div');
bottomMenuBar.style.visibility = 'visible';

document.body.appendChild (bottomMenuBar);
this.bottomMenuBar = bottomMenuBar;

this.copybutton = document.createElement('input');
this.copybutton.type = "button";
this.copybutton.id ="copybutton";
this.copybutton.style.fontSize = '25px';
this.copybutton.style.display = "block";
this.copybutton.style.width = "250px";
this.copybutton.style.height = "50px";
this.copybutton.style.position = 'absolute';
this.copybutton.style.left = '40%';
this.copybutton.style.top = '20%';


var copybutton = this.copybutton;
var gofor = this;

bottomMenuBar.appendChild(this.copybutton);

this.copybutton.onfocus = function(e) {
    copybutton.hasFocus = true;
};
this.copybutton.onblur = function(e) {
    copybutton.hasFocus = false;
};  
this.copybutton.addEventListener('click', function() {

var isSafari = navigator.vendor && navigator.vendor.indexOf('Apple') > -1 && navigator.userAgent && navigator.userAgent.indexOf('CriOS') == -1 && navigator.userAgent.indexOf('FxiOS') == -1;
    
if (isSafari) {

    var textArea = document.createElement ('textArea');
    textArea.value = "passwort copied";
    textArea.style.position = 'absolute';
    textArea.style.top = '-9999px';
    
    document.body.appendChild (textArea);

    range = document.createRange();
    range.selectNodeContents(textArea);
    selection = window.getSelection();
    selection.removeAllRanges();
    selection.addRange(range);
    textArea.setSelectionRange(0, 999999);
        
    var range, selection;
        
    document.execCommand ('copy');
    document.body.removeChild (textArea);
    
} else {    
   // NOT WORKING ON ANDROID  :(         https://developers.google.com/web/updates/2018/03/clipboardapi

   var ln = window.navigator.clipboard.writeText ("passwort copied").then (function() {
       alert ("WORKED");
   }, function() {
       alert ("FAILED");
   });
}
}, false);

};