chrom脚本12306抢票

1.制作Chrome扩展程序:新建配置文件 manifest.json,配置如下:

{
    "manifest_version": 2,
    "name": "Chrome抢票脚本",
    "version": "1.0",
    "description": "chrome 抢票脚本示例",
    "browser_action": {
      "default_icon": "icon.ico"
    },
    "content_scripts": [
      {
        "matches": ["https://kyfw.12306.cn/otn/leftTicket/init?linktypeid=dc&fs=%E5%B9%BF%E5%B7%9E,GZQ&ts=%E9%95%BF%E6%98%A5,CCT&date=2020-12-11&flag=N,N,Y"],
        "js": ["script.js"]
      },
      {
        "matches": ["https://kyfw.12306.cn/otn/confirmPassenger/initDc"],
        "js": ["script2.js"]
      }
    ]
}

2.新建script.js文件:

window.onload = function() {
    var date = new Date();
    if (date.getHours() >= 12) {
        // 此处为相应页面的抢票按钮,请自行获取dom元素
        var button = document.getElementsByClassName('btn72')[1];
        console.log(button)

        if (button!='') {
            // 可抢票,点击抢票
            button.click();
        } else {
            // 不可抢票,刷新页面
            setTimeout(function() {
                window.location.reload();
            }, 500);
        }
    } else {
        // 未到设定抢票时间,进入下一轮循环
        setTimeout(function() {
            window.location.reload();
        }, 500);
    }
}

3.新建一个script2.js

window.onload = function() {
    var label = document.getElementsByClassName('check')[0];//check btn92s
    console.log(label) 
    label.click();
    var button = document.getElementById('submitOrder_id');
    console.log(button)
    button.click();
}

4.content_scripts的matches链接数组要根据实际情况来粘贴。新建一个文件夹script,将manifest.json,script.js,icon.png都放入此文件夹中。最后打开Chrome->菜单->更多工具->扩展程序->加载已解压的扩展程序,然后找到script文件的路径,点击确认,此时,打开对应的网页就可以发现正在运行此脚本。若script有更新,则在扩展程序页面点击重新加载即可。

你可能感兴趣的:(chrom脚本12306抢票)