Last active
October 3, 2017 15:04
-
-
Save brianpow/077be67b2eb134e910551fab4c284264 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//https://www.mchk.org.hk/english/list_register/list.php?&type=L | |
(function($) { | |
var parse = function($$) { | |
return $$.find("table[cellpadding=4] tr").map(function(i) { | |
if (i === 0) return "" | |
let $td = $("td", this) | |
let data; | |
if ($td.eq(0).attr("headers") === "qualification") { | |
data = ["", "", "", "", "", | |
$td.eq(0).find("span:eq(0)").get(0).childNodes[0].data, | |
($td.eq(0).find("span:eq(0)").get(0).childNodes.length > 2) ? $td.eq(0).find("span:eq(0)").get(0).childNodes[2].data : "", | |
$td.eq(2).text() | |
] | |
} else { | |
data = [ | |
$td.eq(0).text().trim(), | |
($td.eq(1).find("a:eq(0)").get(0).childNodes.length) ? $td.eq(1).find("a:eq(0)").get(0).childNodes[0].data : "", | |
($td.eq(1).find("a:eq(0)").get(0).childNodes.length > 2) ? $td.eq(1).find("a:eq(0)").get(0).childNodes[2].data : "", | |
($td.eq(3).find("span:eq(0)").get(0).childNodes.length) ? $td.eq(3).find("span:eq(0)").get(0).childNodes[0].data : "", | |
($td.eq(3).find("span:eq(0)").get(0).childNodes.length > 2) ? $td.eq(3).find("span:eq(0)").get(0).childNodes[2].data : "", | |
($td.eq(5).find("span:eq(0)").get(0).childNodes.length) ? $td.eq(5).find("span:eq(0)").get(0).childNodes[0].data : "", | |
($td.eq(5).find("span:eq(0)").get(0).childNodes.length > 2) ? $td.eq(5).find("span:eq(0)").get(0).childNodes[2].data : "", | |
$td.eq(7).text() | |
] | |
} | |
return data.join("\t") | |
}).filter(function() { | |
return this != "" | |
}).get() | |
} | |
var findNext = function($$) { | |
console.log("findNext") | |
let $a = $$.find("a.paginate:last") | |
return ($a.text().indexOf("Next") !== -1) ? $a.attr("href") : ""; | |
} | |
var failed = function() { | |
console.error("Error occured!") | |
show() | |
} | |
var loaded = function(data) { | |
console.log("loaded") | |
let $data = $(data) | |
rows = rows.concat(parse($data)) | |
let nextUrl = findNext($data) | |
if (nextUrl) { | |
console.log("Processing " + nextUrl) | |
$.get(nextUrl, loaded).fail(failed) | |
} else | |
show(rows) | |
} | |
var show = function(result) { | |
console.log("show") | |
if (!$("#result").size()) | |
$("<textarea id=result />").css({ | |
width: "100%", | |
height: "100px" | |
}).prependTo(document.body) | |
$("#result").html( | |
headers.join("\t") + "\n" + rows.join("\n") | |
) | |
} | |
var headers = ["ID", "Name", "Name (Eng)", "Address", "Address (Eng)", "Qualification", "Qualification (Eng)", "Year"], | |
rows = [] | |
$.ajaxSettings.beforeSend = function(xhr) { | |
xhr.setRequestHeader('X-Requested-With', { | |
toString: function() { | |
return ''; | |
} | |
}); | |
}; | |
rows = rows.concat(parse($(document.body))) | |
nextUrl = findNext($(document.body)) | |
if (nextUrl) { | |
console.log("Processing " + nextUrl) | |
$.get(nextUrl, loaded).fail(failed) | |
} else | |
show(rows) | |
})(jQuery) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment