Skip to content

Instantly share code, notes, and snippets.

@Artoria2e5
Last active July 11, 2016 16:52
Show Gist options
  • Save Artoria2e5/8394259c4000f81f9e28 to your computer and use it in GitHub Desktop.
Save Artoria2e5/8394259c4000f81f9e28 to your computer and use it in GitHub Desktop.
it works..?
// ==UserScript==
// @name Train leaving
// @namespace https://gist.github.com/Artoria2e5/8394259c4000f81f9e28
// @updateURL https://gist.githubusercontent.com/Artoria2e5/8394259c4000f81f9e28/raw/train_leaving.js
// @version 0.3.0
// @description wuuuuuuuuuuuuuu
// @author You
// @match *://www.gtbyxx.com/wxg/station/jyx_station.jsp?code=*
// @match *://www.gtbyxx.com/wxg/station/crh_station.jsp?code=*
// @match *://218.29.99.82/WTINFOS/hccx.mor?method=list&station=*
// @grant Notification
// ==/UserScript==
/* jshint -W097 */
// https://developer.mozilla.org/en-US/docs/Web/API/notification
// http://www.gtbyxx.com/wxg/station/jyx_station.jsp?code=GZZ
// https://stackoverflow.com/a/901144/3770260
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
// https://stackoverflow.com/a/3067896/3770260
Date.prototype.yyyymmdd = function() {
var mm = this.getMonth() + 1; // getMonth() is zero-based
var dd = this.getDate();
return [this.getFullYear(), (!mm[1] && '0') + mm, (!dd[1] && '0') + dd].join('-'); // padding
};
function DomText(el){ return el ? (el.textContent || el.innerText || '') : ''; }
// fromUTC epoch
var tNow = new Date();
Notification.requestPermission();
if (window.location.hostname === 'www.gtbyxx.com') {
// 车站代码
var curr_station_code = getParameterByName("code");
// 车站名称
var curr_station_name = DomText(document.querySelector('body > div.nav.nav-bg > h1'));
} else if (window.location.hostname === '218.29.99.82') {
// 车站代码
var curr_station_code = getParameterByName("station");
// 车站名称
/* 取文字 顶上标题 删空格 删掉那几个字 */
var curr_station_name = DomText(document.querySelector('body > nav > h4')).trim().replace("候车资讯", "");
} else {
throw new RangeError("Hostname not acceptable.");
}
var trains = window.location.hostname === 'www.gtbyxx.com' ? document.getElementById('trainTable').children :
window.location.hostname === '218.29.99.82' ? document.getElementById('infoBody').children :
null;
for (var idx = 1; idx < trains.length; ++idx) {
var train = trains[idx];
// 也是凑巧,几列排得都一样。。。
var info = {
num: DomText(train.children[0]),
frm: DomText(train.children[1]),
dst: DomText(train.children[2]),
time: DomText(train.children[3]),
// 举一个分情况讨论的例子
gate: window.location.hostname === 'www.gtbyxx.com' ? DomText(train.children[4]) :
window.location.hostname === '218.29.99.82' ? DomText(train.children[5]) : '',
// 还可以继续取备注 blah..
};
// HACK: 手动补一下时间格式
var t2 = +(Date.parse(window.location.hostname === 'www.gtbyxx.com' ? tNow.getFullYear() + '-' + info.time.replace(' ', 'T') + ':00' :
// HACK: 默认了火车在当天,实际上应该另外扫一圈看有没有折回来(说明是下一天)
window.location.hostname === '218.29.99.82' ? tNow.yyyymmdd() + 'T' + info.time + ':00' :
"FAIL")) - 8 * 3600000;
// 可以有一点提前量……
if (t2 > (+tNow)) {
setTimeout(
(function(info){
// 前面可以候车地点之类的用过来……
return new Notification(curr_station_name + ':' + info.num + ' ' + info.frm + '→' + info.dst,
{"body": info.time + ' ' + '你本来写了好多'});
}),
t2 - (+tNow),
info
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment