在阻断chrome.webRequest.onBeforeSendHeaders监听异步操作 [英] Asynchronous operation in blocking chrome.webRequest.onBeforeSendHeaders listener

查看:4541
本文介绍了在阻断chrome.webRequest.onBeforeSendHeaders监听异步操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Chrome扩展,并由于对同步和异步API的混合发展遇到的问题。

I'm developing a Chrome extension, and have run into an issue due to developing against a mixture of synchronous and asynchronous apis.

chrome.webRequest.onBeforeSendHeaders.addListener(function(details) {
  //code that modifies details.requestHeaders
  //..
  return {requestHeaders: details.requestHeaders};
},
{urls: ["<all_urls>"]},
["blocking", "requestHeaders"]

在监听的功能,我想从索引资料获取数据和修改基于此数据的请求头。 IndexedDB的只有一个异步API,而听者的合同要求它是同步的。

Inside the listener function I want to fetch data from IndexedDB and modify the request headers based on this data. IndexedDB only has an asynchronous API, while the contract for the listener requires it to be synchronous.

任何想法我怎么能解决这个问题?

Any ideas how I could solve this problem?

推荐答案

一个DB调用通常是太昂贵了。这就是说,这里是一个想法,把我的头顶部(这可能是可怕的):

A DB call is generally too expensive. That said, here is one idea off the top of my head (which might be horrible):


  • 在最频繁使用的数据内存缓存保持的。使用某种类型的简单的数据结构像一个地图

  • pre-填充地图时,应用程序加载与任何数据是最有可能是必要的。做到这一点,当扩展后台页面加载。

  • 在适当的时候运行生命周期内定期更新缓存的内容。从应用程序的后台页面做到这一点,通过报警触发的时间表更新。注册后台页面加载/应用程序启动时报警。

  • 查询在onBeforeSendHeaders存储器映射。地图查找是同步的,快速的。如果查找的作品,伟大的。如果失败,认为错误处理机制(如果你把这个错误)。

  • 当缓存未命中时,触发一个异步调用(您不需要等待解决),最终将记录缓存未命中,以另一种数据结构。您使用的code定期更新地图的其它相同的结构。

  • 当高速缓存命中时,触发一个异步呼叫,增加的值保留在地图中的机会,并且稍微减小对其他项目的机会在高速缓存(或许是隐含在增大)。

  • 请不要忘记修剪在后台缓存更新缓存中的项目不再可能

  • 实验与产生不俗的性能和合理的内存使用量的高速缓存大小。

话虽这么说,你需要考虑的高速缓存未命中的情况下的用户体验。也许提供一个默认的参数,或某种莫明通知小姐的用户的占位符值。它种取决于你想如何应用工作,哪些应用呢,你没有说明。

That being said, you would need to think of the user experience in the event of a cache miss. Maybe supply a default parameter, or a placeholder value of some sort that notifies the user of the miss somehow. It kind of depends on how you want the app to work and what the app does, which you did not state.

呵呵,DERP,并考虑在存储器映射使用本地存储为...咄。

Oh, derp, and consider using localStorage as the in memory map... Duh.

这篇关于在阻断chrome.webRequest.onBeforeSendHeaders监听异步操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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