Advertisement
Savelyev_Vyacheslav

htnl поле JS

Apr 25th, 2024
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     waitForIframeAndPrintContent(function(content) {
  2.       s_widget.setFieldValue('description_html', content)  
  3.     });
  4.  
  5. function waitForIframeAndPrintContent(callback) {
  6.   var container = document.querySelector('.description_html');
  7.  
  8.   if (!container) {
  9.     console.error('Container not found');
  10.     return;
  11.   }
  12.  
  13.   // Function to check and extract iframe content
  14.   function checkAndExtractIframeContent() {
  15.     var iframe = container.querySelector('iframe');
  16.     if (iframe && iframe.contentWindow.document.body) {
  17.       try {
  18.         var content = iframe.contentWindow.document.body.innerHTML;
  19.         console.log('Iframe content:', content);
  20.         callback(content);  // Call the callback function with the content
  21.       } catch (error) {
  22.         console.error('Error accessing iframe content:', error);
  23.       }
  24.     } else {
  25.       console.error('Iframe not found');
  26.     }
  27.   }
  28.  
  29.   // Creating an observer to watch for DOM changes
  30.   var observer = new MutationObserver(function(mutations, obs) {
  31.     var iframe = container.querySelector('iframe');
  32.     if (iframe && iframe.contentWindow.document.body) {
  33.       checkAndExtractIframeContent();
  34.       obs.disconnect(); // Stop observing once the iframe is processed
  35.     }
  36.   });
  37.  
  38.   // Observer configuration
  39.   var config = { childList: true, subtree: true };
  40.  
  41.   // Start observing
  42.   observer.observe(container, config);
  43.  
  44.   // Also check once directly in case the iframe is already there
  45.   checkAndExtractIframeContent();
  46. }
  47.  
  48. // Example usage:
  49. waitForIframeAndPrintContent(function(content) {
  50.   // You can use the content here
  51.   console.log('Received iframe content:', content);
  52. });
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement