-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.js
216 lines (191 loc) · 5.99 KB
/
app.js
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
var app = new Vue({
el: '#app',
data: function() {
return {
messages: [],
perPage: perPage,
currentPage: 1,
navNum: 2
}
},
mounted: function() {
var that = this;
axios.get('WhatsApp_Chat.txt')
.then(function (res) {
//console.log(res.data);
var data = res.data;
data = data.replace(/\u200E/g, ""); // Left-To-Right Mark
var lines = data.split(/(\n|\r)/);
// Debug: Import kürzen
//lines = lines.slice(0, 200)
var message;
lines.forEach(function(line) {
line = line.replace(/(\r\n|\n|\r)/gm, "").trim();
if(line.length == 0) { return; }
var newMessage = /^\[[0-9]{2}\.[0-9]{2}\.[0-9]{2}, [0-9]{2}:[0-9]{2}:[0-9]{2}.*/.test(line);
if(newMessage && message) {
that.messages.push(message);
}
if(newMessage) {
message = {};
message.lines = [];
// Timestamp
var tmpTimestamp = line.match(/^\[([0-9]{2}\.[0-9]{2}\.[0-9]{2}, [0-9]{2}:[0-9]{2}:[0-9]{2})\]/);
if(tmpTimestamp) {
message.timestamp = tmpTimestamp[1];
message.date = parseWhatsAppDate(tmpTimestamp[1]);
if(that.messages.length > 1) { // Ab der zweiten Nachricht
message.newDay = !that.sameDay(that.messages[that.messages.length-1].date, message.date)
}
}
}
// Special Chars
line = line.replace(/</g, '<');
// Media
line = line.replace(/([a-zA-Z0-9-]+\.jpg)\ <angeh�ngt>/g, '<div class="media"><a href="media/$1" target="blank"><img src="media/$1" /></a></div>');
line = line.replace(/([a-zA-Z0-9-]+\.jpg)\ <angehängt>/g, '<div class="media"><a href="media/$1" target="blank"><img src="media/$1" /></a></div>');
line = line.replace(/\<Anhang: ([a-zA-Z0-9-]+\.jpg|webp|gif)\>/g, '<div class="media"><a href="media/$1" target="blank"><img src="media/$1" /></a></div>');
line = line.replace(/([a-zA-Z0-9-]+\.jpg|webp|gif) (Datei angehängt)/g, '<div class="media"><a href="media/$1" target="blank"><img src="media/$1" /></a></div>');
line = line.replace(/\<Anhang: ([a-zA-Z0-9-]+\.mp4)\>/g, '<div class="media"><video src="media/$1" controls=""></div>');
line = line.replace(/\<Anhang: ([a-zA-Z0-9-]+\..+)\>/g, '<div class="media">Media: <a href="media/$1" target="blank">$1</a></div>');
// Username
var tmpUsername = line.match(/^\[[0-9]{2}\.[0-9]{2}\.[0-9]{2}, [0-9]{2}:[0-9]{2}:[0-9]{2}\] (.+?):/);
if(tmpUsername) {
message.username = tmpUsername[1];
message.me = (myNames.indexOf(tmpUsername[1]) > -1);
}
// Message
var tmpMessage = line.match(/^\[[0-9]{2}\.[0-9]{2}\.[0-9]{2}, [0-9]{2}:[0-9]{2}:[0-9]{2}\] .+?: (.*)/);
if(tmpMessage) {
// First Line of message with meta
line = tmpMessage[1];
}
// link URLs
line = line.autoLink()
// following lines without meta
message.lines.push(line);
});
// Add Last Message
that.messages.push(message);
if(jumpToLastPage) {
that.currentPage = that.pageCount
}
})
.catch(function (error) {
// handle error
console.log(error);
})
window.addEventListener('wheel', function(e) {
if(!e.shiftKey) {
return;
}
if(e.deltaY / 120 < 0) {
that.pageChange('previous');
} else {
that.pageChange('next');
}
})
window.addEventListener('keydown', function(e) {
if(e.shiftKey) {
return;
}
console.log(e.which)
switch(e.which) {
case 37:
that.pageChange('previous');
break;
case 39:
that.pageChange('next');
break;
default: return; // exit this handler for other keys
}
});
},
methods: {
pageChange(value) {
console.log('pageChangeHandle', value);
switch (value) {
case 'next':
if(this.currentPage + 1 <= this.pageCount) {
this.currentPage += 1
}
break
case 'previous':
if(this.currentPage - 1 > 0) {
this.currentPage -= 1
}
break
default:
if(value > 0 && value <= this.pageCount) {
this.currentPage = value
}
}
},
changePerPage(n) {
console.log('changePerPage', n);
if(n == -1) {
if(confirm("Achtung! \n\nDiese Aktion ist sehr CPU intensiv und kann in seltenen Fällen als Nebenwirkung ein schwarzes Loch erzeugen. \n\nNe, aber das kann bei >10.000 Nachrichten etwas dauern oder den Browser crashen.") ) {
this.perPage = -1
}
}else{
this.perPage = n
}
if(jumpToLastPage) {
this.currentPage = this.pageCount
}
},
sameDay(a,b) {
return a.toDateString() === b.toDateString()
}
},
computed: {
messagesCount() {
return this.messages.length
},
pageCount() {
return Math.ceil(this.messagesCount / this.perPage)
},
pagedMessages() {
if(this.perPage == -1) { return this.messages; }
var end = this.currentPage*this.perPage;
var start = end-this.perPage;
return this.messages.slice(start, end)
},
messagesDates() {
return this.messages.map(function (message) {
return message.date;
})
},
statsFirstMessageDate() {
return parseWhatsAppDate(this.messages[0].timestamp);
},
statsLastMessageDate() {
return parseWhatsAppDate(this.messages[this.messages.length-1].timestamp);
},
statsTimespan() {
var timespanMin = this.statsLastMessageDate.getTime() - this.statsFirstMessageDate.getTime();
var timespanHours = timespanMin / (1000 * 3600 * 24);
return Math.round(timespanHours)
}
},
filters: {
formatDate(value) {
return value.toLocaleDateString(undefined, {year: 'numeric', month: '2-digit', day: '2-digit', })
},
formatDateDivider(value) {
return value.toLocaleDateString(undefined, {weekday: 'long', year: 'numeric', month: '2-digit', day: '2-digit', })
}
}
});
function range(start, stop, step) {
return Array.from({ length: (stop - start) / step + 1}, (_, i) => start + (i * step));
}
function parseWhatsAppDate(dateString) {
var d = dateString.match(/^(\d+).(\d+).(\d+), (\d+):(\d+):(\d+)/)
d.shift()
d= d.map(function (elm) {
return parseInt(elm)
})
d = new Date(2000+d[2], d[1]-1, d[0], d[3], d[4], d[5])
return d;
}