1

The Question is in continuation to the link given below.

Getting the source HTML of the current page from chrome extension

The top answer given in the above URL gets the source code of the present tab. But this does not get the source code inside the iframes of the source code.

How do I access the source code the entire page INCLUDING FRAMES in chrome extensions?

Community
  • 1
  • 1
Arun Sethupat
  • 109
  • 1
  • 11
  • Welcome to Stack Overflow. We’d love to help you. To improve your chances of getting an answer, here are some tips: [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) – Paul Roub Oct 17 '15 at 11:06

1 Answers1

3

Use allFrames: true parameter of chrome.tabs.executeScript to inject the code into all frames. The callback function will receive an array with the results for all frames:

popup.js:

chrome.tabs.executeScript({
    allFrames: true,
    code: 'JSON.stringify({ \
                url: location.href, \
                html: document.documentElement.innerHTML \
           })'
}, function(results) {
    if (chrome.runtime.lastError || !results || !results.length) {
        console.log("Some error occurred", chrome.runtime.lastError);
    } else {
        results.forEach(function(str, index) {
            var info = JSON.parse(str);
            console.log("Frame #%d | %s | %s", 
                index, info.url, info.html.substr(0, 200) + "...");
        });
    }
});
wOxxOm
  • 65,848
  • 11
  • 132
  • 136