I'm developing a Google Chrome extension and I'd like to know how to open a new tab (ok, this is simple:
chrome.tabs.create({'url': chrome.extension.getURL(mypage)}, function(tab) { /* ... */ });
) and retrieve the source code of that page.
I know that I can use AJAX to get the source, but the problem is that the web page contains some Javascript code that edits the page, and I need the edited page.
Is it possible?
To serialize the full, live HTML document, use the following code:
Now, in a Chrome extension, you have to add some events to the extension page such as a background page or popup page:
The above function returns the source code of the main frame in a tab. If you want to get the source of a child frame, call
chrome.tabs.executeScript
with aframeId
parameter.The next snippet shows an example of how your extension could use the function. Paste the snippet in the background page's console, or declare a browserAction, put the snippet in the
onClicked
listener and click on the extension button.The referenced
get_source.js
contains the following code:Don't forget to add the appropriate host permissions, so that you can read DOM from the page. In the above example, you have to add
"https://example.com/*"
to the "permissions" section of manifest.json.Related documentation
Node
MDNDocumentType
(document.doctype
,<!DOCTYPE ... >
) MDNchrome.tabs.create
Google Chrome Extension docschrome.tabs.executeScript
Google Chrome Extension docschrome.tabs.onUpdated
Google Chrome Extension docschrome.tabs.onRemoved
Google Chrome Extension docs