63 lines
2.1 KiB
JavaScript
63 lines
2.1 KiB
JavaScript
/**
|
|
* Node-side ghost cursor helpers.
|
|
* Injects the browser bundle and forwards mouse action events to the page overlay.
|
|
*/
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
let ghostCursorCode = null;
|
|
function getGhostCursorCode() {
|
|
if (ghostCursorCode) {
|
|
return ghostCursorCode;
|
|
}
|
|
const currentDir = path.dirname(fileURLToPath(import.meta.url));
|
|
const bundlePath = path.join(currentDir, '..', 'dist', 'ghost-cursor-client.js');
|
|
ghostCursorCode = fs.readFileSync(bundlePath, 'utf-8');
|
|
return ghostCursorCode;
|
|
}
|
|
async function ensureGhostCursorInjected(options) {
|
|
const { page } = options;
|
|
const hasGhostCursor = await page.evaluate(() => {
|
|
return Boolean(globalThis.__playwriterGhostCursor);
|
|
});
|
|
if (hasGhostCursor) {
|
|
return;
|
|
}
|
|
const code = getGhostCursorCode();
|
|
await page.evaluate(code);
|
|
}
|
|
export async function enableGhostCursor(options) {
|
|
const { page, cursorOptions } = options;
|
|
await ensureGhostCursorInjected({ page });
|
|
await page.evaluate(({ optionsFromNode }) => {
|
|
const api = globalThis.__playwriterGhostCursor;
|
|
api?.enable(optionsFromNode);
|
|
}, { optionsFromNode: cursorOptions });
|
|
}
|
|
export async function disableGhostCursor(options) {
|
|
const { page } = options;
|
|
await page.evaluate(() => {
|
|
const api = globalThis.__playwriterGhostCursor;
|
|
api?.disable();
|
|
});
|
|
}
|
|
export async function applyGhostCursorMouseAction(options) {
|
|
const { page, event } = options;
|
|
const applied = await page.evaluate(({ serializedEvent }) => {
|
|
const api = globalThis.__playwriterGhostCursor;
|
|
if (!api) {
|
|
return false;
|
|
}
|
|
api.applyMouseAction(serializedEvent);
|
|
return true;
|
|
}, { serializedEvent: event });
|
|
if (applied) {
|
|
return;
|
|
}
|
|
await ensureGhostCursorInjected({ page });
|
|
await page.evaluate(({ serializedEvent }) => {
|
|
const api = globalThis.__playwriterGhostCursor;
|
|
api?.applyMouseAction(serializedEvent);
|
|
}, { serializedEvent: event });
|
|
}
|
|
//# sourceMappingURL=ghost-cursor.js.map
|