const loaded: { [key: string]: Promise } = {} function loadScript(url) { if (!loaded[url]) { loaded[url] = new Promise((resolve, reject) => { const script = document.createElement('script') script.onload = () => resolve() script.onerror = reject script.src = url document.head.appendChild(script) }) } return loaded[url] } type Message = any interface IProfile { userId: string displayName: string pictureUrl: string statusMessage: string } export interface ILiff { init( config: { liffId: string }, successCallback?: () => void, errorCallback?: () => void, ) getOS (): string getLanguage (): string getVersion(): string isInClient(): boolean // plugins initPlugins(pluginList: string[]): Promise // authentication isLoggedIn(): boolean login( loginConfig?: { redirectUri?: string } ) logout() getAccessToken(): string // profile getProfile(): Promise sendMessages(messages: Message[]) // utils openWindow(params: { url: string, external?: boolean }) scanCode(): string closeWindow() } export async function initLIFF(liffId): Promise { await loadScript('https://static.line-scdn.net/liff/edge/2.1/sdk.js') const liff: ILiff = (window as any).liff await new Promise((resolve, _) => { liff.init({ liffId }, resolve) }) return liff }