2014-08-19 00:50:30 +00:00
|
|
|
chrome.commands.onCommand.addListener(function(command){ // Keyboard shortcut trigger - Shorten current tab
|
|
|
|
if(command == "shortenTab"){
|
|
|
|
chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
|
|
|
|
var current = tabs[0]
|
|
|
|
shortenTabURL(current.id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
chrome.browserAction.onClicked.addListener(function(tab){ // Shorten current tab when lobli icon pressed
|
|
|
|
chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
|
|
|
|
var current = tabs[0]
|
|
|
|
shortenTabURL(current.id);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function showAlert(text){
|
|
|
|
var opt ={
|
|
|
|
type: "basic",
|
|
|
|
title: "lob.li Chrome",
|
|
|
|
message: text,
|
|
|
|
iconUrl: "../icons/lobli-128.png"
|
|
|
|
}
|
|
|
|
chrome.notifications.create('lobli-cr', opt, function(id){});
|
|
|
|
}
|
|
|
|
|
|
|
|
function copyToClipboard(text){
|
|
|
|
var clipboard = document.getElementById("clipboard");
|
|
|
|
clipboard.value = text;
|
|
|
|
clipboard.focus();
|
|
|
|
clipboard.select();
|
|
|
|
document.execCommand("copy");
|
|
|
|
}
|
|
|
|
|
|
|
|
function testURL(s) { // Stolen from http://stackoverflow.com/a/17726973 since I suck at RegExp - Returns true(bool) on good looking link
|
|
|
|
var regexp = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
|
|
|
|
return regexp.test(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
function shortenTabURL(tabid){ // Use just a tab id to shorten its url
|
|
|
|
chrome.tabs.get(tabid, function(tab){
|
|
|
|
shortenURL(tab.url);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function shortenURL(url){ // Creates a short url and copies it to clipboard
|
|
|
|
if(testURL(url)){
|
2014-08-29 21:54:26 +00:00
|
|
|
sendAPIRequest("?shorten&url=" + url, function(req){
|
2014-08-19 00:50:30 +00:00
|
|
|
var res = req.responseText.trim();
|
2014-08-29 15:22:37 +00:00
|
|
|
switch(res){
|
|
|
|
case "dead":
|
|
|
|
showAlert("Apparently the link is dead...");
|
|
|
|
break;
|
|
|
|
case "db":
|
|
|
|
showAlert("I got a database error!");
|
|
|
|
break;
|
|
|
|
case "Error":
|
|
|
|
showAlert("General Error.");
|
|
|
|
break;
|
|
|
|
default:
|
2014-08-29 21:54:26 +00:00
|
|
|
copyToClipboard("http://b.lob.li/?"+res);
|
2014-08-29 15:22:37 +00:00
|
|
|
showAlert("Link shortened. Short link copied to clipboard!");
|
|
|
|
break;
|
2014-08-19 00:50:30 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function resolveURL(url){ // For when/if I decide to add the ability to resolve links through the extension
|
|
|
|
if(testURL(url)){
|
2014-08-29 21:54:26 +00:00
|
|
|
sendAPIRequest("?resolve&url=" + url, function(req){
|
2014-08-19 00:50:30 +00:00
|
|
|
var res = req.responseText.trim();
|
|
|
|
copyToClipboard(res);
|
|
|
|
showAlert("Link Resolved!\n" + res);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function sendAPIRequest(url, callback){ // Sends a GET request to the server, response is expected to be text and only short id, or resolved link
|
|
|
|
var method = "GET";
|
|
|
|
var req = new XMLHttpRequest();
|
2014-08-29 21:54:26 +00:00
|
|
|
req.open(method, "http://b.lob.li/ch/" + url, true);
|
2014-08-19 00:50:30 +00:00
|
|
|
req.onload = function(){
|
|
|
|
callback(req);
|
|
|
|
};
|
|
|
|
req.send();
|
|
|
|
}
|