From 510122d3366e10b42eae1e1e31eeeac843f953a3 Mon Sep 17 00:00:00 2001 From: Kun Ren Date: Tue, 1 Sep 2020 13:19:29 +0800 Subject: [PATCH 1/2] Check url in browser --- R/init.R | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/R/init.R b/R/init.R index 2ad3b819a..58b607efd 100644 --- a/R/init.R +++ b/R/init.R @@ -331,7 +331,23 @@ if (interactive() && browser <- function(url, title = url, ..., viewer = getOption("vsc.browser", "Active")) { - request("browser", url = url, title = title, ..., viewer = viewer) + if (grepl("^https?\\://(127\\.0\\.0\\.1|localhost)(\\:\\d+)?", url)) { + request("browser", url = url, title = title, ..., viewer = viewer) + } else if (grepl("^https?\\://", url)) { + message("VSCode WebView only supports showing local http content.") + message("Opening in external browser...") + request("browser", url = url, title = title, ..., viewer = FALSE) + } else if (file.exists(url)) { + url <- normalizePath(url) + if (grepl("\\.html?$", url, ignore.case = TRUE)) { + message("VSCode WebView has restricted access to local file.") + message("Opening in external browser...") + request("browser", url = url, title = title, ..., viewer = FALSE) + } else { + request("dataview", source = "object", type = "txt", + title = title, file = url, viewer = viewer) + } + } } webview <- function(url, title, ..., viewer) { From 88eeb474a183684b902f8d9659dde66f28c8bba6 Mon Sep 17 00:00:00 2001 From: Kun Ren Date: Tue, 8 Sep 2020 07:25:14 +0800 Subject: [PATCH 2/2] Use path_to_uri in browser --- R/init.R | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/R/init.R b/R/init.R index 58b607efd..7ec028b4b 100644 --- a/R/init.R +++ b/R/init.R @@ -329,6 +329,20 @@ if (interactive() && plot = getOption("vsc.plot", "Two")) } + path_to_uri <- function(path) { + if (length(path) == 0) { + return(character()) + } + path <- path.expand(path) + if (.Platform$OS.type == "windows") { + prefix <- "file:///" + path <- gsub("\\", "/", path, fixed = TRUE) + } else { + prefix <- "file://" + } + paste0(prefix, utils::URLencode(path)) + } + browser <- function(url, title = url, ..., viewer = getOption("vsc.browser", "Active")) { if (grepl("^https?\\://(127\\.0\\.0\\.1|localhost)(\\:\\d+)?", url)) { @@ -338,15 +352,18 @@ if (interactive() && message("Opening in external browser...") request("browser", url = url, title = title, ..., viewer = FALSE) } else if (file.exists(url)) { - url <- normalizePath(url) + url <- normalizePath(url, "/", mustWork = TRUE) if (grepl("\\.html?$", url, ignore.case = TRUE)) { message("VSCode WebView has restricted access to local file.") message("Opening in external browser...") - request("browser", url = url, title = title, ..., viewer = FALSE) + request("browser", url = path_to_uri(url), + title = title, ..., viewer = FALSE) } else { request("dataview", source = "object", type = "txt", title = title, file = url, viewer = viewer) } + } else { + stop("File not exists") } }