写了个浏览器插件Tampermonkey脚本抢12306火车票的,却发现人家已经有类似的功能了!!!

freddon 发表于2018-12-28   阅读 2476 | 评论 0

😑,😔 记录下吧,虽然很简单
``` // ==UserScript== // @name 12306飘飘 // @namespace https://kyfw.12306.cn/ // @version 0.1 // @description I wanna go home! // @author Fred Don // @match https://kyfw.12306.cn/* // @grant none // ==/UserScript== (function() { 'use strict'; var $one = function (sel) { return document.querySelector(sel) }; var log=function(m){ console.log(m); }; var $all = function (sel) { return document.querySelectorAll(sel) }; HTMLElement.prototype.$one = function (sel) { return this.querySelector(sel) }; HTMLElement.prototype.$all = function (sel) { return this.querySelectorAll(sel) }; Function.prototype.wait=function(){setTimeout(this,50)} var initTeaser=function(){ var widget=document.createElement('div'); widget.innerHTML=(`<h3 id="h-teaser">O(∩_∩)O哈!</h3> <input type='button' value='停止' /> `); widget.style.position='fixed'; widget.style.backgroundColor='red'; widget.style.top='2rem'; widget.style.left='2rem' widget.style.right='2rem' widget.style.zIndex='6000' document.body.appendChild(widget) return widget; }; // Your code here... initTeaser(); function clickAnchor(aHref){ aHref && aHref.click(); } //step1 - page1 otn/leftTicket/init if(location.pathname.startsWith('/otn/leftTicket/init')){ log('/otn/leftTicket/init'); var step1=function(){ var goDateSpace=['7:00','10:00'];//max [] or ['0:00','23:59'] var goDateSpaceTimeSec=goDateSpace.map(function(item){ var seg=item.split(':') return seg[0]*60+seg[1]*1 }); log(`${goDateSpaceTimeSec}`); var checkTicket=function(){ var tbody=$one('#t-list tbody'); var btns=tbody.$all('tr td:last-child a[href]') log(`btns:${btns}`); btns.forEach(function(item){ var startAt = item.parentNode.parentNode.$one('.start-t').innerText; log(`startAt:${startAt}`); var seg=startAt.split(':'); var segVal=seg[0]*60+seg[1]*1; if(goDateSpace.length>0){ log(`segVal:${segVal}`); if(segVal>=goDateSpaceTimeSec[0] && segVal<=goDateSpaceTimeSec[1]){ log(`BREAK:`); clickAnchor(item); throw new Error('BREAK') } }else{ clickAnchor(item); throw new Error('BREAK') } }) }; checkTicket(); log(`step1 end`); }; try{ setTimeout(function(){ step1(); },200) }catch(e){ log('Err:'+e) } } //step2 otn/confirmPassenger/initDc else if(location.pathname.startsWith('/otn/confirmPassenger/initDc')){ log('/otn/confirmPassenger/initDc'); var step2=function(){ //选择乘客 var targetPassenger=['唐三','张五']; var seat_level=2; var seat=['E','F']; // A B C D E F 二等座 / A C D F 一等座 $all('.per-sel ul#normal_passenger_id li').forEach(function(item){ if(targetPassenger.indexOf(item.$one('label').innerText)>-1){ clickAnchor(item.$one('input[type=checkbox]')); } }); $one('#submitOrder_id').click() //选座 if(seat.length>0 && $one('#id-seat-sel')){ seat.forEach(function(item){ $one('.seat-list li a[id$='+item+']').click(); }); } //确认 $one('.dhtmlx_window_active') && $one('.dhtmlx_window_active #qr_submit_id').click(); }; try{ step2(); }catch(e){} } })(); ``` [tampermonkey下载地址](https://tampermonkey.net/)

分类 :日常记录

评论(0)


登录,才能发评论哦~