# Is there a reliable way to execute a page script at document\_start

**URL:** <https://discourse.mozilla.org/t/is-there-a-reliable-way-to-execute-a-page-script-at-document-start/65580>\
**Category:** Development\
**Created:** [August 12, 2020, 7:58am UTC](https://discourse.mozilla.org/t/is-there-a-reliable-way-to-execute-a-page-script-at-document-start/65580 "2020-08-12T07:58:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tiansh](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/tiansh/32/30573_2.png) [@tiansh](https://discourse.mozilla.org/u/tiansh)\
**Post date:** [August 12, 2020, 7:58am UTC](https://discourse.mozilla.org/t/is-there-a-reliable-way-to-execute-a-page-script-at-document-start/65580/1 "2020-08-12T07:58:10Z")

</div>

Let’s say, I have a `page.js` which contains codes need to be run at document\_start as page script. The script must be executed before any code the page provided. The script need to access variables on window, so it have to be an page script not content script.

Is there a reliable way to execute the `page.js` in pages’ context before any other code? How can I do that?

---

<div class="post-metadata">

**Author:** ![jscher2000](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/jscher2000/32/28202_2.png) [@jscher2000](https://discourse.mozilla.org/u/jscher2000)\
**Post date:** [August 12, 2020, 3:14pm UTC](https://discourse.mozilla.org/t/is-there-a-reliable-way-to-execute-a-page-script-at-document-start/65580/2 "2020-08-12T15:14:47Z")

</div>

run\_at: “document\_start” isn’t reliable? I haven’t tried it myself.

> **[content\_scripts - Mozilla | MDN](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/content_scripts)**
>
> Instructs the browser to load content scripts into web pages whose URL matches a given pattern.

---

<div class="post-metadata">

**Author:** ![tiansh](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/tiansh/32/30573_2.png) [@tiansh](https://discourse.mozilla.org/u/tiansh)\
**Post date:** [August 13, 2020, 1:54am UTC](https://discourse.mozilla.org/t/is-there-a-reliable-way-to-execute-a-page-script-at-document-start/65580/3 "2020-08-13T01:54:47Z")

</div>

I’m asking for page script.

---

<div class="post-metadata">

**Author:** ![jscher2000](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/jscher2000/32/28202_2.png) [@jscher2000](https://discourse.mozilla.org/u/jscher2000)\
**Post date:** [August 13, 2020, 6:19am UTC](https://discourse.mozilla.org/t/is-there-a-reliable-way-to-execute-a-page-script-at-document-start/65580/4 "2020-08-13T06:19:26Z")

</div>

I don’t know how a page script is different from a content script. We are talking about injecting a script into the page using an extension, correct?

---

<div class="post-metadata">

**Author:** ![juraj.masiar](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/juraj.masiar/32/30587_2.png) [@juraj.masiar](https://discourse.mozilla.org/u/juraj.masiar)\
**Post date:** [August 13, 2020, 7:09am UTC](https://discourse.mozilla.org/t/is-there-a-reliable-way-to-execute-a-page-script-at-document-start/65580/5 "2020-08-13T07:09:51Z")

</div>

I think he wants to execute his own content script in a page but it must run before the page executes its own scripts.

But I’m not sure about accessing window variables of the page script from your content script…  
[MDN](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Content_scripts#Content_script_environment) says it’s not possible:

> The same is true in reverse; page scripts cannot see JavaScript properties added by content scripts.
> 
> This means that content scripts can rely on DOM properties behaving predictably, without worrying about its variables clashing with variables from the page script.
> 
> One practical consequence of this behavior is that a content script doesn’t have access to any JavaScript libraries loaded by the page. So, for example, if the page includes jQuery, the content script can’t see it.

---

<div class="post-metadata">

**Author:** ![tiansh](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/tiansh/32/30573_2.png) [@tiansh](https://discourse.mozilla.org/u/tiansh)\
**Post date:** [August 13, 2020, 7:17am UTC](https://discourse.mozilla.org/t/is-there-a-reliable-way-to-execute-a-page-script-at-document-start/65580/6 "2020-08-13T07:17:59Z")

</div>

I think I want to execute my own PAGE SCRIPT, not content script.

---

<div class="post-metadata">

**Author:** ![juraj.masiar](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/juraj.masiar/32/30587_2.png) [@juraj.masiar](https://discourse.mozilla.org/u/juraj.masiar)\
**Post date:** [August 13, 2020, 7:29am UTC](https://discourse.mozilla.org/t/is-there-a-reliable-way-to-execute-a-page-script-at-document-start/65580/7 "2020-08-13T07:29:45Z")

</div>

Ok so I’m no expert in this area but I guess if you relax [some of the CSP](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/content_security_policy), you should be able to edit DOM and inject your own script to the page.

This may pose a issue for an AMO reviewer though.

---

<div class="post-metadata">

**Author:** ![alexdi0421](https://avatars.discourse-cdn.com/v4/letter/a/22d042/32.png) [@alexdi0421](https://discourse.mozilla.org/u/alexdi0421)\
**Post date:** [August 10, 2021, 10:48pm UTC](https://discourse.mozilla.org/t/is-there-a-reliable-way-to-execute-a-page-script-at-document-start/65580/8 "2021-08-10T22:48:30Z")

</div>

You can create a script element and append it to the document.

var script = document.createElement(‘script’);  
document.documentElement.appendChild(script);

documentElement needs to be added for it to work.

And set run\_at to document\_start.
