Nadim Kobeissi
Nadim Kobeissi

Reputation: 254

Get Source of Loaded URLs via Chrome Extension?

I'm writing a Chrome extension that needs to be able to analyze the source code of a specific HTML page and all the external Javascript and CSS files it loads without loading them again via an XHR request - that is, it will be analyzing the running copies loaded by the browser.

Is that possible? I know it's possible to analyze the source of a particular open tab, but while these Javascript files will be loaded by the browser, they obviously won't be occupying their own tab or window (only the HTML loading them will be.) Please help!

Upvotes: 4

Views: 1364

Answers (1)

Andrew Moore
Andrew Moore

Reputation: 95424

Out of the box, there is no way to get the source of the resources without resorting to the chrome.experimental.devtools.resources APIs.

However, when the experimental APIs are enabled using the --enable-experimental-extension-apis switch, you can do the following to retrieve the source of each resource:

chrome.experimental.devtools.resources.onFinished.addListener(function(resource) {
  resource.getContent(function(content, encoding) {
    if(encoding !== 'base64') {
      alert(content);
    }
  });
});

Upvotes: 1

Related Questions