Add js parameter for custom JavaScript injection

- Add js parameter to ScreenshotOptions interface (max 5000 chars)
- Execute JavaScript via page.evaluate() after delay, before CSS/hideSelectors
- 5-second timeout with JS_TIMEOUT error handling
- JS_EXECUTION_ERROR for script failures with sanitized error messages
- Support in both GET and POST endpoints with validation
- Updated OpenAPI spec for both GET and POST routes
- Added comprehensive test coverage (service + route layers)
- Updated SDK documentation (Node.js and Python) with examples

Test results: 414 tests passing (includes new JS injection tests)
This commit is contained in:
SnapAPI Developer 2026-03-05 12:07:54 +01:00
parent ba888bb580
commit 91a08bab70
6 changed files with 572 additions and 3 deletions

View file

@ -121,6 +121,37 @@ const combined = await snap.capture({
});
```
### JavaScript Injection
```typescript
// Execute custom JavaScript before capture
const interactiveScreenshot = await snap.capture({
url: 'https://example.com',
js: `
// Dismiss modal popup
document.querySelector('.modal-overlay')?.remove();
// Scroll to specific content
window.scrollTo(0, 500);
// Click button to reveal content
document.querySelector('#show-more-btn')?.click();
// Wait for animation to complete
await new Promise(resolve => setTimeout(resolve, 1000));
`,
});
// Combine with other options for complex scenarios
const complexCapture = await snap.capture({
url: 'https://example.com/app',
js: 'document.querySelector(".sidebar").style.display = "none";',
css: 'body { zoom: 0.8 }',
waitForSelector: '#content-loaded',
hideSelectors: ['.ad-banner', '.cookie-notice'],
});
```
## API Reference
### `new SnapAPI(apiKey, config?)`