From 4aa2c0b7c01cbd8982ff1df4983a924c0d803056 Mon Sep 17 00:00:00 2001 From: David Todd Date: Fri, 19 Sep 2014 15:35:41 -0400 Subject: [PATCH] Add more API functions The primary functions are using a server presented API key, appeneded to all requests (except testAPIKey and GetNewAPIKey). Also downloads a new key on startup if not in the storage --- lob.li crx/js/background.js | 68 +++++++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 6 deletions(-) diff --git a/lob.li crx/js/background.js b/lob.li crx/js/background.js index 415632d..f92eb3b 100644 --- a/lob.li crx/js/background.js +++ b/lob.li crx/js/background.js @@ -8,10 +8,30 @@ chrome.commands.onCommand.addListener(function(command){ // Keyboard shortcut tr }); 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); - }); + var disabled = getData("lobli-disabled"); + if(disabled == true){ + showAlert("For some reason or another, your extension has been disabled.\nFor more info, please email c0de@unps,us"); + }else{ + chrome.tabs.query({active: true, currentWindow: true}, function(tabs){ + var current = tabs[0] + shortenTabURL(current.id); + }); + } +}); + +chrome.runtime.onInstalled.addLIstener(function(data){ // Get a new API key from the get go + if(data.reason == "install"){ + getNewAPIKey(); + }else if(data.reason == "update"){ + testAPIKey(); + } +}); + +chrome.runtime.onStartup.addListener(function(){ // Check to see if extension is disabled on startup + var data = getData("lobliAPIKey"); + if(data != undefined && data == true){ + testAPIKey(); + } }); function showAlert(text){ @@ -45,7 +65,8 @@ function shortenTabURL(tabid){ // Use just a tab id to shorten its url function shortenURL(url){ // Creates a short url and copies it to clipboard if(testURL(url)){ - sendAPIRequest("?shorten&url=" + url, function(req){ + var key = getData("lobliAPIKey"); + sendAPIRequest("?shorten&url=" + url + "&key=" + key, function(req){ var res = req.responseText.trim(); switch(res){ case "dead": @@ -68,7 +89,8 @@ function shortenURL(url){ // Creates a short url and copies it 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 key = getData("lobliAPIKey"); + sendAPIRequest("?resolve&url=" + url + "&key=" + key, function(req){ var res = req.responseText.trim(); copyToClipboard(res); showAlert("Link Resolved!\n" + res); @@ -76,6 +98,40 @@ function resolveURL(url){ // For when/if I decide to add the ability to resolve } } +function testAPIKey(){ // Compares local key to server + var key = getData("lobliAPIKey"); + if(key != undefined){ + sendAPIRequest("?testKey&key=" + key, function(req){ + var res = req.responseText.trim(); + if(key == "Invalid API Key"){ // Misformatted or other, try to get a new one + getNewAPIKey(); + } + }); + }else{ + getNewAPIKey(); + } +} + +function getNewAPIKey(){ // Tries to get a new API key from the server + var manifest = chrome.runtime.getManifest(); + sendAPIRequest("?newAPIKey&c=loblichrome&v=" + manifest.version, function(req){ + var res = req.responseText.trim(); + if(res == "Blacklisted client"){ // This client was blacklisted for some reason. Lets disable parts of the extension + chrome.browserAction.setIcon({ path: "../icons/lobli-19-disabled.png" }); + chrome.storage.sync.set({ "lobli-disabled": true }); + showAlert("D: Your client has been blacklisted!\nIf this was a mistake, please email c0de@unps.us"); + }else{ + chrome.storage.sync.set({ "lobliAPIKey": res, "lobli-disabled": false }); + } + }); +} + +function getData(key){ + chrome.storage.sync.get(key, function(data){ + return data; + }); +} + 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();