How to copy text to clipboard when running inside a web worker?

0 favourites
  • 9 posts
From the Asset Store
Full game Construct 2 and Construct 3 to post on Google Play
  • Hi there,

    I'm trying to copy some text inside the clipboard using the following method:

    	navigator.clipboard.writeText('some text')
    

    The above code does not work inside a web worker since the WorkerNavigator class does not provide Clipboard API.

    I'm also not sure if I can use the old method (document.execCommand('copy')) by creating a text input...

    Any ideas how I can achieve this while still keeping the project in a web worker?

  • You should run the script in DOM side.

    I think my clipboard addon will solve this problem.

    This is a WIP project I wanted publish later with other addons.

    https://mega.nz/folder/AUkESYiZ#gxnko1ex8KG_PCDRlexbWw

    In a link you can find a example project. It works with minify and in worker mode too.

  • Thank you nyuszi008. I'm going to install your plugin and try it out. Does it provide scripting API or is it only accessible through Event Sheets?

    Btw, I can't find any mentions to how to run scripts (JS) on the DOM side in the Scripting documentation. Enabling the worker mode basically is running all my code inside a web worker (including main.js).

    Aside from plugins, is there a way to exclude a certain script file from running in the web worker?

    CC: Ashley

  • I will make javascript interface but later.

    Now you can use callFunction(name, ...params) function in runtime scripting. That call an event sheet function. It is a workaround but now it is the fastest solution.

  • When using Construct's scripting feature, currently you cannot force a script to run on the DOM side, but the easiest thing to do is just turn off worker mode so all your scripts run in the DOM. This is covered in the scripting manual here: https://www.construct.net/en/make-games/manuals/construct-3/scripting/using-scripting/javascript-construct

    The default "auto" mode should already run your code in the DOM, so did you change the setting to force worker mode on?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Ashley Yes I forced it on and realistically I want it always on to benefit from the performance improvements.

    It would be a really useful feature if we could have forced a specific script to always run on the DOM side and have it act as a bridge for all the DOM-specific APIs that are not available inside web workers.

  • If you run script in DOM side you cannot access to runtime script api.

    Anyway you need a message channel but it provided by SDK API.

    Runtime DOM side
    Project datas DOM
    Construct 3 functions Built in functions
  • nyuszi008 I'm talking about scripting inside C3 and not the plugins SDK, and inside scripting you do have access to the runtime object both inside and outside of web workers (runtime object is passed to the runOnStartup() method).

  • As far as I know you have two option when run a project: worker, and without worker.

    - Worker mode means that the runtime code (scripting codes, plugins, runtime etc.) run in a worker thread in other words what you can see (HTML page) is run as a display but the real calculation run in background.

    You can see the threads with developer tool (f12)

    - Without worker you run in one page (top) that cause if have to calculate many things or use some methods that stop running the display (HTML) cannot render in time.

    Plugin SDK and Scripting is a javascript code too. The difference between the function and usage. At the end both will build in the final project.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)