-
Notifications
You must be signed in to change notification settings - Fork 1
/
content.js
58 lines (44 loc) · 1.51 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
document.addEventListener('contextmenu', function (e) {
var srcElement = e.srcElement;
var pathParts = window.location.href.split('/');
var protocol = pathParts[0];
var host = pathParts[2];
if (srcElement.nodeName == 'SPAN') {
pastClipboardData();
var githubLink = getClipBoardData();
$('#paste-container').remove();
var objectToSave = {
linkText: $(srcElement).text(),
url: protocol + '//' + host + '/' + $(srcElement).parent('a').attr('href').replace(/(\r\n|\n|\r)/gm),
description: $(srcElement).parents('td').siblings('th').find('span')[0].innerText.replace(/(\r\n|\n|\r)/gm,""),
copiedGithubLink: githubLink
};
chrome.runtime.sendMessage({
type: 'copy',
text: JSON.stringify(objectToSave)
});
}
}, false);
var pastClipboardData = () => {
// add a DIV, contentEditable=true, to accept the paste action
var helperdiv = document.createElement('div');
helperdiv.setAttribute("id", "paste-container");
document.body.appendChild(helperdiv);
helperdiv.contentEditable = true;
// focus the helper div's content
var range = document.createRange();
range.selectNode(helperdiv);
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
helperdiv.focus();
// trigger the paste action
document.execCommand("Paste");
}
var getClipBoardData = () => {
var clipBoardData = $('#paste-container').children().html()
var githubLink = 'PASTE_GITHUB_LINK_HERE';
if (clipBoardData && clipBoardData.includes('github.com')) {
githubLink = clipBoardData
}
return githubLink;
}