-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-attach.js
More file actions
executable file
·75 lines (67 loc) · 2.89 KB
/
github-attach.js
File metadata and controls
executable file
·75 lines (67 loc) · 2.89 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
//TODO: Do nothing if the user is not signed in to Github!
var comments = document.getElementsByClassName('comment');
for (var commentIndex = 0; commentIndex < comments.length; commentIndex++) {
addUploadButton(comments[commentIndex], 'minibutton');
}
var newComment = document.getElementsByClassName('timeline-new-comment')[0];
addUploadButton(newComment, 'button');
function addUploadButton(commentDiv, buttonClass) {
var uploadInput;
var button;
var buttonContainer;
var commentTextArea;
var formActions = commentDiv.getElementsByClassName('form-actions')[0];
if (!formActions) {
return;
}
buttonContainer = formActions.getElementsByTagName('button')[0].parentNode;
commentTextArea = commentDiv.getElementsByClassName('comment-form-textarea')[0];
uploadInput = document.createElement("input");
uploadInput.setAttribute("style", "display:none");
uploadInput.type = "file";
uploadInput.addEventListener('change', function(event) {
//TODO: Move authentication to extension configuration
chrome.runtime.sendMessage({Action: "GetToken"}, function(response)
{
var file = event.target.files[0];
var reader = new FileReader();
reader.readAsBinaryString(file);
reader.onload = function(e) {
var contentType = file.type || 'application/octet-stream';
var base64Data = btoa(reader.result);
var fileData = {
Data: base64Data,
Type: contentType,
Name: file.name
};
chrome.runtime.sendMessage({Action: "UploadFile", File: fileData}, function(response) {
commentTextArea.value = response.Result;
});
};
});
/* var comment = 'Implement file upload (' + uploadInput.value + ')';
commentTextArea.value = commentTextArea.value ? commentTextArea.value + '\n' + comment : comment; */
});
buttonContainer.insertBefore(uploadInput, buttonContainer.firstChild);
button = createUploadButton(buttonClass);
button.addEventListener("click", function() {
//TODO: use JQuery
var clickEvent = document.createEvent('MouseEvents');
clickEvent.initEvent('click', true, true);
uploadInput.dispatchEvent(clickEvent);
});
buttonContainer.insertBefore(button, buttonContainer.firstChild);
}
function createUploadButton(buttonClass) {
button = document.createElement("input");
button.type = "button";
button.value = "Upload File...";
button.setAttribute("class", buttonClass);
return button;
}
function get(url, callback) {
var x = new XMLHttpRequest();
x.onload = x.onerror = function() { callback(x.responseText); };
x.open('GET', url);
x.send();
}