-
Notifications
You must be signed in to change notification settings - Fork 5
/
content.js
executable file
·240 lines (197 loc) · 7.2 KB
/
content.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
const SERVER_GH = `https://github.com`;
const SERVER_GL = `https://gitlab.com`;
const GL_BASE_BRANCH = "js-target-branch";
const GL_COMMIT_MESSAGE = "form-control js-commit-message";
const GL_FILE_CONTENT = "ace_content";
const GL_FILE_NAME = "file_name";
const GL_FILE_PATH = "file_path";
const GL_MERGE_MESSAGE = "merge-message-edit";
const GL_PR_BRANCH = "js-source-branch";
const GH_BASE_BRANCH = "commit-ref css-truncate user-select-contain expandable base-ref";
const GH_COMMIT_MESSAGE = "commit-description-textarea";
const GH_COMMIT_SUMMARY = "commit-summary-input";
const GH_DPATH_LIST = "js-path-segment";
const GH_FILE_CONTENT = "form-control file-editor-textarea js-blob-contents js-code-textarea";
const GH_FILE_NAME = "form-control js-blob-filename js-breadcrumb-nav";
const GH_MERGE_MESSAGE = "merge_message_field";
const GH_MERGE_TITLE = "merge_title_field";
const GH_PR_BRANCH = "commit-ref css-truncate user-select-contain expandable head-ref";
var fileContents = {};
// Response to request from background script
chrome.runtime.onMessage.addListener(
function(message, sender, sendResponse) {
let {
commitType,
server
} = message;
if (server == SERVER_GH) {
sendResponse(dataExtractor_GH(commitType));
} else {
sendResponse(dataExtractor_GL(commitType));
}
});
// Extract the information from gitlab page
function dataExtractor_GL(commitType) {
if (commitType == "merge") { //merge commit
let baseBranch = document.getElementsByClassName(
GL_BASE_BRANCH)[0].innerText;
let prBranch = document.getElementsByClassName(
GL_PR_BRANCH)[0].innerText;
let commitMessage = document.getElementById(
GL_MERGE_MESSAGE).value;
return {
baseBranch,
prBranch,
commitMessage
};
} else { //regular commit
//TODO: Make sure if encode_utf8 is needed before passing content
let commitMessage = document.getElementsByClassName(
GL_COMMIT_MESSAGE)[0].value;
// delete
if (commitType == "delete") {
return {
commitMessage
};
}
// edit/new
else if ((commitType == "edit") || (commitType == "new")) {
let newPath = getFilePath_GL(commitType);
let fileContent = document.getElementsByClassName(
GL_FILE_CONTENT)[0].innerText;
return {
newPath,
fileContent,
commitMessage
};
}
// upload
// TODO: Remove the need for pre-click
// TODO: Support upload file through upload link
else {
var logFiles = function(files) {
$(files).each(function() {
var fname = this.name;
var reader = new FileReader();
reader.readAsBinaryString(this);
reader.onloadend = function() {
fileContents[fname] = reader.result;
}
});
}
/*$("a.markdown-selector").on("click", function(event) {
logFiles(this.files)
}).on("change", function(event) {
logFiles(this.files)
})*/
$("div.dropzone-previews.blob-upload-dropzone-previews").on("drop", function(event) {
logFiles(event.originalEvent.dataTransfer.files)
});
var fnames = Object.keys(fileContents);
return {
newPath: fnames[0],
fileContent: fileContents[fnames[0]],
commitMessage
};
return {
commitMessage
};
}
}
}
// Extract the information from github page
function dataExtractor_GH(commitType) {
if (commitType == "merge") { //merge commit
let baseBranch = document.getElementsByClassName(GH_BASE_BRANCH);
baseBranch = baseBranch[0].innerText;
let prBranch = document.getElementsByClassName(GH_PR_BRANCH);
prBranch = prBranch[0].innerText;
let commitTitle = document.getElementById(GH_MERGE_TITLE);
commitTitle = commitTitle.value;
let commitSummary = document.getElementById(GH_MERGE_MESSAGE);
commitSummary = commitSummary.value;
let commitMessage = `${commitTitle}\n\n${commitSummary}`;
return {
baseBranch,
prBranch,
commitMessage
};
} else { //regular commit
let commitDescription = document.getElementById(GH_COMMIT_MESSAGE);
commitDescription = commitDescription.value;
let commitSummary = document.getElementById(GH_COMMIT_SUMMARY);
let placeholder = commitSummary.placeholder;
commitSummary = commitSummary.value;
if (commitSummary.length < 1) commitSummary = placeholder;
let commitMessage = commitSummary + "\n" + commitDescription;
// delete
if (commitType == "delete") {
return {
commitMessage
};
}
// edit or new
else if ((commitType == "edit") || (commitType == "new")) {
let fileContent = document.getElementsByClassName(
GH_FILE_CONTENT)[0].value;
let fname = document.getElementsByClassName(
GH_FILE_NAME)[0].value;
let dirs = document.getElementsByClassName(GH_DPATH_LIST);
let newPath = getFilePath_GH(dirs);
newPath = `${newPath}/${fname}`;
return {
newPath,
fileContent,
commitMessage
};
}
// upload
// TODO:Remove the need for pre-click
else {
var logFiles = function(files) {
$(files).each(function() {
var fname = this.name;
var reader = new FileReader();
reader.readAsBinaryString(this);
reader.onloadend = function() {
fileContents[fname] = reader.result;
}
});
}
$("input[type=file]").on("change", function(event) {
logFiles(this.files)
});
$("file-attachment").on("drop", function(event) {
logFiles(event.originalEvent.dataTransfer.files)
});
// TODO: Get all uploaded files for GitHub
// NOTE: GitLab only allows to upload one file
var fnames = Object.keys(fileContents);
return {
newPath: fnames[0],
fileContent: fileContents[fnames[0]],
commitMessage
};
}
}
}
// Get file path in gitlab UI
function getFilePath_GL(commitType) {
let tag = commitType == "new" ? GL_FILE_NAME : GL_FILE_PATH;
return document.getElementById(tag).value;
}
// Get file path in github UI
function getFilePath_GH(dirs) {
// Capture changes in path by taking the upated path
let filePath = [];
for (let dir of dirs) {
filePath.push(dir.innerText)
}
// Remove anything before and after the path : REPO<path>REPLACEME
filePath.shift();
filePath.pop();
return filePath.join('/');
}
function encode_utf8(s) {
return unescape(encodeURIComponent(s));
}