forked from ylon/onetab-merge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge-html
executable file
·111 lines (101 loc) · 3.15 KB
/
merge-html
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env node
const usage = 'file...'
const onetab_dateformats = ['M-D-Y, h:m:s A', 'Y-M-D HH:m:s', 'D/M/Y, HH:m:s']
if (process.argv.length == 2) {
console.log('Usage:', usage)
process.exit()
}
var cheerio = require('cheerio');
var fs = require('fs-extra-promise');
var moment = require('moment');
merge_html_files(process.argv.slice(2))
function merge_html_files(files) {
// return console.log(files);
try {
var onetabs = files.map(file => load_html(file))
// onetabs.forEach(onetab => console.log(onetab.summary))
var wins = []
for (let onetab of onetabs) {
for (let win of onetab.windows) {
// win.file = onetab.file
wins.push(win)
}
}
wins.sort((a, b) => a.created - b.created)
// for (let win of wins) console.log(win.created, win.file);
// // for (let win of wins) console.log(win);
// console.log(wins.length);
console.log(JSON.stringify(wins));
}
catch (e) {
console.error(e);
}
}
function load_html(file) {
// console.log('load', file);
var html = fs.readFileSync(file)
var $ = cheerio.load(html)
var onetab = {
file: file,
windows: [],
}
$('#contentAreaDiv > div').each((idx, div) => {
if (div.children.length != 3) return;
var createdText = $(':first-child :first-child :nth-child(5) :first-child', div).text().replace('Created ', '')
var created = null;
var dtformat = onetab_dateformats.find(format => {
var m = moment(createdText, format)
if (!m.isValid()) return false
created = m.toDate()
let y = created.getFullYear()
if (!(y > 2010 && y < 2020)) return false
return true
})
if (!dtformat) created = null
if (!created) {
console.error('unfound date format', createdText);
return;
}
var window = {
created: created,
tabs: [],
}
onetab.windows.push(window)
$(':nth-child(2) a', div).each((idx, a) => {
a = $(a)
window.tabs.push({
url: a.attr('href'),
title: a.text().trim(),
})
})
})
onetab.summary = {
file: file,
windows: onetab.windows.length,
tabs: onetab.windows.reduce((count, w) => count + w.tabs.length, 0),
}
return onetab;
}
// function merge(onetabs) {
// var map = {}
// for (let onetab of onetabs) {
// for (let window of onetab.windows) {
// let id = window.created.toISOString() + ' ' + onetab.file
// let tabs = map[id]
// if (!tabs) {
// tabs = []
// map[id] = tabs
// }
// for (let tab of window.tabs) {
// tabs.push(tab)
// }
// }
// }
// for (let id of Object.keys(map).sort().reverse()) {
// console.log();
// // console.log('\n' + id);
// for (let tab of map[id]) {
// console.log(tab.url + ' | ' + tab.title);
// }
// }
// }