admin管理员组文章数量:1310050
Is it possible to look at a page's source code, find a certain part and replace it with something else before the page loads? I would like to acplish this using JavaScript so that I can use it in a Chrome extension. So something like this:
Find the google
<script type="text/javascript">
var URLgo = "";
</script>
Replace with yahoo
<script type="text/javascript">
var URLgo = "";
</script>
Is it possible to look at a page's source code, find a certain part and replace it with something else before the page loads? I would like to acplish this using JavaScript so that I can use it in a Chrome extension. So something like this:
Find the google.
<script type="text/javascript">
var URLgo = "http://google.";
</script>
Replace with yahoo.
<script type="text/javascript">
var URLgo = "http://yahoo.";
</script>
Share
Improve this question
edited Jan 9, 2017 at 17:29
revelt
2,4201 gold badge26 silver badges38 bronze badges
asked May 10, 2011 at 14:40
sarsarsarsar
1,5614 gold badges14 silver badges17 bronze badges
1 Answer
Reset to default 4<script type="text/javascript">
function replaceScript() {
var toReplace = 'http://google.';
var replaceWith ='http://yahoo.';
document.body.innerHTML = document.body.innerHTML.replace(toReplace, replaceWith);
}
</script>
Then initialise in the body tag to do on page load.
<body onload="replaceScript();">
Should work fine and replace all instances in html body code.
If it is in an iframe with id "external_iframe" then you would modify document.body.innerHTML to be:
window.frames['external_iframe'].document.body.innerHTML
Although I'm not convinced you can use it for an external site.
Seems to be some info here: Javascript Iframe innerHTML
本文标签: javascriptfind and replace stringStack Overflow
版权声明:本文标题:javascript - find and replace string - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741857948a2401455.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论