line_push/plugins/liff.ts
2022-07-17 13:16:16 +08:00

73 lines
1.4 KiB
TypeScript

const loaded: { [key: string]: Promise<void> } = {}
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<void>
// authentication
isLoggedIn(): boolean
login(
loginConfig?: {
redirectUri?: string
}
)
logout()
getAccessToken(): string
// profile
getProfile(): Promise<IProfile>
sendMessages(messages: Message[])
// utils
openWindow(params: {
url: string,
external?: boolean
})
scanCode(): string
closeWindow()
}
export async function initLIFF(liffId): Promise<ILiff> {
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
}