0

I want to listen to all the requests going from a page(including from the iframes that it might have) and get metrics similar to resource timing api for all of the requests. I want to be able to do this from a background script.

I tried injecting a script tag directly which will listen to dom change events and then use that data. but see 2 problem there 1. it doesn't get all the dom change events of iframes. 2. I'm unable to locate the additional calls that are made from the iframes.

    {

      "manifest_version": 2,
      "name": "somwnamw",
      "version": "1.0",
      "author": "Me",
      "description": "Some desc",

      "icons": {
        "48": "icons/border-48.png"
      },

      "content_scripts": [
        {
            "matches": ["*://*.mydomain.com/*"],
        "js": ["trackIt.js"],
        "run_at": "document_start"
        }
      ],
      "background": {
        "scripts": ["background.js"]
      },
      "permissions": [
        "webRequest",
        "<all_urls>"
      ],
      "web_accessible_resources": ["pa.js"]

    }

My content script has something like this

            s.addEventListener('load', function(w) {

             });
            (document.body || document.documentElement).appendChild(s);

And the script I'm injecting into the dom looks like this

    var observeDOM = (function(){
        var MutationObserver = window.MutationObserver || window.WebKitMutationObserver,
            eventListenerSupported = window.addEventListener;
        return function(obj, callback){
            if( MutationObserver ){
                // define a new observer
                var obs = new MutationObserver(function(mutations, observer){
                    if( mutations[0].addedNodes.length || mutations[0].removedNodes.length )
                        callback();
                });
                // have the observer observe foo for changes in children            
                obs.observe( obj, { childList:true, subtree:true });
            }
            else if( eventListenerSupported ){
                obj.addEventListener('DOMNodeInserted', callback, false);
                obj.addEventListener('DOMNodeRemoved', callback, false);
                obj.addEventListener('DOMAttrModified', callback, false);
                obj.addEventListener('DOMElementNameChanged', callback, false);
                obj.addEventListener('DOMNodeInsertedIntoDocument', callback, false);
                obj.addEventListener('DOMNodeRemovedFromDocument', callback, false);
                obj.addEventListener('DOMSubtreeModified', callback, false);
            }
        };
    })();

    // Observe a specific DOM element:
    observeDOM( document.documentElement,function(data){
        let performanceData = performance.getEntries();
        console.log(performanceData);
        aggregateDataForFPTI(performanceData.slice(perfDataLength, performanceData.length));
        perfDataLength = performanceData.length;

    });

But my above code doesn't detect dom changes and I'm also unable to get the calls made from the iframes. I do see those requests in chrome developer tools though. here my iframes are from the same domain.

So I want to instead switch to background script and then listen to all the calls happening from the page and get the resource's timings.

Itachi
  • 61
  • 1
  • 10
  • Why do you have code which is falling back from using `MutationObserver`? Unlike code in a webpage, you know exactly which browsers your extension code will run in. All browsers in which you *can* run WebExtension based extensions have supported MutationObserver for a long time, usually longer than they've supported WebExtensions. – Makyen Oct 19 '17 at 04:25
  • Please [edit] the question to be on-topic: include a [mcve] that *duplicates the problem*. For Chrome extensions or Firefox WebExtensions you almost always need to include your *manifest.json* and some of the background, content, and/or popup scripts/HTML, and often webpage HTML/scripts. Questions seeking debugging help ("why isn't my code working the way I want?") must include: (1) the desired behavior, (2) a specific problem or error and (3) the shortest code necessary to reproduce it *in the question itself*. Please also see: [What topics can I ask about here?](/help/on-topic), and [ask]. – Makyen Oct 19 '17 at 04:27
  • You don't get DOM change events in the parent document from changes in child iframes. It's a separate document, with a separate DOM. You have to have a `MutationObserver` in that iframe. Depending on the source of the iframe document, this often requires that you inject a content script in the iframe, which may require additional `permissions`, but you haven't included your *manifest.json*, so we don't know. – Makyen Oct 19 '17 at 07:21
  • @Makyen, Is there a way Mutation Observer can listen to events on iframe? I wont have control on what iframes the application is going to have and it can inject multiple iframes into the page without page reload. so can I listen to any injection of iframes and changes inside the iframe? – Itachi Oct 19 '17 at 13:49
  • No, observer is per-document. – wOxxOm Oct 19 '17 at 14:23

0 Answers0