如何中止/取消由Javascript代码引起的HTTP请求?

时间:2019-09-10 06:45:40

标签: javascript jquery xmlhttprequest

我有这段代码可以从this website获取验证码

var imported = document.createElement('script');
imported.onload = function() {
  var str = document.querySelector("body > table.troisbords > tbody > tr > td > table > tbody > tr > td > div > blockquote > table > tbody > tr > td:nth-child(3) > table > tbody > tr:nth-child(2) > td > form > table > tbody > tr:nth-child(4) > td:nth-child(1)").innerHTML.length
  if (str <= 43) {
    var div = document.createElement("div");
    div.id = "divPage";
    var img = document.querySelector("body > table.troisbords > tbody > tr > td > table > tbody > tr > td > div > blockquote > table > tbody > tr > td:nth-child(3) > table > tbody > tr:nth-child(2) > td > form > table > tbody > tr:nth-child(4) > td:nth-child(1) > img");
    img.parentNode.insertBefore(div, img);
    div.appendChild(img);
  }

  html2canvas(document.querySelector("#divPage"), {
    onrendered: function(canvas) {
      var ctx = canvas.getContext("2d");
      var w = canvas.width;
      var h = canvas.height;
      ctx.fillStyle = "#FFF";
      ctx.fillRect(0, 0, w, h);
      var cap = document.querySelector("#divPage > img")
      ctx.drawImage(cap, 0, 0);
      var imgData = canvas.toDataURL('image/webp', 1.0);
      console.log(imgData);
    },
    useCORS: true,
    allowTaint: true,
    taintTest: false
  });
}

imported.src = 'https://pastebin.com/raw/dRap1YD8';
document.head.appendChild(imported);

此行将img请求发送到服务器,从而导致验证码值更改

ctx.drawImage(cap,0,0);

我尝试使用XMLHttpRequest.abort()但没有用

请注意,我正在使用Chrome控制台。感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我不知道这是如何工作的,但我在使用/ p的/ORegMx/capito.png之后添加

document.querySelector("body > table.troisbords > tbody > tr > td > table > tbody > tr > td > div > blockquote > table > tbody > tr > td:nth-child(3) > table > tbody > tr:nth-child(2) > td > form > table > tbody > tr:nth-child(4) > td:nth-child(1) > img").src="/ORegMx/capito.png?=0"