Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

How to listen on message (postmesssage) in Checkout ui extension

Solved

How to listen on message (postmesssage) in Checkout ui extension

manishgowardipe
Visitor
2 0 0

Hi,

I have a scenario where a javascript (injected using scriptTags)
this script does some calculation & gets a result variable ( const a ="foo" )
I want this script to send a `postmessage` to my extension worker.

Inside my extension, i'm listening for the message,
Checkout.tsx

  useEffect(() => {
    seld.addEventListener("message", (event: { data: string }) => {
      const data = JSON.parse(event.data)
  
      !!data &&
        applyMetaFieldChange({
          type: "updateCartMetafield",
          metafield: {
            key: "foo-key",
            namespace: "my-extension-namesapce",
            type: "single_line_text_field",
            value: data,
          },
        }).then((result) => {
          console.log("metafield set in the cart", result);
        });
    });
  }, [applyMetaFieldChange]);



when i try to send to postmessage like following in the console window from `top`window..

window.postMessage(JSON.stringify({"foo-key": "abcd"}), "https://extensions.shopifycdn.com") // to the specific target origin


I get the following error

Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('https://extensions.shopifycdn.com') does not match the recipient window's origin ('https://quickstart-9bac1fe7.myshopify.com').



Accepted Solution (1)

Liam
Community Manager
3108 344 899

This is an accepted solution.

Hi Manishgowardipe

 

Checkout Extensibility does not support DOM manipulation or use of custom JavaScript, which may be why you're not able to send to postmessage as expected. 

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

View solution in original post

Reply 1 (1)

Liam
Community Manager
3108 344 899

This is an accepted solution.

Hi Manishgowardipe

 

Checkout Extensibility does not support DOM manipulation or use of custom JavaScript, which may be why you're not able to send to postmessage as expected. 

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog