-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathliff-starter.js
243 lines (220 loc) · 8.02 KB
/
liff-starter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
window.onload = function() {
const useNodeJS = false; // if you are not using a node server, set this value to false
const defaultLiffId = "1653577920-jvgBKvv6"; // change the default LIFF value if you are not using a node server
// DO NOT CHANGE THIS
let myLiffId = "";
// if node is used, fetch the environment variable and pass it to the LIFF method
// otherwise, pass defaultLiffId
if (useNodeJS) {
fetch('/send-id')
.then(function(reqResponse) {
return reqResponse.json();
})
.then(function(jsonResponse) {
myLiffId = jsonResponse.id;
initializeLiffOrDie(myLiffId);
})
.catch(function(error) {
document.getElementById("liffAppContent").classList.add('hidden');
document.getElementById("nodeLiffIdErrorMessage").classList.remove('hidden');
});
} else {
myLiffId = defaultLiffId;
initializeLiffOrDie(myLiffId);
}
};
/**
* Check if myLiffId is null. If null do not initiate liff.
* @param {string} myLiffId The LIFF ID of the selected element
*/
function initializeLiffOrDie(myLiffId) {
if (!myLiffId) {
document.getElementById("liffAppContent").classList.add('hidden');
document.getElementById("liffIdErrorMessage").classList.remove('hidden');
} else {
initializeLiff(myLiffId);
}
}
/**
* Initialize LIFF
* @param {string} myLiffId The LIFF ID of the selected element
*/
function initializeLiff(myLiffId) {
liff
.init({
liffId: myLiffId
})
.then(() => {
// start to use LIFF's api
initializeApp();
})
.catch((err) => {
document.getElementById("liffAppContent").classList.add('hidden');
document.getElementById("liffInitErrorMessage").classList.remove('hidden');
});
}
/**
* Initialize the app by calling functions handling individual app components
*/
function initializeApp() {
displayLiffData();
displayIsInClientInfo();
registerButtonHandlers();
// check if the user is logged in/out, and disable inappropriate button
if (liff.isLoggedIn()) {
document.getElementById('liffLoginButton').disabled = true;
} else {
document.getElementById('liffLogoutButton').disabled = true;
}
}
/**
* Display data generated by invoking LIFF methods
*/
function displayLiffData() {
document.getElementById('browserLanguage').textContent = liff.getLanguage();
document.getElementById('sdkVersion').textContent = liff.getVersion();
document.getElementById('isInClient').textContent = liff.isInClient();
document.getElementById('isLoggedIn').textContent = liff.isLoggedIn();
document.getElementById('deviceOS').textContent = liff.getOS();
}
/**
* Toggle the login/logout buttons based on the isInClient status, and display a message accordingly
*/
function displayIsInClientInfo() {
if (liff.isInClient()) {
document.getElementById('liffLoginButton').classList.toggle('hidden');
document.getElementById('liffLogoutButton').classList.toggle('hidden');
document.getElementById('isInClientMessage').textContent = 'You are opening the app in the in-app browser of LINE.';
} else {
document.getElementById('isInClientMessage').textContent = 'You are opening the app in an external browser.';
}
}
/**
* Register event handlers for the buttons displayed in the app
*/
function registerButtonHandlers() {
// openWindow call
document.getElementById('openWindowButton').addEventListener('click', function() {
liff.openWindow({
url: 'https://line.me',
external: true
});
});
// closeWindow call
document.getElementById('closeWindowButton').addEventListener('click', function() {
if (!liff.isInClient()) {
sendAlertIfNotInClient();
} else {
liff.closeWindow();
}
});
// sendMessages call
document.getElementById('sendMessageButton').addEventListener('click', function() {
if (!liff.isInClient()) {
sendAlertIfNotInClient();
} else {
liff.sendMessages([{
'type': 'text',
'text': "You've successfully sent a message! Hooray!"
}]).then(function() {
window.alert('Message sent');
}).catch(function(error) {
window.alert('Error sending message: ' + error);
});
}
});
// scanCode call
document.getElementById('scanQrCodeButton').addEventListener('click', function() {
if (!liff.isInClient()) {
sendAlertIfNotInClient();
} else {
liff.scanCode().then(result => {
// e.g. result = { value: "Hello LIFF app!" }
const stringifiedResult = JSON.stringify(result);
document.getElementById('scanQrField').textContent = stringifiedResult;
toggleQrCodeReader();
}).catch(err => {
document.getElementById('scanQrField').textContent = "scanCode failed!";
});
}
});
// get access token
document.getElementById('getAccessToken').addEventListener('click', function() {
if (!liff.isLoggedIn() && !liff.isInClient()) {
alert('To get an access token, you need to be logged in. Please tap the "login" button below and try again.');
} else {
const accessToken = liff.getAccessToken();
document.getElementById('accessTokenField').textContent = accessToken;
toggleAccessToken();
}
});
// get profile call
document.getElementById('getProfileButton').addEventListener('click', function() {
liff.getProfile().then(function(profile) {
document.getElementById('userIdProfileField').textContent = profile.userId;
document.getElementById('displayNameField').textContent = profile.displayName;
const profilePictureDiv = document.getElementById('profilePictureDiv');
if (profilePictureDiv.firstElementChild) {
profilePictureDiv.removeChild(profilePictureDiv.firstElementChild);
}
const img = document.createElement('img');
img.src = profile.pictureUrl;
img.alt = 'Profile Picture';
profilePictureDiv.appendChild(img);
document.getElementById('statusMessageField').textContent = profile.statusMessage;
toggleProfileData();
}).catch(function(error) {
window.alert('Error getting profile: ' + error);
});
});
// login call, only when external browser is used
document.getElementById('liffLoginButton').addEventListener('click', function() {
if (!liff.isLoggedIn()) {
// set `redirectUri` to redirect the user to a URL other than the front page of your LIFF app.
liff.login();
}
});
// logout call only when external browse
document.getElementById('liffLogoutButton').addEventListener('click', function() {
if (liff.isLoggedIn()) {
liff.logout();
window.location.reload();
}
});
}
/**
* Alert the user if LIFF is opened in an external browser and unavailable buttons are tapped
*/
function sendAlertIfNotInClient() {
alert('This button is unavailable as LIFF is currently being opened in an external browser.');
}
/**
* Toggle access token data field
*/
function toggleAccessToken() {
toggleElement('accessTokenData');
}
/**
* Toggle profile info field
*/
function toggleProfileData() {
toggleElement('profileInfo');
}
/**
* Toggle scanCode result field
*/
function toggleQrCodeReader() {
toggleElement('scanQr');
}
/**
* Toggle specified element
* @param {string} elementId The ID of the selected element
*/
function toggleElement(elementId) {
const elem = document.getElementById(elementId);
if (elem.offsetWidth > 0 && elem.offsetHeight > 0) {
elem.style.display = 'none';
} else {
elem.style.display = 'block';
}
}