This repository has been archived by the owner on Jun 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
173 lines (149 loc) · 5.32 KB
/
background.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
chrome.extension.onMessage.addListener(function(msg, sender, sendMessage) {
switch(msg.req) {
case "shorten":
shortenTabURL(msg.tabid);
break;
case "screenshot":
screenshotTab(msg.tabid);
break;
case "savefile":
saveFile(msg.dataUrl, msg.filename, msg.tabid);
break;
case "shortenurl":
shortenURL(msg.linkUrl, msg.tabid);
break;
default:
return;
}
sendMessage("OK");
});
function showAlert(text) {
var notification = window.webkitNotifications.createNotification('foxcaves-32.png', 'foxCaves', text);
notification.show();
window.setTimeout(function() {
notification.cancel();
}, 5000);
}
function copyToClipboard(text) {
var clipboard = document.getElementById("clipboard");
clipboard.value = text;
clipboard.focus();
clipboard.select();
document.execCommand("copy");
}
function shortenTabURL(tabid) {
chrome.tabs.get(tabid, function(tab) {
shortenURL(tab.url, tabid);
});
}
function shortenURL(url, tabid) {
sendAPIRequest("shorten?" + url, function(req) {
copyToClipboard("https://fox.re/g" + req.responseText.trim());
showAlert("Link shortened. Short link copied to clipboard!");
}, tabid);
}
function saveURL(url, tabid) {
chrome.tabs.executeScript(tabid, {file: "loader.js"}, function(res) {
var x = url.lastIndexOf("/");
var filename = url.substr(x + 1);
x = url.indexOf("?");
if(x && x >= 0) filename = filename.substring(0, x - 1);
_setDisplayProgressText("Downloading from website...", 0.5, 0, tabid);
var req = new XMLHttpRequest();
req.responseType = "arraybuffer";
req.open("GET", url, true);
req.onload = function() {
if(req.status != 200) {
showAlert("Error downloading file to reupload");
return;
}
saveFile(req.response, filename, tabid, 0.5, 0.5);
};
req.addEventListener("loadstart", function(evt) { uploadStart(evt, 0.5, 0, tabid); }, false);
req.addEventListener("progress", function(evt) { uploadProgress(evt, 0.5, 0, tabid); }, false);
req.addEventListener("load", function(evt) { uploadComplete(evt, 0.5, 0, tabid); }, false);
req.send(null);
});
}
function saveFile(data, filename, tabid, progress_mult, progress_offset) {
sendAPIRequest("create?" + filename, function(req) {
if(req.status != 200) {
showAlert("Error: " + req.responseText);
return;
}
var file = req.responseText.split("\n");
var fileInfo = file[1].split(">");
var fileID = fileInfo[0];
copyToClipboard("https://fox.re/v" + fileID);
showAlert("File uploaded. Link copied to clipboard!");
}, tabid, "PUT", data, progress_mult, progress_offset);
}
function saveDataURL(dataURL, filename, tabid) {
var x = dataURL.lastIndexOf(",");
if((!x) || x < 0) x = dataURL.lastIndexOf(";");
var data = Base64Binary.decodeArrayBuffer(dataURL.substr(x + 1));
saveFile(data, filename, tabid);
}
function screenshotTab(tabid) {
chrome.tabs.captureVisibleTab(null, {format: "png"}, function(dataURL) {
chrome.tabs.getSelected(null, function(tab) {
var filename = tab.title + ".png";
saveDataURL(dataURL, filename, tab.id);
});
});
}
function _setDisplayProgressText(text, mult, offset, tabid) {
if(offset <= 0) offset = -1;
chrome.tabs.sendMessage(tabid, {progress: offset, text: text});
}
function _setDisplayProgress(progress, mult, offset, tabid) {
chrome.tabs.sendMessage(tabid, {progress: ((progress * mult) + (offset * 100))});
}
function uploadStart(evt, mult, offset, tabid) {
_setDisplayProgress(0, mult, offset, tabid);
}
function uploadComplete(evt, mult, offset, tabid) {
if(offset + mult >= 1) {
_setDisplayProgressText("foxCaves server is processing...", mult, offset + mult, tabid);
}
_setDisplayProgress(100, mult, offset, tabid);
}
function uploadProgress(evt, mult, offset, tabid) {
if(evt.lengthComputable) {
_setDisplayProgress((evt.loaded / evt.total) * 100.0, mult, offset, tabid);
}
}
function sendAPIRequest(url, callback, tabid, method, body, progress_mult, progress_offset, dontloadloader) {
if(!method) method = "GET";
if(!body) body = null;
if(!progress_mult) progress_mult = 1;
if(!progress_offset) progress_offset = 0;
if(progress_offset <= 0 && !dontloadloader) {
chrome.tabs.executeScript(tabid, {file: "loader.js"}, function(res) {
sendAPIRequest(url, callback, tabid, method, body, progress_mult, progress_offset, true);
});
return;
}
_setDisplayProgressText("Uploading to foxCaves...", progress_mult, progress_offset, tabid);
var req = new XMLHttpRequest();
req.open(method, "https://foxcav.es/api/" + url, true);
req.onload = function() {
if(req.status == 401) {
chrome.tabs.create({
url: "https://foxcav.es/login"
});
if(progress_mult + progress_offset >= 1) {
_setDisplayProgress(101, 1, 0, tabid);
}
return;
}
callback(req);
if(progress_mult + progress_offset >= 1) {
_setDisplayProgress(101, 1, 0, tabid);
}
};
req.upload.addEventListener("loadstart", function(evt) { uploadStart(evt, progress_mult, progress_offset, tabid); }, false);
req.upload.addEventListener("progress", function(evt) { uploadProgress(evt, progress_mult, progress_offset, tabid); }, false);
req.upload.addEventListener("load", function(evt) { uploadComplete(evt, progress_mult, progress_offset, tabid); }, false);
req.send(body);
}