-
Notifications
You must be signed in to change notification settings - Fork 0
/
test2.js
150 lines (147 loc) · 6.72 KB
/
test2.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
const Popup = {
settings: {},
async sendBG(data:any) {
let respond = await chrome.runtime.sendMessage(data);
chrome.runtime.lastError;
return respond;
},
async init() {
this.$loginPanel = $('#logged-in');
this.$homePanel = $('#logged-in #initial-state');
this.$progressPanel = $('#logged-in #running-state');
this.$downloadPanel = $('#logged-in #download-state');
this.$logoutPanel = $('#logged-out');
this.sendBG({ cmd: "getNumOfConns" }).then((totalConnections:any) => {
this.settings.totalConnections = totalConnections;
this.manageTemplates();
})
this.settings = await this.sendBG({ cmd: "gettingSettings" });
//this.settings.isInProgress = true;
//this.settings.isCollectedDone = true;this.settings.connections=[1,2,3];
console.log(this.settings);
this.manageTemplates();
this.$homePanel.find('#collect-btn').click(() => {
if (this.settings.totalConnections) {
this.settings.isInProgress = true;
this.settings.isCollectedDone = false;
this.sendBG({ cmd: "startProcess", settings: { isCollectedDone: false, isInProgress: true } }).then((settings:any) => {
this.settings.isInProgress = true;
this.manageTemplates();
})
}
});
this.$progressPanel.find("#cancel-progress").click(() => {
this.settings.isInProgress = false;
this.settings.isCollectedDone = true;
this.sendBG({ cmd: "updateSettings", settings: { isCollectedDone: true, isInProgress: false } }).then((settings:any) => {
this.settings = settings;
this.manageTemplates();
})
});
this.$downloadPanel.find("#cancel-download").click(() => {
this.settings.isInProgress = false;
this.settings.isCollectedDone = false; this.settings.connections = [];
this.sendBG({ cmd: "updateSettings", settings: { isCollectedDone: false, connections: [], isInProgress: false } });
this.manageTemplates();
});
let _this = this;
this.$downloadPanel.find('#btn-download').click(function () {
$(this).prop("disabled", true).html("Please wait...");
setTimeout(() => _this.downloadCSV($(this)), 800)
});
},
enclosedFields: function (desc:any) {
var itemDesc
if (desc) {
itemDesc = desc.replace(/[^\\"]"/g, '\"')
itemDesc = itemDesc.replace(/(\r\n|\n|\r|\s+|\t| )/gm, ' ')
itemDesc = itemDesc.replace(/,/g, ',')
itemDesc = itemDesc.replace(/"/g, "'")
itemDesc = itemDesc.replace(/'/g, `'`)
itemDesc = itemDesc.replace(/’/g, '’')
itemDesc = itemDesc.replace(/ +(?= )/g, '')
} else {
itemDesc = ''
}
return itemDesc
},
downloadCSV(btn:any) {
var csv_data = "linkedinFullName,firstName,lastName,fullName,publicIdentifier,uniqueProfileID,headline,profileUrl,profilePhoto,connectedOn,additionalNote\n";
var nil = "NIL";
for (let record of this.settings.connections) {
let epochTime = record.connectedOn,
userLocale = navigator.language,
date = new Date(epochTime),
options = { timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone },
formattedDate = date.toLocaleDateString(userLocale, options);
csv_data += '"' + this.enclosedFields(record.originalFullName) + '"';
csv_data += ',';
csv_data += '"' + this.enclosedFields(record.firstName) + '"';
csv_data += ',';
csv_data += '"' + this.enclosedFields(record.lastName) + '"';
csv_data += ',';
csv_data += '"' + this.enclosedFields(record.fullName) + '"';
csv_data += ',';
csv_data += '"' + this.enclosedFields(record.publicIdentifier) + '"';
csv_data += ',';
csv_data += '"' + record.uniqueProfileID + '"';
csv_data += ',';
csv_data += '"' + this.enclosedFields(record.headline) + '"';
csv_data += ',';
csv_data += '"' + this.enclosedFields(record.profileUrl) + '"';
csv_data += ',';
csv_data += '"' + this.enclosedFields(record.profilePhoto) + '"';
csv_data += ',';
csv_data += `${formattedDate}`;
csv_data += ',';
csv_data += '"' + this.enclosedFields(record.additionalNote) + '"';
csv_data += '\r\n';
}
let fileName = "connections_" + (new Date()).toISOString().substring(0, 10) + "-" + (new Date()).toLocaleTimeString();
let link = document.createElement("a");
document.body.appendChild(link);
link.download = fileName + ".csv"
link.href = window.webkitURL.createObjectURL(new Blob([csv_data.replace(/,\s*$/, "")], { type: 'text/csv;charset=utf-8,%EF%BB%BF;' }));
link.click()
document.body.removeChild(link);
setTimeout(() => (btn.prop("disabled", false).html("Download Connections"), this.$downloadPanel.find("#cancel-download").click()), 500)
},
manageTemplates() {
this.$loginPanel.hide();
this.$logoutPanel.hide();
this.$homePanel.hide();
this.$progressPanel.hide();
this.$downloadPanel.hide();
if (this.settings.hasLiAt) {
this.$loginPanel.show();
this.$logoutPanel.hide();
} else {
this.$loginPanel.hide();
this.$logoutPanel.show();
return;
}
$('#profilePic').attr("src", this.settings.loggedInUser.profilePicture + "&" + (+new Date()))
if (this.settings.isInProgress) {
this.$progressPanel.show();
this.$progressPanel.find("#collecting-label").html(`Collecting ${this.settings.totalConnections} connections.`);
} else {
if (this.settings.isCollectedDone && this.settings.connections.length) {
this.$downloadPanel.show();
this.$downloadPanel.find("#collected-label").html(`${this.settings.totalConnections} connections collected.`);
} else {
this.$homePanel.show();
}
}
},
updateProgress(progress:any) {
$('.progress-panel .progress-bar').css({ width: progress + "%" })
}
}
$(() => Popup.init());
chrome.runtime.onMessage.addListener((message, sender, respond) => {
console.log(message)
switch (message.cmd) {
case 'updateUI': Popup.settings = message.settings; Popup.manageTemplates(); break;
case 'updateProgress': Popup.updateProgress(message.progress);
}
});