Files
Techmino/Zframework/clipboard_thread.lua
2024-11-02 05:52:36 +08:00

46 lines
1.4 KiB
Lua

local getCHN=love.thread.getChannel('CLIP_get')
local setCHN=love.thread.getChannel('CLIP_set')
local trigCHN=love.thread.getChannel('CLIP_trig')
JS=require'Zframework.js'
love.timer=require'love.timer'
while true do
if trigCHN:getCount()>0 then
trigCHN:pop()
if setCHN:getCount()>0 then
repeat setCHN:pop() until setCHN:getCount()==1
-- Set Clipboard
JS.callJS(JS.stringFunc(
[[
window.navigator.clipboard
.writeText('%s')
.then(() => console.log('Copied to clipboard'))
.catch((e) => console.warn(e));
]],
setCHN:pop()
))
end
-- Get Clipboard
JS.newPromiseRequest(
JS.stringFunc[[
window.navigator.clipboard
.readText()
.then((text) => _$_(text))
.catch((e) => {
console.warn(e);
_$_('');
});
]],
function(data)
while getCHN:getCount()>0 do getCHN:pop() end
getCHN:push(data)
end,
function(id,error) print(id, error) end,
2,
'getClipboardText'
)
end
love.timer.sleep(.0626)
end