donobu
Version:
Create browser automations with an LLM agent and replay them as Playwright scripts.
73 lines • 3.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.InputRandomizedEmailAddressTool = void 0;
const ReplayableInteraction_1 = require("./ReplayableInteraction");
const MiscUtils_1 = require("../utils/MiscUtils");
class InputRandomizedEmailAddressTool extends ReplayableInteraction_1.ReplayableInteraction {
constructor() {
super(InputRandomizedEmailAddressTool.NAME, `Create a new randomized email address based on a given email and inputs it to a specific input text field.
For example if passed "foo@gmail.com", this tool will create "foo+SOMETHING_RANDOM@gmail.com", where
SOMETHING_RANDOM is a random string of characters safe for use in an email address, and input it in the
specified text field.`, 'SelectorBasedInputRandomizedEmailAddressToolParameters', 'AnnotationBasedInputRandomizedEmailAddressToolParameters');
}
async invoke(_context, parameters, element) {
const randomizedEmail = this.randomizeEmail(parameters.baseEmail);
// Clear any existing text first.
await this.clearField(element);
for (const char of randomizedEmail) {
await element.press(char, {
delay: MiscUtils_1.MiscUtils.generateHumanLikeKeyPressDurationInMs(char),
timeout: 3000,
});
}
if (parameters.finalizeWithSubmit) {
const char = 'Enter';
await element.press(char, {
delay: MiscUtils_1.MiscUtils.generateHumanLikeKeyPressDurationInMs(char),
timeout: 3000,
});
}
return `Inputted email: ${randomizedEmail}`;
}
randomizeEmail(baseEmail) {
const atIndex = baseEmail.indexOf('@');
if (atIndex === -1) {
throw new Error(`Invalid email address: ${baseEmail}`);
}
const localPart = baseEmail.substring(0, atIndex);
const domainPart = baseEmail.substring(atIndex);
const randomString = this.generateRandomString(InputRandomizedEmailAddressTool.RANDOM_STRING_LENGTH);
return `${localPart}+${randomString}${domainPart}`;
}
generateRandomString(length) {
const result = [];
const charactersLength = InputRandomizedEmailAddressTool.CHARACTERS.length;
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * charactersLength);
result.push(InputRandomizedEmailAddressTool.CHARACTERS[randomIndex]);
}
return result.join('');
}
async clearField(element) {
try {
const value = await element.inputValue();
if (value !== '') {
await element.selectText({ timeout: 3000 });
const char = 'Backspace';
await element.press(char, {
delay: MiscUtils_1.MiscUtils.generateHumanLikeKeyPressDurationInMs(char),
timeout: 3000,
});
}
}
catch (_e) {
// This can happen if the element is not a text element, but still accepts text inputs.
// Pass.
}
}
}
exports.InputRandomizedEmailAddressTool = InputRandomizedEmailAddressTool;
InputRandomizedEmailAddressTool.NAME = 'inputRandomizedEmailAddress';
InputRandomizedEmailAddressTool.CHARACTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
InputRandomizedEmailAddressTool.RANDOM_STRING_LENGTH = 10;
//# sourceMappingURL=InputRandomizedEmailAddressTool.js.map