Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

137
Visualizações
How to read Blob synchronously in Javascript?

I'm writing a chrome extension to intercept WebSocket traffic, by hooking the function WebSocket.send, like:

var _send = WSObject.send
WSObject.send = function (data) {
    arguments[0] = my_hook_function(data)
    _send.apply(this, arguments)
}

function my_hook_function(data) {
    // how to read the data here synchronously?
    return data
}

The my_hook_function should be synchronous. The type of data is Blob, I can't find a synchronous API for reading the Blob. Google says there is FileReaderSync but I tried in console and this class does not exist.

How to solve it?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There isn't a synchronous FileReader in client-side JavaScript. And you can't make it synchronous either. So instead you should make everything async.

Use the regular FileReader object and wrap it inside of a Promise. By using async / await you can wait for the file reader to be finished before continuing and sending over the value.

var _send = WSObject.send
WSObject.send = async function(data) {
  const result = await my_hook_function(data);
  _send.call(this, result);
};

function my_hook_function(data) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.onload = ({ target }) => resolve(target.result);
    reader.onerror = error => reject(error);
    reader.readAsText(data);
  });
}

Sidenote: If WSObject.send expects a single argument, then the .call method will suffice. It's similar to .apply with the only difference that .call expects one or more arguments to pass, and .apply an array of arguments.

Also, try to avoid the arguments object and use rest parameters if possible.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda