-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
39 lines (35 loc) · 1.04 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
function isValidScheme(aURL) {
var reg = new RegExp("^https?", "i");
return reg.test(aURL)
}
var Clipboard = {
copy: function(data) {
var textArea = document.createElement("textarea");
textArea.style.position = "absolute";
textArea.style.left = "-100%";
textArea.value = data;
document.body.appendChild(textArea);
textArea.select();
document.execCommand("Copy");
document.body.removeChild(textArea);
}
};
$(function(){
chrome.browserAction.onClicked.addListener(function(tab){
if(isValidScheme(tab.url)){
var backend = localStorage["backend"] || "bitly";
var shortener = new Shortener(backend);
shortener.shorten(tab.url, function(success, msg){
if(success){
Clipboard.copy(msg);
chrome.browserAction.setBadgeText({text:"done"});
} else {
chrome.browserAction.setBadgeText({text:"fail"});
}
window.setTimeout(function(){
chrome.browserAction.setBadgeText({text:""});
}, 1000);
});
}
});
});