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!
-
Im sorry but I dont get the question – Tom Doodler Aug 19 '15 at 14:33
-
How can I get source from other page, not currentPage like example I gave? – Sunary Aug 19 '15 at 14:46
-
You mean getting the source of a tab from another tab? – cviejo Aug 19 '15 at 15:05
1 Answers
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.
-
2It'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