Chrome extension cannot inject content script into dynamically generated iframes

Viewed 131

I want to access performance.getEntries inside all iframes on a certain page (my aim is to create a deep understanding of what is going on inside banner tags). However, I cannot inject a content script inside dynamically generated iframes it seems.

Am I doing something wrong in the example below?

Or is there a possible other route of getting performance.getEntries for these dynamically generated frames?

Consider an extremely simple html like this:

<html>
<body>
<script>
    var iframe = document.createElement('iframe');
    var html = '<body><img src="http://dummyimage.com/100x200/333/fff&text=hey_2_1"><img src="http://dummyimage.com/100x200/555/fff&text=hey_2_2"></body>';
    iframe.src = 'data:text/html;charset=utf-8,' + encodeURI(html);
    document.body.appendChild(iframe);
</script>
<img src="http://dummyimage.com/100x200/333/fff&text=hey_1_1">
<img src="http://dummyimage.com/100x200/333/fff&text=hey_1_2">
</body>
</html>

And an extension like this

manifest.json

{
  "manifest_version": 2,
  "name": "Banner Spy 1",
  "description": "Check all iframes",
  "version": "1.0",
  "background": {
  "scripts": ["background.js"]
},
  "content_scripts": [
  {
    "run_at": "document_end",
    "all_frames" : true,
    "match_about_blank": true,
    "matches": ["<all_urls>"],
    "js": ["content_frame.js"]
  }
  ],
  "permissions": [
    "tabs",
    "activeTab",
    "<all_urls>"
  ]
}

content_frame.js

setTimeout(function(){
  var pe = performance.getEntries();
  console.log(pe);
}, 1000);

Running this extension will only show performance entries for the top page, not the dynamic iframe.

0 Answers
Related