3

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.

6
  • 1
    for any browser. Mainly for Chrome 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 recommend 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). 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 complete page (especially CSS) reload. This is actually very simple and I will leave a small snippet example although it is not pure JS 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? 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? Commented Jul 8, 2013 at 15:35

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

2

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.

3 Comments

what if I want to start from the head? the whole page
@hurremdev Unfortunately that is not possible with JavaScript.
@hurremdev Yes, the title is in the head element. The html element contains the head.
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.