Search code examples
javascripthtmlgoogle-chrome-extension

Reach HTML code inside another HTML document


enter image description here

I'm trying to create a Chrome extension and when I click the extension's button, I want to get the HTML inside the other HTML tag (in red in the picture above). How can I achieve this?

Right now I only have implemented this:

 chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.tabs.query({ active: true, currentWindow: true }, tabs => {
       chrome.tabs.executeScript(
           tabs[0].id,
           {
              code: '' // what could I use there?
            },downloadFiles);

   });
});

Thanks.


Solution

  • Use the html selector to get the whole HTML:

    chrome.browserAction.onClicked.addListener(function(tab) {
      chrome.tabs.query({ active: true, currentWindow: true }, tabs => {
           chrome.tabs.executeScript(
               tabs[0].id, {
                 code: `var htmlElement = document.querySelectorAll("html")[1];`
               },
           downloadFiles);
       });
    });