Clipboard API — how to deal with promises?

olive

Member
3rd Party Module Dev
Tutorial Author
Patreon Bronze
Joined
Jul 17, 2017
Messages
77
I'm experimenting with adding the newish Clipboad API to the existing GetClipboard function so it'll be available on HTML5.

This is what I have so far, in gametarget.js:
Code:
BBGame.prototype.GetClipboard=function(){
    console.log("Attempting to read clipboard...")

    if (!navigator.clipboard.readText) {
        console.log( "navigator.clipboard.readText is not supported by this browser.");
        return "";
    }

    navigator.clipboard.readText().then(
      clipText => {
          console.log("CLIP TEXT : '",clipText,"'");
          return clipText;
      }
    )
    return "promise pending...";
}

Problem is that it uses an asynchronous promise to get the data, so this will always return "promise pending..." to Cerberus and the actual clipboard data gets lost to the void, even if the permissions have been set. Does anyone know how to get async js code to talk nicely to CX?
 

Wingnut

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
1,262
Not the best example but I converted a really old example of how to response to a HTML5 requester.

I added some HTML5 code for Saving strings for good measure.

 

Attachments

  • html5-filereq.zip
    82.6 KB · Views: 43
Last edited:
Top Bottom