I wanna detect DOM changes on some loaded page (eg. new article added on local news page) and do something after detection (send email). In this example im trying to detect if the child node have been added or removed from parent node (target div node) and output something in the console after detection.
Do I need to implement expose function? And if yes, how do I do that?
Im using pupetter and MutationObserver but its not working.
This is my code:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.goto('https://www.example.com/');
await page.evaluate(() => {
const target = document.querySelector("body > div.container.upscore-pos-1 > section > div:nth-child(3) > div:nth-child(2) > div.row.hidden-xs > div > section");
const observer = new MutationObserver( mutations => {
for (const mutation of mutations) {
if (mutation.type === 'childList') {
console.log('Mutation Detected: A child node has been added or removed.');
}
}
});
observer.observe(target, { childList: true });
});
})();
Thanks in advance!
Maybe page.exposeFunction()
is the simplest way:
'use strict';
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
await page.goto('https://www.example.com/');
await page.exposeFunction('puppeteerLogMutation', () => {
console.log('Mutation Detected: A child node has been added or removed.');
});
await page.evaluate(() => {
const target = document.querySelector('body');
const observer = new MutationObserver( mutations => {
for (const mutation of mutations) {
if (mutation.type === 'childList') {
puppeteerLogMutation();
}
}
});
observer.observe(target, { childList: true });
});
await page.evaluate(() => {
document.querySelector('body').appendChild(document.createElement('br'));
});
})();
But page.waitForSelector()
(or some other waitFor...
methods) in some cases can also suffice:
'use strict';
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
await page.goto('https://www.example.com/');
await page.evaluate(() => {
setTimeout(() => {
document.querySelector('body')
.appendChild(document.createElement('br')).className = 'puppeteer-test';
}, 5000);
});
await page.waitForSelector('body br.puppeteer-test');
console.log('Mutation Detected: A child node has been added or removed.');
})();