This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

The comments section of this article has been added to the lucky draw luxury package, see the details at the end of the article ~

preface

Teaching a man to fish is better than teaching a man to fish, we have a lottery code in hand, why not figure out how to write this thing

I forgot to pull down the lottery code on the Google Browser on the company computer because I ran away with the bucket, and now I have accumulated a huge amount of mine (1W). At the same time, I am touched by it. Unfortunately, without the function of Aite, I can only name and criticize

Fetch what?

The script I used earlier, which simulates fetch() for a request, works like an Ajax asynchronous request for data, but fetch() returns a Promise object

As you can see from reading the FAQ and workarounds used in fetch, fetch() is a new feature of JS, meaning there is a risk that it will not be recognized because of the browser version

Attach a generic request template

fetch(url [, config]).then(function(response) {
 // For the first time, we return a wrapped Promise object, which we throw into the then to deconstruct the value the Promise carries
  return response.json();
}).then(function(data) {
 // The value returned by the request is processed here
  console.log(data);
}).catch(function(e) {
 // Catch the exception in the request here
  console.log("Oops, error");
});
Copy the code

Raffle request analysis

After logging in on PC, open our lottery page, open F12, click on the network TAB, clear the excess request data, and limit the request type to FETCH /XHR

Click on the draw, after I get my own draw link https://api.juejin.cn/growth_api/v1/lottery/draw?aid=2608&uuid=6911658697077982728

Then, we replace the fetch template above with this URL, and throw the console to run the test to see what happens

This request is in POST format, so let’s add some configuration and continue to submit

If we are not logged in, take a closer look at the parameters in the original HTTPS request.

Would you like to right-click Replay XHR and find that you can copy the request or even copy as fetch?

Then things are simple, we make a synchronous request based on the fetch data with await, wrap a layer around it and iterate until the request fails. At the same time, we wrap the code block with {} to close the scope of our custom variables

Results the following

The finished product source

{
  let flag = true;
  for (; flag;) {
    await fetch("https://api.juejin.cn/growth_api/v1/lottery/draw?aid=2608&uuid=7016487434494658063", {
      "headers": {
        "accept": "* / *"."accept-language": "zh-CN,zh; Q = 0.9"."content-type": "application/json"."sec-ch-ua": "\"Chromium\"; v=\"94\", \"Google Chrome\"; v=\"94\", \"; Not A Brand\"; v=\"99\""."sec-ch-ua-mobile": "? 0"."sec-ch-ua-platform": "\"Windows\""."sec-fetch-dest": "empty"."sec-fetch-mode": "cors"."sec-fetch-site": "same-site"
      },
      "referrer": "https://juejin.cn/"."referrerPolicy": "strict-origin-when-cross-origin"."body": "{}"."method": "POST"."mode": "cors"."credentials": "include"
    }).then(function(response) {
      return response.json();
    }).then(data= > {
      if (data.err_msg == 'success') {
        console.log(data.data.lottery_name);
      } else {
        flag = false; }}}})Copy the code

use

Open F12 directly to the console to throw on the line, Google Browser support to save as a code block, next time you need to use, directly right-click the corresponding script run

I’m not sure if XDM’s AID and UUID are universal, so I’d rather just go through the request and manually replace the URL


Feel nice XDM to the comment section of the message support ya ~

Please feel free to discuss in the comments section. The nuggets will draw 100 nuggets in the comments section after the diggnation project. See the event article for details