-1

How can I get source https://google.com when I'm in http://example.com in chrome extension. I read: Getting the source HTML of the current page from chrome extension but it get html currentPage only. I need get html source from any url. Thanks!

Community
  • 1
  • 1
Sunary
  • 121
  • 1
  • 14

1 Answers1

1
  • Use XMLHttpRequest to download whatever the server responds with when the url is accessed. On some sites it could be a minipage with script loader that would later render the page in case it were loaded by the browser normally.

  • To get a fully rendered source or DOM tree of an arbitrary url you'll have to load it in a tab first. To make the process less distracting for the user load it in a pinned tab:

    chrome.tabs.create({url: "https://google.com", pinned: true}, function(tab) {
        .... wait for the tab to load, get the source
    });
    

    (the simplest form of waiting that doesn't require any additional permissions would be periodic checking of tab.status == "complete" invoked from the above callback, otherwise use webNavigation.onCompleted for example or inject a content script with the run-of-the-mill "DOMContentLoaded" or "load" event handlers).

  • Or load the page in an IFRAME but some sites forbid the browser to do it.

Community
  • 1
  • 1
wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • 2
    It's work. this is my function for who need it. function loadHTMLSource(urlSource){ xhttp = new XMLHttpRequest(); xhttp.open("GET", urlSource, false); xhttp.send(); return xhttp.response; }} – Sunary Aug 20 '15 at 02:23