Advertisement
fanno

MutationObserver

Jul 18th, 2025
145
0
6 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import DOMMessenger from '@/common/helpers/dom-messenger';
  2. import browser from 'webextension-polyfill';
  3.  
  4. DOMMessenger.registerMessageListener();
  5.  
  6. const containerId = DOMMessenger.containerId();
  7.  
  8. function sendPageInfo() {
  9.     const pageInfoPayload = {
  10.         domain: window.location.hostname,
  11.         url: window.location.href,
  12.     };
  13.  
  14.     void browser.runtime.sendMessage({
  15.         type: 'pageInfo',
  16.         payload: pageInfoPayload,
  17.     });
  18. }
  19.  
  20. sendPageInfo();
  21.  
  22. let updateTimerRunnig = true;
  23.  
  24. const observer = new MutationObserver((mutationsList, observer) => {
  25.     let _ignore = false;
  26.     if (!updateTimerRunnig) {
  27.         mutationsList.forEach((record) => {
  28.             if (record.type === 'childList') {
  29.                 record.addedNodes.forEach((node) => {
  30.                     if (node instanceof HTMLElement && node.id) {
  31.                         if (node.id == containerId) {
  32.                             _ignore = true;
  33.                         }
  34.                     }
  35.                 });
  36.                 record.removedNodes.forEach((node) => {
  37.                     _ignore = true;
  38.                 });
  39.             }
  40.         });
  41.  
  42.         if (!_ignore) {
  43.             updateTimerRunnig = true;
  44.             console.log('MutationObserver::setTimeout::SET');
  45.             setTimeout(() => {
  46.                 console.log('MutationObserver::setTimeout::DONE');
  47.                 sendPageInfo();
  48.                 updateTimerRunnig = false;
  49.             }, 5000);
  50.         }
  51.     }
  52. });
  53.  
  54. const config = {
  55.     childList: true, // observe direct children being added/removed
  56.     subtree: true, // observe all descendants
  57.     attributes: false, // observe attribute changes
  58.     characterData: false, // observe text content changes
  59. };
  60.  
  61. setTimeout(() => {
  62.     updateTimerRunnig = false;
  63.     observer.observe(document.body, config);
  64. }, 20000);
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement