-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.js
More file actions
121 lines (105 loc) · 3.44 KB
/
main.js
File metadata and controls
121 lines (105 loc) · 3.44 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
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
initMain();
function initMain(){
var $loaderContainer = $("<div>").css({
'position': 'fixed',
'bottom': '5px',
'right': '5px',
'z-index':'999999999'
});
var $loaderSpinner = $('<div>').addClass('loader');
$loaderContainer.append($loaderSpinner);
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.cmd == "getSelectedHtml") {
var selectedHtml = getHTMLOfSelection();
sendResponse({
html: selectedHtml,
href: location.href
});
}
if (request.cmd == "appendLoader") {
appendLoader();
}
if (request.cmd == "remoteInProcessNotification") {
showRemoteInProcessNotification();
}
if (request.cmd == "successNotification") {
showSuccessNotification(request.type, function(obj) {
sendResponse(obj);
});
return true;
}
if (request.cmd == "errorNotification") {
showErrorNotification();
}
if (request.cmd == "showModal") {
showModal(request.type);
}
});
function getHTMLOfSelection() {
var range;
if (document.selection && document.selection.createRange) {
range = document.selection.createRange();
return range.htmlText;
} else if (window.getSelection) {
var selection = window.getSelection();
if (selection.rangeCount > 0) {
range = selection.getRangeAt(0);
var clonedSelection = range.cloneContents();
var div = document.createElement('div');
div.appendChild(clonedSelection);
return div.innerHTML;
} else {
return '';
}
} else {
return '';
}
}
function appendLoader() {
$loaderContainer.appendTo('body');
}
function removeLoader() {
$loaderContainer.remove();
}
function showRemoteInProcessNotification() {
removeLoader();
notie.alert(1, 'Your remote upload has begun.', 4);
}
function showSuccessNotification(type, callback) {
removeLoader();
var confirmText = "";
if (type == 0)
confirmText = 'Download links copied to clipboard. Open them in new tab?';
else if (type == 1)
confirmText = 'Transfer has started & links are copied. Open them in new tab?'
notie.confirm(confirmText, 'Yes', 'No', function() {
callback({
success: true
});
});
}
function showErrorNotification() {
removeLoader();
notie.alert('error', 'An error occured!', 4);
}
function showModal(type) {
var label = "";
if (type == 0)
label = 'Instant download custom links';
else if (type == 1)
label = 'Cloud download custom links';
else if (type == 2)
label = 'Remote download custom links';
notie.textarea({
rows: 5
}, label, 'Process link(s) to Offcloud.com', 'Cancel', function(customLinks) {
if (customLinks && customLinks.trim() != "") {
chrome.runtime.sendMessage({
cmd: "custom",
html: customLinks,
type: type
});
}
});
}
}