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
Add a ment  | 

1 Answer 1

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