-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.js
More file actions
150 lines (132 loc) · 4.39 KB
/
main.js
File metadata and controls
150 lines (132 loc) · 4.39 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
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
initMain();
function initMain() {
var $loaderContainer = $("<div>").css({
'position': 'fixed',
'bottom': '5px',
'right': '5px',
'z-index': '999999999'
});
var $loaderSpinner = $('<div>').addClass('loader');
$loaderContainer.append($loaderSpinner);
browser.runtime.onMessage.addListener(requestHandler);
function requestHandler(request) {
return new Promise(function (resolve, reject) {
var response = {};
if (request.cmd == "getSelectedHtml") {
var selectedHtml = getHTMLOfSelection();
response = {
html: selectedHtml,
href: location.href
};
resolve(response);
} else if (request.cmd == "appendLoader") {
appendLoader();
resolve({});
} else if (request.cmd == "remoteInProcessNotification") {
showRemoteInProcessNotification();
resolve({});
} else if (request.cmd == "successNotification") {
showSuccessNotification(request.text, request.type, function (obj) {
resolve(obj);
});
} else if (request.cmd == "errorNotification") {
showErrorNotification();
resolve({});
} else if (request.cmd == "showModal") {
showModal(request.type);
resolve({});
}
});
}
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 && 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({
type: 1,
text: 'Your remote upload has begun.',
time: 4
});
}
function showSuccessNotification(text, type, callback) {
removeLoader();
copyTextToClipboard(text);
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({
text: confirmText,
submitText: 'Yes',
cancelText: 'No',
submitCallback: function () {
callback({success: true});
}
});
}
function showErrorNotification() {
removeLoader();
notie.alert({
type: 'error',
text: 'An error occured!',
time: 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({
text: label,
submitText: 'Process link(s) to Offcloud.com',
cancelText: 'Cancel',
rows: 5,
submitCallback: function (customLinks) {
if (customLinks && customLinks.trim() != "") {
chrome.runtime.sendMessage({
cmd: "custom",
html: customLinks,
type: type
});
}
}
});
}
function copyTextToClipboard(text) {
var copyFrom = $('<textarea/>');
copyFrom.text(text.replace("\n", "\r\n"));
$('body').append(copyFrom);
copyFrom.select();
document.execCommand('copy');
copyFrom.remove();
}
}