Skip to content

Instantly share code, notes, and snippets.

@emphaticsunshine
Last active January 20, 2026 18:33
Show Gist options
  • Select an option

  • Save emphaticsunshine/04ee27cb9c937f3b84f167ba4171711d to your computer and use it in GitHub Desktop.

Select an option

Save emphaticsunshine/04ee27cb9c937f3b84f167ba4171711d to your computer and use it in GitHub Desktop.
class PrintRecipeWithEmailBehavior {
// Required: string id for this behavior
static id = "print-recipe-email";
// Required: function that checks if this behavior should run on the current page
static isMatch() {
// This behavior should run on recipe pages that have a Print button
return window.location.href.includes("/recipes/") &&
[...document.querySelectorAll('a')].some(a => a.textContent.trim() === 'Print');
}
// Required: init method to set up the behavior
static init() {
return {};
}
// Optional: if defined, provides a way to define a custom stopping criterion
static get isTimeoutDefined() {
return false;
}
// Required: the main behavior async iterator which should yield for post-crawl processing
async* run(ctx) {
// Step 1: Click the Print button to open modal
const printButton = [...document.querySelectorAll('a')].find(a =>
a.textContent.trim() === 'Print'
);
if (!printButton) {
console.error('Print button not found');
return;
}
printButton.click();
console.log('Clicked Print button');
// Yield to allow page state to update
await ctx.Lib.getState(ctx, "starting behavior");
// Step 2: Wait for modal to appear
await new Promise(resolve => {
const checkModal = setInterval(() => {
const modal = document.getElementById('modal_print');
if (modal && modal.offsetParent !== null) {
clearInterval(checkModal);
console.log('Modal appeared');
resolve();
}
}, 100);
// Timeout after 5 seconds
setTimeout(() => {
clearInterval(checkModal);
resolve();
}, 5000);
});
// Yield after modal appears
await ctx.Lib.getState(ctx, "modal opened");
// Step 3: Click Email radio button
const emailRadio = document.getElementById('delivery_type_email');
if (emailRadio) {
if (!emailRadio.checked) {
emailRadio.click();
console.log('Clicked Email radio button');
} else {
console.log('Email radio already selected');
}
}
// Small delay to ensure state is updated
await new Promise(resolve => setTimeout(resolve, 300));
// Step 4: Click the Print button in modal
const modalPrintButton = document.querySelector('#modal_print button[type="submit"]');
if (modalPrintButton) {
modalPrintButton.click();
console.log('Clicked Print button in modal');
} else {
console.error('Modal Print button not found');
return;
}
// Yield after submitting
await ctx.Lib.getState(ctx, "submitted form");
// Step 5: Wait for modal to close
await new Promise(resolve => {
const checkModalClosed = setInterval(() => {
const modal = document.getElementById('modal_print');
if (!modal || modal.offsetParent === null) {
clearInterval(checkModalClosed);
console.log('Modal closed');
resolve();
}
}, 100);
// Timeout after 10 seconds
setTimeout(() => {
clearInterval(checkModalClosed);
resolve();
}, 10000);
});
console.log('Print recipe workflow completed successfully!');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment