但是也有一些有效的不同的方法,所以挑你喜欢/一与您的代码最适合。
使用promise.then()
function fetchOAuth2()
{
var redirectURL = browser.identity.getRedirectURL() + "page.html";
console.log(redirectURL);
var clientID = "CLIENT ID";
var authURL = "https://trakt.tv/oauth/authorize"; // API I'm using
authURL += `?response_type=code`;
authURL += `&client_id=${clientID}`;
authURL += `&redirect_uri=${redirectURL}`;
console.log(authURL);
browser.identity.launchWebAuthFlow({
interactive: true,
url: authURL
})
.then(function(authResult) {
// do something with authResult
})
}
如果您要访问的authResult这个功能之外,那么你就需要返回的承诺,并从调用点then
它,即
function fetchOAuth2()
{
var redirectURL = browser.identity.getRedirectURL() + "page.html";
console.log(redirectURL);
var clientID = "CLIENT ID";
var authURL = "https://trakt.tv/oauth/authorize"; // API I'm using
authURL += `?response_type=code`;
authURL += `&client_id=${clientID}`;
authURL += `&redirect_uri=${redirectURL}`;
console.log(authURL);
return browser.identity.launchWebAuthFlow({
interactive: true,
url: authURL
})
}
fetchOAuth2().then(function(authResult) {
// do something with auth result
});
使用ES7异步/等待
这是一个相对较新的语言功能,但它在FF54中受支持。如果您需要支持其他浏览器,则必须使用Babel才能兼容。
async function fetchOAuth2()
{
var redirectURL = browser.identity.getRedirectURL() + "page.html";
console.log(redirectURL);
var clientID = "CLIENT ID";
var authURL = "https://trakt.tv/oauth/authorize"; // API I'm using
authURL += `?response_type=code`;
authURL += `&client_id=${clientID}`;
authURL += `&redirect_uri=${redirectURL}`;
console.log(authURL);
var authResult = await browser.identity.launchWebAuthFlow({
interactive: true,
url: authURL
});
// do something with authResult
}
非常感谢您的快速回复!并感谢您的多种选择+信息,alw ays很高兴知道!不幸的是,它似乎没有办法。可能是一个webextension的东西(因为browser.identity.launchWebAuthFlow()打开一个新窗口),但由于某种原因,这些解决方案都没有工作。我总是在等待foo.then(console.log(“test”))之后进行测试,但没有控制台输出。有任何想法吗? – FStijn
检查控制台中的错误,但使用Ctrl + J打开** browser **控制台,以便您也可以看到扩展中的错误。 – trincot
@trincot我得到的是: 在btgallery.js返回语句后无法访问的代码:1:52329(不是我的,据我所知) ReferenceError:事件未定义pop.js:4:6264(不是我的据我所知) 我自己的应用程序,不幸的是 – FStijn