admin管理员组

文章数量:1400128

I'm writing an extension for browser using Javascript. How do I get the whole HTML source code and re-write that?

I need to change some words inside that source and reopen that page.

I'm writing an extension for browser using Javascript. How do I get the whole HTML source code and re-write that?

I need to change some words inside that source and reopen that page.

Share Improve this question asked Jul 8, 2013 at 15:11 HurremHurrem 1832 gold badges4 silver badges15 bronze badges 6
  • 1 for any browser. Mainly for Chrome – Hurrem Commented Jul 8, 2013 at 15:14
  • You can use document.getElementsByTagName("html")[0].innerHTML to read the source and document.write for rewrite the page, if that's what you ask. However, I would remend you to use something like document.getElementById("idOfTheElement").innerHTML="new string" to change a few words in a document than rewriting the whole page (if you know the id/class of the element). – Petr R. Commented Jul 8, 2013 at 15:14
  • 1 I beg to disagree with the answers below. You should try doing an string replacement at the text node level without forcing a plete page (especially CSS) reload. This is actually very simple and I will leave a small snippet example although it is not pure JS – Alexander Commented Jul 8, 2013 at 15:23
  • Thank you very much. So, I'm gonna do this: I will get the whole source by MyString=document.getElementsByTagName("html")[0].innerHTML and then apply my finction to change it, and rewrite the code by: document.documentElement.innerHTML = changeString (Mystring); will it work? – Hurrem Commented Jul 8, 2013 at 15:24
  • @Alexander actually you're right, I need to change the text only, but I need to change the whole text, from title page till the end. I dodn't understand TEXT_NODE, what is that? – Hurrem Commented Jul 8, 2013 at 15:35
 |  Show 1 more ment

3 Answers 3

Reset to default 3

In javascript you could try something like:

var source = document.documentElement.outerHTML;

...and to get the doctype use:

var doctype = document.doctype;

source: Get the whole document's html with JavaScript/JQuery

I also found this post regarding doing the same thing in a Chrome extension if this helps:

Getting the source HTML of the current page from chrome extension

Try something like this to get the page source:

document.documentElement.innerHTML

And this to change it:

document.documentElement.innerHTML = '<body>I like pie</body>'

document.documentElement is the <html> ... </html> element on the page, and innerHTML will give is the HTML of it.

You can get the source of the whole page with:

oldSource = document.getElementsByTagName("html")[0].innerHTML;

And then replace the entire page with:

document.write(newSource);

Or, if you want to replace few elements on the page, use one of the following:

  1. If you know the id of the element:

    oldText = document.getElementById("idOfElement").innerHTML;
    document.getElementById("idOfElement").innerHTML = newText;
    
  2. If you know the class of the element:

    oldText = document.getElementsByTagName("classOfElement")[0].innerHTML;
    document.getElementsByTagName("classOfElement")[0].innerHTML = newText;
    

    (where 0 means you're looking for the first element with the specificed classname on the page)

  3. If you know the tag name of the element:

    oldText = document.getElementsByClassName("tagName")[0].innerHTML;
    document.getElementsByClassName("tagName")[0].innerHTML = newText;
    

    (where 0 means you're looking for the first element with the specificed tag name on the page)

  4. Or, you can use jQuery selectors, for example:

    oldText = $("#idOfElement").html();
    oldText = $(".classOfElement").html();
    oldText = $("tagName:nth-child(1)").html();
    
    $("#idOfElement").html(newText);
    $(".classOfElement").html(newText);
    $("tagName:nth-child(1)").html(newText);
    

    Where :nth-child(1) means you're looking for the first element with the specificed tag name on the page. You can also use this when searching for classes, if there are multiple element with the same classname.

本文标签: Getting HTML source of a page by JavaScript and overwriting itStack Overflow