-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathess_ets_decrappify.js
More file actions
77 lines (60 loc) · 2.37 KB
/
Copy pathess_ets_decrappify.js
File metadata and controls
77 lines (60 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
var records = $("#grid1").data('igGrid').dataSource.settings.dataSource.Records;
records = records.slice(0,4);
extendAllRecords(records).then(console.log);
function extendAllRecords(recordList) {
// Caution : Might make the servers catch fire if run
var fakeDoc = initShadowDOM();
return records.reduce(function(promiseAccumulator, record) {
return promiseAccumulator.then(function(extendedRecords){
return extendRecord(record, fakeDoc).then(function(extendedRecord) {
extendedRecords.push(extendedRecord);
return extendedRecords;
}).catch(function(e) {
console.warn(e);
return extendedRecords;
});
})
}, Promise.resolve([]));
}
function extendRecord(record, fakeDoc) {
// Defensive copy, we're not altering the original
record = Object.assign({}, record);
var jqPromise = $.get("https://see.etsmtl.ca/Poste/" + record["GuidString"]).then(function(pageHtml) {
pageHtml = $.trim(pageHtml);
var jqObj = $(pageHtml, fakeDoc);
record.desc = jqObj.find(".divBoiteBleu").text().trim();
record.extraInfo = detailsToKeys(jqObj);
// Only body really needs to be cleared between parses.
$(fakeDoc).find("body").empty();
return record;
});
return new Promise(function(resolve, reject) { jqPromise.then(resolve, reject) })
}
function detailsToKeys(jqDocument) {
var individualKeys = $.map(jqDocument.find(".ligneInfo"), function(domLine) {
var lineDivs = jqDocument.find(domLine).children("div");
if (lineDivs.length === 2) {
var key = $(lineDivs[0]).text().trim().replace(':', '').toLowerCase().replace(/[^\w]/g, '_');
var val = $(lineDivs[1]).html().trim();
var result = {};
result[key] = val;
return result;
}
});
return individualKeys.reduce(function(accumulator, nextItem) {
// Somehow, `arrayODicts.reduce(Object.assign, {})` doesn't work
return Object.assign(accumulator, nextItem);
}, {})
}
function initShadowDOM() {
// Create shadow DOM (Mirorring jQuery's internal implementation)
// https://github.com/jquery/jquery/blob/2d4f53416e5f74fa98e0c1d66b6f3c285a12f0ce/src/core/parseHTML.js#L32
context = document.implementation.createHTMLDocument( "" );
// Set the base href for the created document
// so any parsed elements with URLs
// are based on the document's URL (gh-2965)
base = context.createElement( "base" );
base.href = document.location.href;
context.head.appendChild( base );
return context;
}