在回调中使用cursor.continue()

Pra*_*eep 0 javascript google-chrome google-chrome-extension indexeddb

我试图在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
Run Code Online (Sandbox Code Playgroud)

码:

//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");
  }
};
Run Code Online (Sandbox Code Playgroud)

我已经尝试了各种方法来保持交易开放.但无法成功.

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);
}
Run Code Online (Sandbox Code Playgroud)

如您所见,我想迭代索引数据库并使用URL填充页面.然后我想注入脚本以使用消息传递获取一些文本.但是,如果我将cursor.continue()放在回调之外,它只是转移到下一个URL,因为chrome.tabs.update是异步的.

有人可以帮我吗?

Kya*_*Tun 5

我的猜测chrome.tabs.update是异步功能.cursor.continue()必须立即调用,即不在异步update函数内.

另一点是你不需要store.get(cursor.key).由于您正在使用值游标,因此可以通过获取它的值cursor.value.