I'm trying to write code to hook the dcument.cookie
(both write and read property) and the corresponding scripts and script URLs by using the call stack trace or error stack facility of javascript but I'm unable to get it, not sure what I'm doing wrong. Here's my piece of code. If anyone could help me, would be thankful.
PS: I'm using playwright as a automation tool.
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch({headless: false});
const context = await browser.newContext();
const page = await context.newPage();
await page.goto('https://www.microsoft.com');
const callStackPromise = await page.evaluate(async () => {
await new Promise(resolve => {
window.onload = resolve;
});
console.log('Evaluating call stack...');
const stack = new Error().stack.split('\n').slice(1).map(line => line.trim());
console.log('Call stack:', stack);
let scripts = Array.from(document.scripts).map(script => script.src);
console.log('Scripts:', scripts);
let cf = [];
for (let i = 0; i < stack.length; i++) {
let line = stack[i];
let matches = line.match(/at (.*?) \(/);
if (matches && matches[1].includes('document.cookie')) {
let scriptUrl = scripts.find(scriptUrl => line.includes(scriptUrl));
if (scriptUrl) {
cf.push({ url: scriptUrl });
}
}
}
console.log('Call stack result:', cf);
return cf;
});
const callStack = await callStackPromise;
console.log(callStack);
await browser.close();
})();