# Replacing only in the first match of \<head\> in the response

**URL:** <https://discourse.mitmproxy.org/t/replacing-only-in-the-first-match-of-head-in-the-response/980>\
**Category:** help\
**Created:** [April 11, 2018, 4:17pm UTC](https://discourse.mitmproxy.org/t/replacing-only-in-the-first-match-of-head-in-the-response/980 "2018-04-11T16:17:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![idiotsavant](https://avatars.discourse-cdn.com/v4/letter/i/71c47a/32.png) [@idiotsavant](https://discourse.mitmproxy.org/u/idiotsavant)\
**Post date:** [April 11, 2018, 4:17pm UTC](https://discourse.mitmproxy.org/t/replacing-only-in-the-first-match-of-head-in-the-response/980/1 "2018-04-11T16:17:20Z")

</div>

I’m using a a script to inject a `<script>` tag at the end of the `<head>` section of the response, and this works fine:

```python
test_tag = b'<script src="test-fakeurl.js"></script></head>'
flow.response.content = flow.response.content.replace(b"</head>", test_tag)

```

I would like the option to do this replacement only the first time that the `</head>` match is found on the page, because otherwise the script will be injected into frames/iframes too, if they also have `<head>` sections. Initially, I tired using the optional `count` parameter for `str.replace`:

```python
flow.response.content = flow.response.content.replace(b"</head>", test_tag, 1)

```

This does not work; all matches are still replaced. I then found closed ticket #1495 for this, where someone else confirms this doesn’t work. So I tried `re.sub`, which also has a `count` option:

```python
flow.response.content = re.sub(b"</head>", test_tag, flow.response.content, count=1, flags=re.IGNORECASE)

```

Again, this doesn’t work. I’m wondering if each frame/iframe is actually being treated as a separate flow? If that’s the case, any ideas on how I could limit a replace operation to just the first `<head>` section?

Thanks!

---

<div class="post-metadata">

**Author:** ![mhils](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mitmproxy.org/mhils/32/7_2.png) [@mhils](https://discourse.mitmproxy.org/u/mhils)\
**Post date:** [April 11, 2018, 11:03pm UTC](https://discourse.mitmproxy.org/t/replacing-only-in-the-first-match-of-head-in-the-response/980/2 "2018-04-11T23:03:51Z")

</div>

> I’m wondering if each frame/iframe is actually being treated as a separate flow?

Yes - this is how HTML works. I’m not sure if you can identify iframe requests by their HTTP headers, but you can identify it in the injected JavaScript: [javascript - How to identify if a webpage is being loaded inside an iframe or directly into the browser window? - Stack Overflow](https://stackoverflow.com/questions/326069/how-to-identify-if-a-webpage-is-being-loaded-inside-an-iframe-or-directly-into-t)

---

<div class="post-metadata">

**Author:** ![idiotsavant](https://avatars.discourse-cdn.com/v4/letter/i/71c47a/32.png) [@idiotsavant](https://discourse.mitmproxy.org/u/idiotsavant)\
**Post date:** [April 12, 2018, 5:30pm UTC](https://discourse.mitmproxy.org/t/replacing-only-in-the-first-match-of-head-in-the-response/980/3 "2018-04-12T17:30:01Z")

</div>

Thanks for confirming, and for the link!
