How can i abort/cancel a HTTP request caused by Javascript code?

做~自己de王妃 提交于 2019-12-24 21:24:36

问题


I have this code to get a captcha from 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);

This line send an img request to the server that cause changing of the captcha value

ctx.drawImage(cap,0,0);

I tried to use XMLHttpRequest.abort() but didn't work

Note that I'm using the Chrome console. I appreciate any help!


回答1:


I don't know how this worked but i add ?=0 after /ORegMx/capito.png using

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"


来源:https://stackoverflow.com/questions/57865361/how-can-i-abort-cancel-a-http-request-caused-by-javascript-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!