To serialize a complete, live HTML document, use the following code:
Now, in the Chrome extension, you need to add some events to the extension page, such as a help page or a pop-up page:
function getSourceFromTab(tabId, callback) {
The above function returns the source code of the main frame in the tab. If you want to get the source of the child frame, call chrome.tabs.executeScript with the frameId parameter.
The following snippet shows an example of how your extension can use this function. Paste the snippet into the background page or declare a browserAction , place the snippet in the onClicked and click the extension button.
var mypage = 'https://example.com'; var callback = function(html_string) { console.log('HTML string, from extension: ', html_string); }; chrome.tabs.create({ url: mypage }, function(tab) { getSourceFromTab(tab.id, callback); });
The get_source.js link contains the following code:
function DOMtoString(document_root) { ... see top of the answer... }
Remember to add the appropriate host permissions so you can read the DOM from the page. In the above example, you need to add "https://example.com/*" to the "Permissions" section of manifest.json.
Related Documentation
Rob W Apr 15 2018-12-12T00: 00Z
source share