我正在尝试在 chrome.tabs.update() 回调中使用 cursor.continue() 。我总是收到以下错误。
DOM IDBDatabase 异常 0 错误:针对当前不活动或已完成的事务发出请求。
Error in event handler for 'undefined': TransactionInactiveError: DOM IDBDatabase Exception 0 Error: A request was placed against a transaction which is either currently not active, or which is finished.
at chrome-extension://fiipdmhnjimefhdbdfpgllkckomakfkh/sample.js:62:20
at miscellaneous_bindings:288:9
at chrome.Event.dispatchToListener (event_bindings:390:21)
at chrome.Event.dispatch_ (event_bindings:376:27)
at chrome.Event.dispatch (event_bindings:396:17)
at Object.chromeHidden.Port.dispatchOnMessage (miscellaneous_bindings:254:22) event_bindings:380
chrome.Event.dispatch_ event_bindings:380
chrome.Event.dispatch event_bindings:396
chromeHidden.Port.dispatchOnMessage
代码:
//background.js
store = getObjectStore(DB_STORE_NAME, 'readwrite');
var req;
req = store.count();
req.onsuccess = function(evt) {
console.log('<p>There are <strong>' + evt.target.result +
'</strong> record(s) in the object store.</p>');
// store = getObjectStore(DB_STORE_NAME, 'readwrite');
};
req.onerror = function(evt) {
console.error("add error", this.error);
// store = getObjectStore(DB_STORE_NAME, 'readwrite');
//displayActionFailure(this.error);
};
var i = 0;
req = store.openCursor();
req.onsuccess = function(evt) {
cursor = evt.target.result;
//store = getObjectStore(DB_STORE_NAME, 'readwrite');
// If the cursor is pointing at something, ask for the data
if (cursor) {
//cursor.advance(i);
console.log("rol cursor:", cursor);
req = store.get(cursor.key);
req.onsuccess = function (evt) {
var value = evt.target.result;
chrome.tabs.update(cTab.id,{url:value.uri,active:true},function(t){
console.log(value.uri,value.path);
chrome.tabs.executeScript(t.id,{file:"/lib/jquery-ui-1.8.6/js/jquery-1.9.0.min.js",runAt:"document_end"},function() {
chrome.tabs.executeScript(t.id, { code:"var jClaw = jQuery.noConflict();jClaw('html, body').animate({scrollTop:jClaw('"+value.path+"').offset().top}, 2000);jClaw('"+value.path+"').css({background:'yellow'},1000);",runAt:"document_end"},function(){
chrome.tabs.sendMessage(t.id,value.path,function(response){
cursor.continue();
});
});
//cursor.update(cursor.value);
});
//cursor.update(cursor.value);
//chrome.tabs.sendMessage(t.id,"scrollTo");
});
};
// Move on to the next object in store
//cursor.continue();
//cursor.update(cursor.value);
// This counter serves only to create distinct ids
i++;
} else {
console.log("No more entries");
}
};
我尝试了各种方法来保持交易开放。但是没能成功。
function getObjectStore(store_name, mode) {
var tx = dbp.transaction(store_name, mode);
tx.oncomplete = function(e){
console.log("Transaction Complete");
};
tx.onabort = function(e){
console.log("Transaction Aborted");
};
tx.onerror = function(e){
console.log("Transaction Error");
};
//tx.onsuccess=keepAlive;
return tx.objectStore(store_name);
}
如您所见,我想遍历索引数据库并使用 URL 来填充页面。然后我想注入脚本来使用消息传递来获取一些文本。但是,如果我将 cursor.continue() 放在回调之外,它只是移动到下一个 URL,因为 chrome.tabs.update 是异步的。
有人可以在这里帮助我吗?