일부 웹사이트에서는 앱의 메뉴 항목을 동적으로 수정하는 기능이 유용할 수 있어요. 다음 JavaScript 함수를 사용하면 돼요.
JavaScript 함수 사용하기
다음의
executeWhenAppReady() 함수를 확인해 보세요. 앱 도우미 스크립트예요.. 이 함수는 앱이 준비되기 전이나 웹사이트가 일반 브라우저에서 로드되었을 때 웹사이트에서 앱과 상호작용을 시도하지 않도록 해줘요(ReferenceError, 함수가 정의되지 않음). getMenuItems
현재 표시되고 있는 모든 메뉴 항목을 나타내는 객체 목록을 가져오려면 이 함수를 사용하세요.
<script>
try {
// returns a list of objects representing the menu items
let menuItems = (await getMenuItems())["menuItems"];
}
catch (e) {
// Can occur if:
// - the app couldn't connect to the native code. Should be very unlikely.
console.log(e);
}
</script>
setMenuItems
표시할 메뉴 항목을 수정하려면 이 함수를 사용하세요.
<script>
try {
// example for a menu item object
let newMenuItem = {
name: "Contact",
action: {
urlToRedirectTo: "https://webtoapp.design/contact",
javascriptToExecute: null,
elementToClickSelector: null,
isFavoriteAction: false,
isShareAction: false,
isOpenExternallyAction: false,
isSettingsAction: false,
isRateAction: false,
isPastNotificationsAction: false,
},
icon: null,
children: []
};
// get the currently displayed menu items
let allMenuItems = (await getMenuItems())["menuItems"];
// add the new menu item to the list of menu items
allMenuItems.push(newMenuItem);
// display the list of menu items, including the new menu item
await setMenuItems(allMenuItems);
}
catch (e) {
// Can occur if:
// - the app couldn't connect to the native code. Should be very unlikely.
console.log(e);
}
</script>
convertElementToMenuItem
특정 요소(일반적으로 링크나 버튼)를 클릭하는 것과 동일한 메뉴 항목 객체를 가져오는 유틸리티 함수예요.
<a href="https://webtoapp.design/" id="my-link">My Link</a>
<script>
let myLinkElement = document.getElementById("my-link");
let myMenuItem = convertElementToMenuItem(myLinkElement);
/*
The result will look something like this:
{
name: "My Link",
action: {
urlToRedirectTo: null,
javascriptToExecute: null,
elementToClickSelector: "#my-link",
isFavoriteAction: false,
isShareAction: false,
isOpenExternallyAction: false,
isSettingsAction: false,
isRateAction: false,
isPastNotificationsAction: false,
},
icon: null,
children: []
};
or (depending on the app configuration)
{
name: "My Link",
action: {
urlToRedirectTo: "https://webtoapp.design/",
javascriptToExecute: null,
elementToClickSelector: null,
isFavoriteAction: false,
isShareAction: false,
isOpenExternallyAction: false,
isSettingsAction: false,
isRateAction: false,
isPastNotificationsAction: false,
},
icon: null,
children: []
};
*/
</script>