Stephen Cagle
Stephen Cagle

Reputation: 14544

'Uncaught TypeError: Illegal invocation' when trying to support cross browser prefixes in clojurescript

getUserMedia has a number of prefixes that are still necessary in many browsers. I am attempting to hide the use of these prefixes by putting all the prefixes in getUserMedia.

(def getUserMedia
  (or js/navigator.getUserMedia
      js/navigator.webkitGetUserMedia
      js/navigator.mozGetUserMedia
      js/navigator.msGetUserMedia))

(defn start-fx [app]
  (if getUserMedia
    (getUserMedia 
     ;; WORKS IF I REPLACE THE ABOVE getUserMedia WITH
     ;; js/navigator.webkitGetUserMedia USING CHROME BROWSER
     #js {:video true :audio true}
     #(om/update! app :record-src (.createObjectURL js/window.URL %))
     #(throw %))
    (js/alert "This browser does not support video recording!")))

When I then attempt to call this from start-fx, I get a Uncaught TypeError: Illegal invocation at the calling of getUserMedia. What should I do to allow my code to be cross browser compatible?

Upvotes: 3

Views: 610

Answers (2)

Stephen Cagle
Stephen Cagle

Reputation: 14544

Just to have an answer, I am posting my fix for the problem. Ugh, surely there must be a better solution available?

(def getUserMedia
  (or js/navigator.getUserMedia
      js/navigator.webkitGetUserMedia
      js/navigator.mozGetUserMedia
      js/navigator.msGetUserMedia))

(defn start-fx [app]
  (if getUserMedia
    (let [[c s f] [#js {:video true :audio true}
                   #(om/update! app 
                               :record-src 
                               (.createObjectURL js/window.URL %))
                   #(throw %)]]
      (getUserMedia c s f))
    (js/alert "This browser does not support video recording!")))

Upvotes: 0

Mike Cluck
Mike Cluck

Reputation: 32511

getUserMedia has to be bound to the navigator object. The equivalent JavaScript would be this:

var getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
                   navigator.mozGetUserMedia || msGetUserMedia;
if (getUserMedia) {
  getUserMedia = getUserMedia.bind(navigator);
}

Upvotes: 5

Related Questions