1

根据此处的文档,browser.pageAction.getTitle()

是一个返回 Promise 的异步函数。

所以我试图在我的代码中实现它的方式是在这样的函数内部:

function title(t){
  if(t===undefined){
    try{
      let t=await browser.pageAction.getTitle({tabId:c.id});
    }
    catch(err){
      try{
        let t=await browser.browserAction.getTitle({tabId:c.id});
      }
      catch(err){
        console.log("Failed to get title. This add-on depends on the button's title.");
      }
    }
    return t;
  }
  else{
    try{
      browser.pageAction.setTitle({tabId:c.id,title:t});
    }
    catch(err){
      try{
        browser.browserAction.setTitle({tabId:c.id,title:t});
      }
      catch(err){
        console.log("Failed to set title. This add-on depends on the button's title.");
      }
    }
  }
  console.log("Setting button title: "+t);
}

这只是我的错误还是乍一看似乎很明显,即文档错误(未更新)?

4

1 回答 1

1

只需在您的函数前加上async

async function title(t){
    await browser.pageAction.getTitle({tabId:c.id});
}
于 2018-10-01T05:06:27.260 回答