UNPKG

claude-playwright

Version:

Seamless integration between Claude Code and Playwright MCP for efficient browser automation and testing

160 lines (159 loc) 5.24 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/core/snapshot-cache.ts var snapshot_cache_exports = {}; __export(snapshot_cache_exports, { SnapshotCache: () => SnapshotCache }); module.exports = __toCommonJS(snapshot_cache_exports); var import_crypto = __toESM(require("crypto")); var SnapshotCache = class { constructor(cacheManager) { this.currentUrl = ""; this.cacheManager = cacheManager; } setContext(page, url, profile) { this.page = page; this.currentUrl = url; this.currentProfile = profile; } async getCachedSnapshot() { if (!this.page) return null; const domHash = await this.computeDomHash(); const cacheKey = { url: this.currentUrl, domHash }; const cached = await this.cacheManager.get( cacheKey, "snapshot", this.currentProfile ); if (cached) { this.lastDomHash = domHash; return cached.snapshot; } return null; } async cacheSnapshot(snapshot) { if (!this.page) return; const domHash = await this.computeDomHash(); const viewportSize = await this.page.viewportSize(); const entry = { snapshot, url: this.currentUrl, timestamp: Date.now(), domHash, viewportSize: viewportSize || { width: 1280, height: 720 } }; const cacheKey = { url: this.currentUrl, domHash }; await this.cacheManager.set( cacheKey, entry, "snapshot", { url: this.currentUrl, profile: this.currentProfile, ttl: 18e5 // 30 minutes } ); this.lastDomHash = domHash; } async getOrCreateSnapshot() { const cached = await this.getCachedSnapshot(); if (cached) { return cached; } if (!this.page) { throw new Error("Page context not set"); } const snapshot = await this.page.accessibility.snapshot(); await this.cacheSnapshot(snapshot); return snapshot; } async computeDomHash() { if (!this.page) return ""; try { const domStructure = await this.page.evaluate(() => { const getStructure = (element, depth = 0) => { if (depth > 5) return ""; let structure = element.tagName; const id = element.id ? `#${element.id}` : ""; const classes = element.className ? `.${element.className.split(" ").join(".")}` : ""; structure += id + classes; const children = Array.from(element.children).slice(0, 10).map((child) => getStructure(child, depth + 1)).filter((s) => s).join(","); if (children) { structure += `[${children}]`; } return structure; }; return getStructure(document.body); }); return import_crypto.default.createHash("md5").update(domStructure).digest("hex"); } catch (error) { console.error("Error computing DOM hash:", error); return import_crypto.default.createHash("md5").update(this.currentUrl + Date.now()).digest("hex"); } } async hasPageChanged() { if (!this.page || !this.lastDomHash) return true; const currentHash = await this.computeDomHash(); return currentHash !== this.lastDomHash; } async invalidateIfChanged() { if (await this.hasPageChanged()) { await this.invalidateSnapshot(); return true; } return false; } async invalidateSnapshot() { await this.cacheManager.invalidate({ url: this.currentUrl, type: "snapshot", profile: this.currentProfile }); this.lastDomHash = void 0; } async prefetchRelatedPages(urls) { } async getSnapshotMetrics() { const metrics = await this.cacheManager.getMetrics(); return metrics.find((m) => m.cache_type === "snapshot"); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { SnapshotCache }); //# sourceMappingURL=snapshot-cache.cjs.map