How can you check if the user's browser supports Google Caja?

69 Views Asked by At

I'm using Google Caja to sanitize user input on my site. I'm currently processing the user's input inside the callback I pass to caja.whenReady(). However, on Microsoft Edge, the callback is never called because Edge can't be made safe by Caja. In the event that Caja doesn't work, I want to fall back to processing the user's content server side where I'll simply strip out all JavaScript.

How can I check if Caja works with the user's browser?

1

There are 1 best solutions below

0
Kurt Tomlinson On

The code below will give you a function, browserSupportsCaja() that will return true at any time after Caja calls its whenReady() callbacks. This way you can determine if Caja is supported after it initializes.

If you check for Caja support before it finishes initializing, then you will get a false negative. To catch that, just put the code that you want to execute after Caja is ready in a whenReady() callback and assume that Caja is not available anywhere else.

caja.initialize({
  cajaServer: 'https://caja.appspot.com/'
});

function browserSupportsCaja() {
  return browserSupportsCaja.return_value;
}
browserSupportsCaja.return_value = false;

caja.whenReady( function() {
  browserSupportsCaja.return_value = true;
});