-
Notifications
You must be signed in to change notification settings - Fork 4
/
popup.html
executable file
·87 lines (74 loc) · 1.7 KB
/
popup.html
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
<style>
body{
min-width: 100px;
overflow-x: hidden;
color: #999;
}
input {
border: 1px solid #ccc;
color: #484848
}
a {
font-size: 1.2em;
color: #5D5D5D;
padding: 3px;
}
</style>
<script>
// get url to be shortened
// .getSelected is apparently asynchronous
chrome.tabs.getSelected(null, function(tab){
if (localStorage['url_service'] == undefined){
failFunction();
}
else{
var status = document.createElement("span");
var req = new XMLHttpRequest();
status.innerHTML = "Generating...";
status.setAttribute("id", "status");
document.body.appendChild(status);
// request to url server
req.open(
"GET",
"http://"+localStorage['url_service']+escape(tab.url),
true
);
// do things when the request gets returned
req.onload = function() {
var url = req.responseText;
var status = document.getElementById("status");
if (url == ""){
status.innerHTML = "Service Error<br>";
failFunction();
}
else if (url.substr(0, 4) != "http"){
status.innerHTML = url;
}
else {
// create input stuff
var input = document.createElement("input");
input.setAttribute("value", url)
// add to body
document.body.appendChild(input);
// copy to clipboard
input.focus();
input.select();
document.execCommand('Copy');
var status = document.getElementById("status");
status.innerHTML = "Copied To Clipboard<br>";
}
}
// woo! do shit!
req.send(null);
}
function failFunction(){
var fail = document.createElement("a");
fail.innerHTML = "Set Shortener";
fail.href = "#";
fail.onclick = function(){
chrome.tabs.create({"url": chrome.extension.getURL('options.html')});
}
document.body.appendChild(fail);
}
});
</script>