Chrome Extension WebSQL - need background & content pages to access same database, but they Don't
This is not possible, same thing as with localStorage
. Different scripts represent different contexts.
All extension scripts can communicate with background page though. Try creating a proxy API to the database on the background page. This should be simple enough.
I would implement it like this (content script):
chrome.extension.sendRequest({ method: 'executeSql', sql: 'SELECT title, author FROM docs WHERE id=?', params: [10] }, function(response) { //do stuff });
and on the other end (background page):
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) { console.log(request); if(request.method == 'executeSql' && request.sql) { db.readTransaction(function (t) { t.executeSql(request.sql, request.params, function (t, r) { //send result with sendResponse }, function (t, e) { //send error with sendResponse }); }); } else if(...) { //some other method etc. } ...}
Above code is not tested, it's just a sketch.