Hello there!
As the title suggests, I'm trying to get the clipboard content in an online game on itch.io.
If I try to do it directly using Javascript, it does not work because itch.io does not give you permission for that, which is required in javascript.
However, I recently read about this workaround:
https://itch.io/t/16097/tip-getting-clipboard-data?before=5#post-6421260
However I do not have any clue how to make this work and correctly implement it.
Does anyone have any clue how I can use this code from the sample, to in the end be able to set a variable to the clipboard content?
E.g.
var clipboard_content
clipboard__content=???
I'm not sure what all elements mean and how to put it together.
Any help is appreciated.
Here once more is the code from the link I shared above:
(it does not seem to paste the new lines inside the code correctly, so for more clarity you might want to check the original code in the link)
Declaring and accessing canvas/site:
declare var document; let canvas = document.getElementsByTagName("canvas")[0];
Main Part:
namespace Clipboard { // Create a hidden input. let content = document.body.appendChild(document.createElement("INPUT")); content.setAttribute("type", "text"); content.style.cssText = "position:absolute;width:1px;height:1px;left:0px;z-index:-1;visibility:none;"; // Redirect the paste event to "content". document.onpaste = (event) => { content.value = ""; content.focus(); Sup.setTimeout(4, () => { canvas.focus(); }); }; // Get the previous data in "content". export function getData(): string { return(content.value); } // Clear the data in "content". export function clear(): void { content.value = ""; } // Continually check for content until the breakpoint is reached, or time has run out. function waitFor(callback: (data) => void, breakpoint: () => Boolean): void function waitFor(callback: (data) => void, timeout: number): void function waitFor(callback: (data) => void, exit: any): void { if((content.value!="")||((typeof exit == "number") ? exit<0 : exit())) { callback(content.value); } else { Sup.setTimeout(4, () => { waitFor(callback, (typeof exit == "number") ? exit-1 : exit); }); } } // The "initialization" function to wait for input. export function onPaste(callback: (data) => void, breakpoint: Function): void export function onPaste(callback: (data) => void, timeout: number): void export function onPaste(callback: (data) => void, exit: any): void { content.value = ""; waitFor(callback, exit); } }
It also mentions that you'd need to add a breakpoint to avoid overflow when there is no content in the clipboard. The suggested code is this, to add a breakpoint:
// Breakpoint example. Clipboard.onPaste( (data) => { Sup.log(data); }, () => { return(Sup.Input.wasKeyJustPressed("ESCAPE")); } ); // Timeout example. (30 seconds) Clipboard.onPaste( (data) => { Sup.log(data); }, 30000 );