# Add html to new tab

**URL:** https://discourse.mozilla.org/t/add-html-to-new-tab/10346
**Category:** Development
**Created:** [August 18, 2016, 12:40pm UTC](https://discourse.mozilla.org/t/add-html-to-new-tab/10346 "2016-08-18T12:40:43Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![h4ever](https://avatars.discourse-cdn.com/v4/letter/h/dc4da7/32.png) [@h4ever](https://discourse.mozilla.org/u/h4ever)
#### Post date: [August 18, 2016, 12:40pm UTC](https://discourse.mozilla.org/t/add-html-to-new-tab/10346/1 "2016-08-18T12:40:44Z")

</div>

I have a web-page with injected Content Script (CS). The Background Script (BS) sends message to create new tab. When the tab is created I send message to CS where id of the new tab is contained. Then In the CS when the message is intercepted, I send message to the tab just been created (Page Script - PS). The message adds styles to head and some PS is executed to add html… Here I add a break point. When I debug it the debugger stopped on the PS in the function which adds the html to webpage and everything is fine. If I remove the break point then the html is not added to the new tab. The tab is opened and the html is not added. I suspect the message was send before the DOM is loaded. It is not clear to me how to solve the problem.

---

<div class="post-metadata">

### Author: ![noitidart](https://avatars.discourse-cdn.com/v4/letter/n/9f8e36/32.png) [@noitidart](https://discourse.mozilla.org/u/noitidart)
#### Post date: [August 19, 2016, 11:53pm UTC](https://discourse.mozilla.org/t/add-html-to-new-tab/10346/2 "2016-08-19T23:53:22Z")

</div>

I think nothing can be injected into privelaged pages like about:blank, about:newtab, etc. Do you think this is the issue that is affecting you?

---

<div class="post-metadata">

### Author: ![h4ever](https://avatars.discourse-cdn.com/v4/letter/h/dc4da7/32.png) [@h4ever](https://discourse.mozilla.org/u/h4ever)
#### Post date: [August 20, 2016, 7:52am UTC](https://discourse.mozilla.org/t/add-html-to-new-tab/10346/3 "2016-08-20T07:52:09Z")

</div>

No, there is just an empty tab using the addon address. I can add html there but the message comes earlier before the DOM is completed or before the javascript listener is started.

I think I will need to improve messaging, make a ready-test request, which will make it a bit more complicated but only solution.

---

<div class="post-metadata">

### Author: ![noitidart](https://avatars.discourse-cdn.com/v4/letter/n/9f8e36/32.png) [@noitidart](https://discourse.mozilla.org/u/noitidart)
#### Post date: [August 20, 2016, 8:01am UTC](https://discourse.mozilla.org/t/add-html-to-new-tab/10346/4 "2016-08-20T08:01:54Z")

</div>

Oh instead of that you can wait till its completley loaded. You can either inject your content script which is wrapped in a window.addEventListener of `load` (or `DOMContentReady`). Or you can change the `run_at` attribute of the content script in manifest.json to be at a latter stage, like `document_end`.

---

<div class="post-metadata">

### Author: ![temps.jo](https://avatars.discourse-cdn.com/v4/letter/t/dec6dc/32.png) [@temps.jo](https://discourse.mozilla.org/u/temps.jo)
#### Post date: [September 5, 2016, 7:06pm UTC](https://discourse.mozilla.org/t/add-html-to-new-tab/10346/5 "2016-09-05T19:06:51Z")

</div>

> [@noitidart](#):
>
> window.addEventListener of load

IHello,  
I have the same problem " calling js with Background "  
I tried window.addEventListener of load, and it does not work for Firefox 48

Sorry, really sorry for the begin of this post, if I use this, it is a well done :  
function load() {  
var el = document.getElementById(“t”);  
el.addEventListener(“click”, lecjoa, false);  
}

document.addEventListener(“DOMContentLoaded”, function(event) {  
load();  
});

cordially

---

<div class="post-metadata">

### Author: ![h4ever](https://avatars.discourse-cdn.com/v4/letter/h/dc4da7/32.png) [@h4ever](https://discourse.mozilla.org/u/h4ever)
#### Post date: [September 6, 2016, 7:22am UTC](https://discourse.mozilla.org/t/add-html-to-new-tab/10346/6 "2016-09-06T07:22:59Z")

</div>

Currently I am occupied with a work on different stuff so I cannot test it. But later I would try. Thanks
