Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 49 additions & 4 deletions skills/reverse-engineering-saas/scripts/page/mask.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
// mask.js: blurs personal data before a screenshot of a production screen.
// Paste the whole file into the browser's JavaScript tool. It masks at once.
// window.__re.mask.apply({ selectors: ['.customer-name'], names: ['Maria Silva'], avatars: true, placeholders: true })
// window.__re.mask.clear()
// window.__re.mask.watch({ names: ['Maria Silva'] }) also mask later changes
// window.__re.mask.stop() stop watching, keep existing masks
// window.__re.mask.clear() stop watching and remove masks
// Selectors and placeholders: true become CSS rules that also blur elements
// rendered later (popovers, dropdowns, re-rendered lists) until clear().
// Patterns, names and avatars mark the elements on the page now: run apply()
// again right before each screenshot.
// again right before each screenshot, or opt into watch(). Watching covers this
// document's DOM mutations and input/change events, not frames, shadow roots or
// programmatic field.value changes without an event. Review each screenshot.
// Playwright browser_evaluate: wrap as () => { <file>; return window.__re.last; }
(() => {
const re = (window.__re = window.__re || {});
Expand All @@ -24,6 +28,13 @@
};
// Rules kept across apply() calls, and across pastes of this file before a reload.
const rules = re.mask?.rules || { selectors: [], placeholders: false };
re.mask?.stop?.(); // Re-pasting must not leave an old observer running.
let observer = null;
let watchedOptions = null;
const observe = () => observer?.observe(document.body, {
subtree: true, childList: true, characterData: true, attributes: true,
attributeFilter: ['class', 'style', 'alt', 'src', 'width', 'height', 'placeholder', 'value'],
});

const escapeRegExp = (text) => text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');

Expand All @@ -47,7 +58,8 @@
const lines = [`.${CLASS}{${BLUR}}`, `.${PLACEHOLDER_CLASS}::placeholder{${BLUR_TEXT}}`];
for (const selector of rules.selectors) lines.push(`${selector}{${BLUR}}`);
if (rules.placeholders) lines.push(`input::placeholder,textarea::placeholder{${BLUR_TEXT}}`);
style.textContent = lines.join('\n');
const css = lines.join('\n');
if (style.textContent !== css) style.textContent = css;
}

function apply(options = {}) {
Expand Down Expand Up @@ -103,7 +115,40 @@
return result;
}

function refresh(event) {
if (!observer || (event?.type === 'load' && event.target?.tagName !== 'IMG')) return;
// A single synchronous scan per mutation batch. Do not observe our own marks.
observer.disconnect();
try {
re.last = { ...apply(watchedOptions), watching: true };
} finally {
observe();
}
}

function stop() {
observer?.disconnect();
observer = null;
watchedOptions = null;
for (const event of ['input', 'change', 'load']) document.removeEventListener(event, refresh, true);
return { watching: false };
}

function watch(options = {}) {
stop();
// Copy arrays so later caller changes cannot silently change the watch rules.
const snapshot = { ...options, names: [...(options.names || [])], selectors: [...(options.selectors || [])] };
const result = apply(snapshot);
watchedOptions = snapshot;
observer = new MutationObserver(refresh);
observe();
for (const event of ['input', 'change', 'load']) document.addEventListener(event, refresh, true);
re.last = { ...result, watching: true };
return re.last;
}

function clear() {
stop();
for (const element of document.querySelectorAll(`.${CLASS}, .${PLACEHOLDER_CLASS}`)) element.classList.remove(CLASS, PLACEHOLDER_CLASS);
document.getElementById(STYLE_ID)?.remove();
rules.selectors = [];
Expand All @@ -112,6 +157,6 @@
return re.last;
}

re.mask = { apply, clear, rules };
re.mask = { apply, watch, stop, clear, rules };
return apply();
})();
87 changes: 87 additions & 0 deletions tests/page-scripts.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,93 @@ test('mask rules cover elements rendered later, and placeholders', async () => {
await page.evaluate(() => document.querySelectorAll('#owner, #later, .popover').forEach((element) => element.remove()));
});

test('mask watch covers new text, names, avatars and field edits and stops cleanly', async () => {
await page.evaluate(script('mask.js'));
try {
const result = await page.evaluate(() => {
window.__re.mask.clear();
return window.__re.mask.watch({ names: ['Private Watch Name'] });
});
assert.equal(result.watching, true);
assert.ok(!JSON.stringify(result).includes('Private Watch Name'));
await page.evaluate(() => {
const host = document.createElement('section');
host.id = 'watch-fixture';
host.innerHTML = '<p id="watch-email">initial</p><p id="watch-name">Private Watch Name</p><input id="watch-field"><img id="watch-avatar" class="avatar" width="32" height="32">';
document.body.append(host);
document.getElementById('watch-email').firstChild.nodeValue = 'synthetic@example.test';
});
await page.waitForFunction(() => ['watch-email', 'watch-name', 'watch-avatar'].every((id) => document.getElementById(id).classList.contains('__re-masked')));
await page.evaluate(() => {
const field = document.getElementById('watch-field');
field.value = 'synthetic@example.test';
field.dispatchEvent(new Event('input', { bubbles: true }));
field.setAttribute('placeholder', 'synthetic@example.test');
});
await page.waitForFunction(() => document.getElementById('watch-field').classList.contains('__re-masked-placeholder'));
assert.equal(await page.locator('#watch-field').evaluate((field) => getComputedStyle(field).filter), 'blur(6px)');
const output = await page.evaluate(() => window.__re.last);
for (const secret of ['Private Watch Name', 'synthetic@example.test']) assert.ok(!JSON.stringify(output).includes(secret));
await page.evaluate(() => {
window.__re.mask.clear();
window.__re.mask.watch({ avatars: false });
});
assert.equal(await page.locator('#watch-avatar').evaluate((element) => element.classList.contains('__re-masked')), false);
await page.evaluate(() => {
window.__re.mask.clear();
const late = document.createElement('p');
late.id = 'watch-late';
late.textContent = 'later@example.test';
document.getElementById('watch-fixture').append(late);
});
// A browser task boundary lets any incorrectly retained observer run.
await page.evaluate(() => new Promise((resolve) => setTimeout(resolve, 0)));
assert.equal(await page.locator('#watch-late').evaluate((element) => element.classList.contains('__re-masked')), false);
} finally {
await page.evaluate(() => {
window.__re.mask.clear();
document.getElementById('watch-fixture')?.remove();
});
}
});

test('mask watch replacement, stop and reinstallation leave no stale observer', async () => {
await page.evaluate(script('mask.js'));
try {
await page.evaluate(() => {
window.__re.mask.clear();
window.__re.mask.watch({ names: ['Old private name'] });
window.__re.mask.watch({ names: ['New private name'] });
const host = document.createElement('section');
host.id = 'watch-lifecycle';
host.innerHTML = '<p id="old-name">Old private name</p><p id="new-name">New private name</p>';
document.body.append(host);
});
await page.waitForFunction(() => document.getElementById('new-name').classList.contains('__re-masked'));
assert.equal(await page.locator('#old-name').evaluate((element) => element.classList.contains('__re-masked')), false);
const stopped = await page.evaluate(() => window.__re.mask.stop());
assert.deepEqual(stopped, { watching: false });
assert.equal(await page.locator('#new-name').evaluate((element) => element.classList.contains('__re-masked')), true);
await page.evaluate(() => {
document.getElementById('old-name').textContent = 'New private name';
});
await page.evaluate(() => new Promise((resolve) => setTimeout(resolve, 0)));
assert.equal(await page.locator('#old-name').evaluate((element) => element.classList.contains('__re-masked')), false);
await page.evaluate(() => window.__re.mask.watch({ names: ['Reinstalled private name'] }));
await page.evaluate(script('mask.js'));
await page.evaluate(() => {
document.getElementById('old-name').textContent = 'Reinstalled private name';
});
await page.evaluate(() => new Promise((resolve) => setTimeout(resolve, 0)));
assert.equal(await page.locator('#old-name').evaluate((element) => element.classList.contains('__re-masked')), false);
} finally {
await page.evaluate(() => {
window.__re.mask.clear();
document.getElementById('watch-lifecycle')?.remove();
});
}
});

test('annotate outlines regions and reports missing selectors', async () => {
await page.evaluate(script('annotate.js'));
const result = await page.evaluate(() => window.__re.annotate(['header', 'main', '.missing']));
Expand Down