chrome.webrequest.onCompleted vs. chrome.runtime.onMessage race

时间:2015-05-03 19:20:20

标签: javascript google-chrome google-chrome-extension race-condition

我的Chrome扩展程序为public String MysteryDeck(){ for(int i =0; i<12; i++){ System.out.println("Why");<--this prints if(p.PlayerTurn()) { // here System.out.print(" Player 1 Type 1 to pick from a deck higher than the MysteryCard and Type 2 to draw from a deck lower."); ans1 = scan.nextInt(); if(ans1 ==1) { // here System.out.println("You have chosen to pick from cards between"+MysteryCard+" and 13"); for(int k= 0; k < cardDraw1.size(); k++) { // here newHighCard = rand.nextInt(13-MysteryCard)+(MysteryCard+1); cardDraw1.add(newHighCard); } // here for(int v=0; v<cardDraw1.size(); v++) {// here optional total1+=cardDraw1.get(v); } //here optional System.out.println(total1); } // here if(ans1== 2) { // here System.out.println("You will draw from a deck 1 to "+MysteryCard); for(int k= 0; k<cardDraw1.size();k++) { // here newLowCard = rand.nextInt(MysteryCard+1)+1; cardDraw1.add(newLowCard); } // here for (int v=0; v<cardDraw1.size(); v++) { // here optional total1+=cardDraw1.get(v); } // here optional System.out.println(total1); } // here } // here if(!p.PlayerTurn()) { // here System.out.println("Player 2 Type 1 to pick from a deck higher than the MysterCard and type 2 to draw from a deck lower than the MysteryCard."); ans2=scan.nextInt(); if(ans2 ==1) { // here System.out.println("You have chosen to pick from cards between "+MysteryCard+" and 13"); for(int k= 0; k<cardDraw2.size(); k++) { // here newHighCard = rand.nextInt(13-MysteryCard)+(MysteryCard+1); cardDraw2.add(newHighCard); } // here for(int d = 0; d<cardDraw2.size(); d++) { // here optional total2 +=cardDraw2.get(d); } // here optional System.out.println(total2); } // here if(ans2== 2) { // here System.out.println("You will draw from a deck 1 to "+MysteryCard); for(int k= 0; k<cardDraw2.size(); k++) { // here newLowCard = rand.nextInt(MysteryCard+1)+1; cardDraw2.add(newLowCard); } // here System.out.println(cardDraw2); }//here }// here } return (""); } ,其中收集并存储了有关页面的信息,

background.js

并提供给要求它的任何其他扩展部分(例如,内容脚本),

chrome.webRequest.onCompleted.addListener(
  function(details) {
  // compute a page hash etc, store it
  tabToHash[details.tabId] = hash;
  },
{
  urls: ['*://*/*.pdf'],
  types: ['main_frame']
}
);

chrome.runtime.onMessage.addListener( function(request, sender, sendResponse) { if (request.getInfo) { sendResponse({ hash: tabToHash[sender.tab.id], }); } }); onMessage完成之前触发onCompleted时会出现问题。

如何避免这种竞争条件?

编辑: 发送初始消息的内容脚本可能包含

chrome.runtime.sendMessage(
  {getInfo: true},
  function(response) {
  // do something with the response
  }
);

可以插入manifest.json作为

"content_scripts": [
    {
      "matches": ["*://*/*.pdf"],
      "css": ["content.css"],
      "js": ["scripts/content.js"]
    }
  ]

1 个答案:

答案 0 :(得分:1)

  • 通过添加
  • 跟踪内容的加载状态
chrome.webRequest.onBeforeRequest.addListener(
  function(details) {
    tabStatus[details.tabId] = 'loading';
  },
  {
    urls: ['*://*/*.pdf'],
    types: ['main_frame']
  }
);

tabStatus[details.tabId] = 'complete';

chrome.webRequest.onCompleted.addListener

的末尾
  • 有条件地推迟onMessage听众的回复:
chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if (request.getInfo) {
      if (tabStatus[sender.tab.id] === 'complete') {
        // send immediately
        sendResponse({
          data: myData[sender.tab.id]
        });
      } else {
        // send later
        responseSender[sender.tab.id] = sendResponse;
        // returning `true` to indicate that we intend to send later,
        // cf. <https://developer.chrome.com/extensions/runtime#event-onMessage>
        return true;
      }
    }
  });
  • responseSender[sender.tab.id]
  • 的末尾调用存储的chrome.webRequest.onCompleted.addListener
if (responseSender[details.tabId]) {
  responseSender[details.tabId]({
    data: myData(details.tabId)
  });
  responseSender[details.tabId] = null;
}