Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.webkit in Typescript

I need to rewrite a javascript code into typescript (angular), the webpage is opened by an IOS app or an Android app. I just want to send a message to the application.

How can i do to send a message to the parent app or how can i use window.webkit ?

notifyTheApp(postData) {
  if (navigator.userAgent.match(/(iPod|iPhone|iPad)/)) {
    if(window.webkit.messageHandlers)      
     window.webkit.messageHandlers.mpos.postMessage(JSON.stringify(postData));
    } 
    else {
      if(window.external.notify)
        window.external.notify(JSON.stringify(postData));
    }

mpos is the iOS application

ERROR in src/app/sms-validation/sms-validation.component.ts(98,17): error TS2339: Property 'webkit' does not exist on type 'Window'.

like image 327
Nassim El gazzah Avatar asked Jun 09 '26 22:06

Nassim El gazzah


2 Answers

You can use type assertion (https://basarat.gitbooks.io/typescript/content/docs/types/type-assertion.html) to temporarily cast window the any type. You will loose intellisense for that statement though.

(window as any).webkit.messageHandlers
like image 116
Fredrik Lundin Avatar answered Jun 12 '26 12:06

Fredrik Lundin


I reckon window.webkit is pretty far from the standard, and it's therefore not part of the window type in Typescript. You can add it like this:

interface Window {
    webkit?: any;
}

declare var window: Window;

Note that webkit? means that the property is optional, so you should do some checking on whether the property exists or not.

like image 23
Phillip Avatar answered Jun 12 '26 10:06

Phillip



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!