@ui-tars/operator-browser
Version:
Native-browser operator for UI-TARS
96 lines (95 loc) • 2.08 kB
JavaScript
/**
* Copyright (c) 2025 Bytedance, Inc. and its affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
import os from "os";
function isMacChrome(browser) {
return 'darwin' === os.platform() && 'chrome' === browser;
}
function delay(ms) {
return new Promise((resolve)=>setTimeout(resolve, ms));
}
const ShortcutsMap = new Map([
[
'Meta+KeyA',
{
key: 'KeyA',
commands: 'SelectAll'
}
],
[
'Meta+KeyX',
{
key: 'KeyX',
commands: 'Cut'
}
],
[
'Meta+KeyC',
{
key: 'KeyC',
commands: 'Copy'
}
],
[
'Meta+KeyV',
{
key: 'KeyV',
commands: 'Paste'
}
],
[
'Meta+KeyZ',
{
key: 'KeyZ',
commands: 'Undo'
}
],
[
'Meta+KeyY',
{
key: 'KeyY',
commands: 'Redo'
}
],
[
'Meta+Shift+KeyZ',
{
key: 'KeyZ',
commands: 'Redo'
}
],
[
'Shift+Meta+KeyZ',
{
key: 'KeyZ',
commands: 'Redo'
}
]
]);
async function shortcuts_shortcuts(page, shortcuts, browser) {
if (1 === shortcuts.length) {
await page.keyboard.down(shortcuts[0]);
await delay(100);
await page.keyboard.up(shortcuts[0]);
return;
}
if (isMacChrome(browser)) {
const matched = ShortcutsMap.get(shortcuts.join('+'));
if (matched) {
await page.keyboard.down(matched.key, {
commands: [
matched.commands
]
});
await delay(100);
await page.keyboard.up(matched.key);
return;
}
}
for (const key of shortcuts)await page.keyboard.down(key);
await delay(100);
for (const key of shortcuts.reverse())await page.keyboard.up(key);
}
export { shortcuts_shortcuts as shortcuts };
//# sourceMappingURL=shortcuts.mjs.map