これは不可能です。と同じですlocalStorage
。異なるスクリプトは異なるコンテキストを表します。
ただし、すべての拡張スクリプトはバックグラウンドページと通信できます。バックグラウンドページでデータベースへのプロキシAPIを作成してみてください。これは十分に単純なはずです。
私はそれを次のように実装します(コンテンツスクリプト):
chrome.extension.sendRequest({
method: 'executeSql',
sql: 'SELECT title, author FROM docs WHERE id=?',
params: [10]
},
function(response) {
//do stuff
}
);
もう一方の端(背景ページ):
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.
} ...
}
上記のコードはテストされていません。単なるスケッチです。