5

油猴中使用 GM_xmlhttpRequest 的诡异情况

 3 years ago
source link: https://www.v2ex.com/t/782499
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  JavaScript

油猴中使用 GM_xmlhttpRequest 的诡异情况

  zhuzhuaini · 4 小时 25 分钟前 · 245 次点击

此 BUG 复现率百分之一百

代码如下:

for(var i=0;i<10;i++){

GM_xmlhttpRequest({

                          url: "http://www.qq.com/",
                          
                          responseType: "document",
                          
                          headers:{'overrideMimeType': 'text/html;charset=gb2312'},
                          
                          onload:
                          
                      function(res){
                      
                          console.log(i)
                          
                          },
                          
                });}
                

我在油猴中使用此代码,但是输出并不是理想情况:

按代码执行来说 应该进入 For 循环后 访问 qq,获取到 QQ 的源代码然后打印当前循环次数,然后自增 i,进入下一次循环

理想情况打印的内容应该为 0-1-2....-9

但事实却是只会输出 10 个 10. 如图: https://www.hualigs.cn/image/60c0b3a27dd2c.jpg

而我如果稍加修改 将输出语句写在 GM_xmlhttpRequest 外面,for 循环的里面,则没有问题,如图: https://www.hualigs.cn/image/60c0b4086faa3.jpg

所以我认为就是 GM_xmlhttprequest 的问题,但是我不清楚这是 bug 还是什么别的原因导致的,有大佬知道吗?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK