TypeError: Illegal invocation on function alias

307 views Asked by At

I have been trying to create JavaScript that includes functions for managing an IndexedDB database that acts as a filesystem; the segment of code below helps protect the database from being modified in unauthorized ways by other client-side scripts, but an uncaught "TypeError: Illegal invocation" exception is thrown on line 16.

$(document).ready(function(){
  var db;
  var proxiedDBOpen = indexedDB.open.bind(window);
  indexedDB.open = function(name, version) {
    if(name === 'MyTestDatabase')
    {
      console.error('Security error: Unauthorized filesystem access.');
      return;
    }
    else
    {
      return proxiedDBOpen.apply(window, arguments);
    }
  }
  
  var request = proxiedDBOpen('MyTestDatabase', 4); // Uncaught TypeError: Illegal invocation
  
  // Database management code follows (uses jQuery)...
  
});

After reading other posts, I tried making sure this was set to window in the scope of the call to proxiedDBOpen (Function.prototype.bind is called on line 3 in an attempt to do this), but this did not seem to help; I also tried var request = function(name, version) {return proxiedDBOpen.apply(this, arguments);}('MyTestDatabase', 4);, which causes the same exception at the call to proxiedDBOpen.apply.

1

There are 1 answers

0
Pi Programs On

@levi's suggestion solved my problem (indexedDB.open expects this to be indexedDB in its context).