简体   繁体   English

For循环同步Javascript注入

[英]For Loop of Synchronous Javascript Injections

Currently, I am attempting to perform injections of a certain script for a variable amount of times in order (synchronously). 目前,我正在尝试按顺序(同步)执行一定数量的特定脚本注入。 Put simply, I want to be able to inject script xyz.js for Z amount of times. 简而言之,我希望能够注入脚本xyz.js达Z次。

I currently have the code to inject the script a hard-coded amount of times. 我目前拥有将脚本注入硬编码次数的代码。 This code looks like such: 此代码如下所示:

chrome.tabs.executeScript(tab.ib,{file: "inject.js"}, function(resp){
    chrome.tabs.executeScript(tab.ib,{file: "inject.js"}, function(resp){
    });
});

With this method, I simply nest the injections into the callback functions of the previous injections. 使用这种方法,我只是将注入嵌套到先前注入的回调函数中。 However, this method of synchronously calling multiple injections must be hard-coded in. As a result, I cannot perform this synchronous injection sequence a variable amount of times. 但是,必须对这种同步调用多次注入的方法进行硬编码。结果,我不能多次执行此同步注入序列。

How can I change my code to allow me to inject the code synchronously a variable amount of times? 如何更改代码以允许我以可变的次数同步注入代码?

Note: These injections are injecting the same file because it is executing the content differently depending upon the webpage's conditions. 注意:这些注入是注入同一个文件,因为它根据网页的条件以不同的方式执行内容。 Also note that it must be synchronous because the file's behavior depends upon the previous file injection. 还要注意,它必须是同步的,因为文件的行为取决于先前的文件注入。 Finally, note that it cannot be simply combined into one large file because of how the program runs. 最后,请注意,由于程序的运行方式,不能简单地将其组合成一个大文件。

You could use recursion for this. 您可以为此使用递归。

(function loop(n) {
  if(n > 1) chrome.tabs.executeScript(tab.ib,{file: "inject.js"}, function(resp){
    loop(n - 1);
  });
})(10);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM