[英]How to close Shiny app when opened in Facebook messenger webview pop up window?
I have a shiny app that opens in facebook messenger webview popup small window.我有一个 shiny 应用程序,它在 facebook 信使 webview 弹出小窗口 Z05B8C74CBD96FBF270244C 中打开。 I want it to be closed when the user is finished.我希望它在用户完成后关闭。
I am using the following to do it but somehow it doesn't close the webview:我正在使用以下方法来执行此操作,但不知何故它不会关闭 webview:
library(shiny)
library(shinyWidgets)
library(jsonlite)
jscode <- "shinyjs.closeWindow = function() { window.close(); }
shinyjs.closeMessengerWebview = function() { MessengerExtensions.requestCloseBrowser(function success() {}, function error(err) {}); }"
send_reminder_data <- function(times_input_value,
recipient_id,
drug_name,
tz_offset,
url = "xxxxxxxxxxxxxxxxxxxxxxxxx",
times_format = "%H:%M") {
x <- list(source = "shiny_time_picker_app",
recipient_id = recipient_id,
drug_name = drug_name,
tz_offset = tz_offset,
times = lapply(
times_input_value,
strftime,
format = times_format
)
)
httr::POST(url,
body = toJSON(x, auto_unbox = TRUE),
encode = "json")
}
ui <- fluidPage(
shinyjs::useShinyjs(),
shinyjs::extendShinyjs(text = jscode, functions = c("closeMessengerWebview")),
htmlOutput("queryText"),
selectInput("timezone_offset", "Timezone: UTC", choices = -12:14, selected = 0),
uiOutput("times")
)
server <- function(input, output, session) {
num_time_slots <- 10
output$queryText <- renderUI({
query <- parseQueryString(session$clientData$url_search)
HTML(paste("<h1>Reminder for ", query[["drug"]], "</h1>", sep = ""))
})
observe({
query <- parseQueryString(session$clientData$url_search)
if (!is.null(query[["default_tz_offset"]])) {
updateSelectInput(session, "timezone_offset", selected = query[["default_tz_offset"]])
}
})
output$times <- renderUI({
query <- parseQueryString(session$clientData$url_search)
num_time_slots <- ifelse(length(query[["n"]])==0, num_time_slots, as.numeric(query[["n"]]))
div(
lapply(seq(num_time_slots), function(i) {
airDatepickerInput(
inputId = paste0("time_", i),
label = "Pick Time:",
timepicker = TRUE,
onlyTimepicker = TRUE,
timepickerOpts = c(minutesStep = 10),
autoClose = TRUE,
clearButton = TRUE
)
}),
actionButton("commit", "Send & Approve", icon = icon("send"))
)
})
observeEvent(input$commit, {
x <- purrr::compact(lapply(seq(num_time_slots), function(i) {input[[paste0("time_", i)]]}))
query <- parseQueryString(session$clientData$url_search)
recipient_id <- query[["recipient_id"]]
drug_name <- query[["drug"]]
tz_offset <- as.numeric(input$timezone_offset)
send_reminder_data(x, recipient_id, drug_name, tz_offset)
shinyjs::hideElement("commit")
# shinyjs::js$closeWindow()
shinyjs::js$closeMessengerWebview()
stopApp()
})
}
shinyApp(ui = ui, server = server)
Add messenger SDK,添加信使SDK,
<script>
var psid;
(function (d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) { return; }
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/messenger.Extensions.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'Messenger'));
window.extAsyncInit = () => {
// TODO: How to parse env file from here?
};
</script>
Now you can easily access MessengerExtensions
and close the webview on a specific action.现在您可以轻松访问MessengerExtensions
并在特定操作上关闭 webview。
MessengerExtensions.requestCloseBrowser(function success() {
console.log("Webview closing");
}, function error(err) {
console.log(err);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.