-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
59 lines (54 loc) · 1.84 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
var latestPastes = [];
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){
if(message.method == "latestPastes")
{
sendResponse(JSON.stringify(latestPastes));
return true;
}
});
chrome.extension.onMessage.addListener(
function(request, sender, sendResponse){
if(request.action === "prefs") {
var prefsString = localStorage.prefs;
if(prefsString === undefined) {
sendResponse(undefined);
} else {
sendResponse(JSON.parse(localStorage.prefs));
}
}
}
);
function upload_to_upaste()
{
chrome.tabs.executeScript(null, {code:"var stored = window.getSelection().toString(); stored"}, function(stored){
var prefs = JSON.parse(window.localStorage.getItem("prefs"));
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://upaste.me/api", true);
xhr.responseType = "json";
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 && xhr.status == 200 && xhr.response.status != "error") {
var url = document.createElement('textarea');
document.body.appendChild(url);
url.value = xhr.response.paste.link;
url.focus();
url.select();
latestPastes.push(url.value);
document.execCommand("Copy");
url.remove();
if(latestPastes.length >= prefs.latestPastesDisplaySize)
latestPastes.shift();
}
else if(xhr.readyState == 4 && xhr.status == 200 && xhr.response.status == "error") {
alert("Hold your horses, you're a bit too quick with all that uploading.");
}
}
xhr.send("expire="+(prefs.expire == 262800 ? 0 : prefs.expire)+"&privacy="+prefs.privacy+"&api_key="+prefs.api_token+"&json=true&paste="+stored);
});
}
chrome.contextMenus.create({
title: "Upload to uPaste",
contexts:["selection"],
onclick: upload_to_upaste
});
chrome.browserAction.setPopup({popup: "popup.html"});