1
0
mirror of https://github.com/gamaio/lobli.git synced 2025-08-14 02:29:05 +00:00

Chrome extension for lob.li.

Untested with the API
This commit is contained in:
alopexc0de
2014-08-18 20:50:30 -04:00
parent ea1a3a4c10
commit e395ac49e8
10 changed files with 159 additions and 0 deletions

View File

@@ -0,0 +1,82 @@
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)){
sendAPIRequest("shorten&url=" + url, function(req){
var res = req.responseText.trim();
if(res == "dead"){
showAlert("Apparently the link is dead...");
}else if(res == "db"){
showAlert("I got a database error!");
}else if(res == "Error"){
showAlert("General Error.");
}else{
copyToClipboard("http://lob.li/" + res);
showAlert("Link shortened. Short link copied to clipboard!");
}
});
}
}
function resolveURL(url){ // For when/if I decide to add the ability to resolve links through the extension
if(testURL(url)){
sendAPIRequest("resolve&url=" + url, function(req){
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();
req.open(method, "http://lob.li/ch/" + url, true);
req.onload = function(){
callback(req);
};
req.send();
}

View File

@@ -0,0 +1,15 @@
chrome.contextMenus.onClicked.addListener(function(info, tab) {
switch(info.menuItemId) {
case "shortenLink":
shortenURL(info.linkUrl, tab.id);
break;
case "shortenFile":
shortenURL(info.srcUrl, tab.id);
break;
}
});
chrome.runtime.onInstalled.addListener(function() {
chrome.contextMenus.create({"title": "Shorten link to this file with lob.li", "contexts": ["image", "video", "audio"], "id": "shortenFile"});
chrome.contextMenus.create({"title": "Shorten link with lob.li", "contexts": ["link"], "id": "shortenLink"});
});