mirror of
https://github.com/gamaio/lobli.git
synced 2024-12-22 11:42:40 +00:00
Chrome extension for lob.li.
Untested with the API
This commit is contained in:
parent
ea1a3a4c10
commit
e395ac49e8
10
lob.li crx/LICENSE.txt
Normal file
10
lob.li crx/LICENSE.txt
Normal file
@ -0,0 +1,10 @@
|
||||
lobli Copyright (c) David Todd (c0de)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of David Todd (c0de) nor the names of his contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
11
lob.li crx/background.html
Normal file
11
lob.li crx/background.html
Normal file
@ -0,0 +1,11 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<title>lobli background</title>
|
||||
<script src="js/background.js"></script>
|
||||
<script src="js/contextmenu.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<textarea id="clipboard"></textarea>
|
||||
</body>
|
||||
</html>
|
BIN
lob.li crx/icons/lobli-128.png
Normal file
BIN
lob.li crx/icons/lobli-128.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.7 KiB |
BIN
lob.li crx/icons/lobli-16.png
Normal file
BIN
lob.li crx/icons/lobli-16.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 476 B |
BIN
lob.li crx/icons/lobli-19.png
Normal file
BIN
lob.li crx/icons/lobli-19.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 619 B |
BIN
lob.li crx/icons/lobli-32.png
Normal file
BIN
lob.li crx/icons/lobli-32.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1005 B |
BIN
lob.li crx/icons/lobli-48.png
Normal file
BIN
lob.li crx/icons/lobli-48.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.7 KiB |
82
lob.li crx/js/background.js
Normal file
82
lob.li crx/js/background.js
Normal 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();
|
||||
}
|
15
lob.li crx/js/contextmenu.js
Normal file
15
lob.li crx/js/contextmenu.js
Normal 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"});
|
||||
});
|
41
lob.li crx/manifest.json
Normal file
41
lob.li crx/manifest.json
Normal file
@ -0,0 +1,41 @@
|
||||
{
|
||||
"name": "lob.li link shortener",
|
||||
"version": "0.0.1",
|
||||
"manifest_version": 2,
|
||||
"description": "lobli shortening and resolving without having to visit lob.li",
|
||||
"homepage_url": "http://lob.li",
|
||||
"browser_action": {
|
||||
"default_icon": "icons/lobli-19.png",
|
||||
"default_title": "lobli for Chrome"
|
||||
},
|
||||
"background": {
|
||||
"page": "background.html",
|
||||
"persistent": false
|
||||
},
|
||||
"permissions": [
|
||||
"<all_urls>",
|
||||
"tabs",
|
||||
"clipboardWrite",
|
||||
"contextMenus",
|
||||
"notifications"
|
||||
],
|
||||
"icons": {
|
||||
"16": "icons/lobli-16.png",
|
||||
"32": "icons/lobli-32.png",
|
||||
"48": "icons/lobli-48.png",
|
||||
"128": "icons/lobli-128.png"
|
||||
},
|
||||
"web_accessible_resources": [
|
||||
"icons/lobli-16.png",
|
||||
"icons/lobli-19.png",
|
||||
"icons/lobli-32.png",
|
||||
"icons/lobli-48.png",
|
||||
"icons/lobli-128.png"
|
||||
],
|
||||
"commands": {
|
||||
"shortenTab": {
|
||||
"suggested_key": { "default": "Alt+L" },
|
||||
"description": "Shortens Current Tab"
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user