承诺支持 Chrome 扩展 API? [英] Promise support for Chrome Extensions API?

查看:23
本文介绍了承诺支持 Chrome 扩展 API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去几周我一直在编写一些浏览器扩展程序,直到今天我认为 Firefox 的 WebExtension 应该可以在 Chrome 中自动运行.所以我试着按照 Mozilla 的例子写我的代码.
但是今天我意识到 Chrome 扩展的 API 文档中没有提到 Promises.
我在所有扩展的代码中都严格使用了 Promise.

I've been writing some browser extensions in the last few weeks and until today I thought that a WebExtension for Firefox should work pretty much automatically in Chrome. So I tried to write my code according to Mozilla's examples.
But today I realized that there is no mention of Promises in the API documentation for Chrome Extensions.
I have strictly used Promises throughout the code for all my Extensions.

那么现在我的问题是,我的代码可以在 Chrome 中运行吗?或者如果我在最顶部添加一个 var browser = chrome 声明会起作用吗?
还是 Chrome 根本不支持 API 上的 Promises?
如果 Chrome 尚不支持 API 函数上的 Promises,将来会支持它们吗?

So now my question is, will my code work in Chrome? Or would it work if I add a var browser = chrome declaration at the very top?
Or does Chrome not support Promises on the API at all?
If Chrome doesn't support Promises on the API functions yet, will it support them in the future?

注意,我知道这个项目:https://github.com/mozilla/webextension-填充物
但是我不愿意经历将那个库包含在任何地方的麻烦.此外,它还有令人讨厌的错误.

Note, I am aware of this project: https://github.com/mozilla/webextension-polyfill
But I'm not willing to go through the hassle of including that library everywhere. Also, it has annoying bugs in it.

此外,我没有 Chrome 或 Chromium,出于隐私和安全原因,我无法安装它们.

And besides that I don't have Chrome or Chromium and I can't install them for privacy and security reasons.

他们终于开始实现对 promise 的支持.

推荐答案

官方承诺支持终于来了,并且已经在一些 API 上实现了.在撰写本文时:

在谷歌浏览器的 Github已经可以找到使用带有承诺的 chrome.tabs API 的 Hello world 示例(等待):

chrome.runtime.onInstalled.addListener(async () => {
  let url = chrome.runtime.getURL("hello.html");
  let tab = await chrome.tabs.create({ url });
  console.log(`Created tab ${tab.id}`);
});

如您所见,promise 支持已简单地在 chrome 对象上实现,该对象现在同时支持 Promise 和回调.Firefox 仍然使用 browser 对象作为 promise API.

As you can see, the promise support has simply been implemented on the chrome object, which now supports both Promises and callbacks. Firefox still uses the browser object for the promise API.

这篇关于承诺支持 Chrome 扩展 API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆