return Request Array XMLHttpRequest

1

I am developing an extension for the Chrome browser.

When a tab is opened or updated, a content.js (a JavaSCript) is injected through chrome.tabs.executeScript through a background.js

What the content.js does is:

  • search all the JavaScripts of the new tab.
  • Analyze the scripts.

To analyze JavaScripts you need its content.

If the scripts are embedded directly in the HTML, it already has its content.

However, for scripts that are not embedded in HTML, which have a "src" tag, for example, an external or local Jquery has to download its content.

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

For this I use XMLHttpRequest.

What I need at the end is to have the content of all the scripts on the tab ... But I have asynchrony problems.

function getScriptContent(src){
   //Aqui lanzo el XMLHttpRequest.
   ...
}

//Scripts es un Array de scripts de la pestaña
function obtenerContenido(scripts){
     for (i = 0; i < scripts.length; i++) {
            rcScript = scripts[i].src;
            if (srcScript != ""){
                  let content=null;
                  content = getScriptContent(srcScript);
                  if(content==null) continue;
                  //Aquí hago otras cosas.
                  //Finalmente añado el contenido a un array.
             } else {
                  //añado el script inscrustrado al array.
             }

// Al finalizar todo el proceso.
// Envio al background el array completo con todos los scripts y su contenido.

There are several problems: - XMLHttpRequest: It is so synchronous so when I do the if (content == null) continue it is always fulfilled since gettingContent () has not yet obtained the content.

  • Executing the scripts follows its path. So when I'm going to send it to the background of the array with all the scripts these have not yet been inserted into the array.

  • To send the scripts to the background I have to be sure that all the scripts are added. Both those that I have to download their content through XMLHttpRequest as those that are added directly because they are embedded in the HTML.

How can I solve this?

Greetings. Thank you very much!

    
asked by Enrique 04.03.2018 в 01:16
source

0 answers