# Webextension Content script return Promise to page-script

**URL:** <https://discourse.mozilla.org/t/webextension-content-script-return-promise-to-page-script/17193>\
**Category:** Development\
**Created:** [July 19, 2017, 6:12am UTC](https://discourse.mozilla.org/t/webextension-content-script-return-promise-to-page-script/17193 "2017-07-19T06:12:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mitkola](https://avatars.discourse-cdn.com/v4/letter/m/5daacb/32.png) [@mitkola](https://discourse.mozilla.org/u/mitkola)\
**Post date:** [July 19, 2017, 6:12am UTC](https://discourse.mozilla.org/t/webextension-content-script-return-promise-to-page-script/17193/1 "2017-07-19T06:12:25Z")

</div>

Hi!  
The case is as follow.

1. Content script creates object and clone it to `window` to be accessible from page script code.
2. Cloned content script object has a method which is asynchronous call and I want to return Promise to page script.  
[Xray vision](https://developer.mozilla.org/en-US/docs/Mozilla/Tech/Xray_vision) prevent returning object to page script.  
The only way I found to inform page script from content script about state of async method is [Window.postMessage](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage).  
Are there any other solution to implement in more elegant way to using Promise style in calling content script object from page script code?  
Here is sample code which is disallowed from Xray vision to return promise .

```javascript
// content script
var jsPrintSetup = {
  print: function(printSettings) {
    return new Promise(
      function (resolve, reject) {
        // calling async print ...
        if (some_condition)
          resolve(res);
        else
          reject(err);
      }
    );
  }
}

window.wrappedJSObject.jsPrintSetup = cloneInto(
  jsPrintSetup,
  window,
  {cloneFunctions: true}  
);

// page script
window.jsPrintSetup.print(null).then(
  (res) => {
    // ok resolved
  }
  , (err) => {
    // rejected
  } 
);

```

---

<div class="post-metadata">

**Author:** ![NilkasG](https://avatars.discourse-cdn.com/v4/letter/n/0ea827/32.png) [@NilkasG](https://discourse.mozilla.org/u/NilkasG)\
**Post date:** [July 19, 2017, 6:47pm UTC](https://discourse.mozilla.org/t/webextension-content-script-return-promise-to-page-script/17193/2 "2017-07-19T18:47:23Z")

</div>

Try

```auto
return new window.wrappedJSObject.Promise(...)

```

You may have to use `exportFunction` or something on the argument to `Promise`. Also, make sure you are aware of all the security implications of what you are doing.

---

<div class="post-metadata">

**Author:** ![mitkola](https://avatars.discourse-cdn.com/v4/letter/m/5daacb/32.png) [@mitkola](https://discourse.mozilla.org/u/mitkola)\
**Post date:** [July 20, 2017, 7:10am UTC](https://discourse.mozilla.org/t/webextension-content-script-return-promise-to-page-script/17193/3 "2017-07-20T07:10:37Z")

</div>

I have tried with `window.wrappedJSObect` bet get an error `Error: Permission denied to access object`.  
`exportFunction` is not applicable to `Permission` object.

```javascript
// content script
//...
    return new window.wrappedJSObject.Promise(
      function (resolve, reject) {
        // calling async print ...
        if (some_condition)
          resolve(res);
        else
          reject(err);
      }
    );
  }
//...

```

---

<div class="post-metadata">

**Author:** ![NilkasG](https://avatars.discourse-cdn.com/v4/letter/n/0ea827/32.png) [@NilkasG](https://discourse.mozilla.org/u/NilkasG)\
**Post date:** [July 20, 2017, 1:35pm UTC](https://discourse.mozilla.org/t/webextension-content-script-return-promise-to-page-script/17193/4 "2017-07-20T13:35:06Z")

</div>

> [@NilkasG](#):
>
> You may have to use `exportFunction` or something on the argument to `Promise`.

What I meant was:

```auto
return new window.wrappedJSObject.Promise(exportFunction((resolve, reject) => /*...*/))

```

That might just avoid the `Error: Permission denied to access object`.

* * *

> [@mitkola](#):
>
> `exportFunction` is not applicable to `Permission` object.

I have no idea what you mean.

---

<div class="post-metadata">

**Author:** ![mitkola](https://avatars.discourse-cdn.com/v4/letter/m/5daacb/32.png) [@mitkola](https://discourse.mozilla.org/u/mitkola)\
**Post date:** [July 20, 2017, 6:19pm UTC](https://discourse.mozilla.org/t/webextension-content-script-return-promise-to-page-script/17193/5 "2017-07-20T18:19:50Z")

</div>

I’ve was in mistake.  
The above is work as I needed.  
Finally i’ve tested this code and is working.

Thank you!

```javascript
// content script
var jsPrintSetup = {
  print: function(printSettings) {
    return new window.wrappedJSObject.Promise(
      exportFunction(
        function (resolve, reject) {
          // calling async print ...
          if (some_condition)
            resolve(res);
          else
            reject(err);
        }
        , window.wrappedJSObject
     )
    );
  }
}

```

---

<div class="post-metadata">

**Author:** ![minfrin](https://avatars.discourse-cdn.com/v4/letter/m/f14d63/32.png) [@minfrin](https://discourse.mozilla.org/u/minfrin)\
**Post date:** [February 25, 2022, 12:06pm UTC](https://discourse.mozilla.org/t/webextension-content-script-return-promise-to-page-script/17193/6 "2022-02-25T12:06:09Z")

</div>

Reviving this issue in 2022.

While returning a promise appears to work, the then() method on the promise cannot be called due to permission being denied.

The inability for promises to cross the cloneInto/exportFunction boundary has been raised here: [https://bugzilla.mozilla.org/show\_bug.cgi?id=1757066](https://bugzilla.mozilla.org/show_bug.cgi?id=1757066)
