0

I would like to disable right click on a shiny app. There is information I would like to not have copied and pasted elsewhere. I understand, a snapshot of the page will remain a possibility, but that's ok. Is there a way to use javascript to disable this functionality in a shiny app? I tried the following, but got an error.

tags$head(HTML("<script type='text/javascript'>document.addEventListener("contextmenu", function(e){
e.preventDefault();
}, false)</script>")),
Sam Kingston
  • 817
  • 1
  • 10
  • 19

2 Answers2

0

This is a JavaScript, not a Shiny, question. Here's the first SO response to this question that I found on Google. Basically, there are many ways for users around it, and it's not recommended to do it because it's mostly just annoying to the user, but you can do it.

Community
  • 1
  • 1
DeanAttali
  • 25,268
  • 10
  • 92
  • 118
  • I understand that. I am having trouble implementing document.addEventListener("contextmenu", function(e){ e.preventDefault(); }, false); in the shiny app – Sam Kingston Oct 18 '15 at 01:35
0

You can set a right-click disable to a block of code with its id and run it with shinyjs

library(shiny)
library(shinyjs)

jscode <- "
shinyjs.init = function() {
    document.getElementById('plot').addEventListener('contextmenu', event => event.preventDefault());
    document.getElementById('myarea').addEventListener('contextmenu', event => event.preventDefault());
}"

ui <- fluidPage(
  useShinyjs(),
  extendShinyjs(text = jscode, functions = c()),
  tabsetPanel(
    tabPanel("Plot", plotOutput("plot")),
    tabPanel("Summary", verbatimTextOutput("summary")),
    tabPanel("Table", tableOutput("table"),div(id="myarea", h1("You can't right click me")))
  )
)

server <- function(input, output, session) {
  
  
    output$plot <- renderPlot({ 
      plot(cars)
    })
}

shinyApp(ui, server)
Zatch Lin
  • 9
  • 3