cesarvr / pdf-generator

Cordova plugin to generate pdf in the client-side
MIT License
107 stars 61 forks source link

Error when opening database after closing plugin view on Android #27

Closed rastafan closed 7 years ago

rastafan commented 7 years ago

Hi, we get an error when trying to access database after using this plugin on Android. The error shown is the following:

"Uncaught SecurityError: Failed to execute 'openDatabase' on 'Window': Access to the WebDatabase API is denied in this context."

How to reproduce:

1)Execute htmlToPdf function from cordova application 2)Exit the newly opened view 3)Try to open database with window.openDatabase(...). The error should now be shown in console.

How can we fix this?

Thanks.

cesarvr commented 7 years ago

Hi,

I think the problem is that for some reason the access to Cordova native from webkit change the webkit context in some way. I tried to initialise WebSQL after calling to the plugin and I got the same error.

DOMException: Failed to execute 'openDatabase' on 'Window': Access to the WebDatabase API is denied in this context.(…)

But if you initialise WebSQL before calling the plugin should work fine.

//execute this first then reuse the instance globally. 
try { var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024); }catch(e){console.log('message->', e)}

// Working code, make cordova calls, make pdf etc.. 

db.transaction(function (tx) { // now db transactions work fine here. 

I tested using this WebSQL example.

Cheers.

rastafan commented 7 years ago

Hi,

thanks for your help. We tried the way you said and it actually worked! But still, launching the same procedure again produced the same error. We made a function that does the following:

1)Function starts 2)instantiate database 3)Make queries, do things 4)call plugin (we print the data extracted from database) 5)function ends

Launching this function twice in a row (waiting for the first call to complete) produces the same error as described in the original post.

Thanks again.

cesarvr commented 7 years ago

cool , I think you can try this:

2)instantiate database (call this just once, and make it global) 1)Function starts
3)Make queries, do things 4)call plugin (we print the data extracted from database) 5)function ends

rastafan commented 7 years ago

We tried your suggestion, but the error still appears. Actually, it works as long as we stay inside the view where we generate the PDF (we are able to launch the generation function multiple times without error) but the error appears again when we change the view and try to access the DB again.

rastafan commented 7 years ago

Ok, we managed to solve this problem. We had some "openDatabase" calls we could not avoid in our framework that were breaking your suggested fix, so we had to modify the plugin code, and made a pull request here

Thanks for your help.