Greasy Fork is available in English.

Tartışmalar » Geliştirme

Trouble getting clipboard content

§
Gönderildi: 28.07.2019

Trouble getting clipboard content

Hello, I'm trying to find out a functional construct to parse clipboard text and parse it to webform fields.

Syntax like this doesn't work:

var clipBoard;
navigator.clipboard.readText().then(text => clipBoard = text); // <= marked by script editor as syntax error

I'm looking for a working form, ideas? TY

wOxxOmMod
§
Gönderildi: 28.07.2019

This is not an error, but rather an ambiguous statement. Simply enclose it in parentheses like text => (clipBoard = text) or in curly braces like text => { clipBoard = text }

wOxxOmMod
§
Gönderildi: 28.07.2019

Note, however, your code is likely incorrect if you expect the subsequent statements to access clipBoard variable. That's because the Promise runs asynchronously after your enclosing context has already completed. It's like a one-time event listener for load event. It happens in the future.

The modern approach is to use async/await syntax with Promise-like things:

(async () => {
  let clipBoard = await navigator.clipboard.readText(); 
  // use the variable here
})();

Cevap paylaş

Yanıt göndermek için oturum açın.