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 anddocument.write
for rewrite the page, if that's what you ask. However, I would remend you to use something likedocument.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
3 Answers
Reset to default 3In 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:
If you know the
id
of the element:oldText = document.getElementById("idOfElement").innerHTML; document.getElementById("idOfElement").innerHTML = newText;
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)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)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
版权声明:本文标题:Getting HTML source of a page by JavaScript and overwriting it - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744232003a2596381.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论