Skip to content

Commit

Permalink
Added goo.gl bookmarking service. Fix #24.
Browse files Browse the repository at this point in the history
  • Loading branch information
Fred Wenzel committed May 11, 2013
1 parent c40b97a commit 503d82a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 17 deletions.
66 changes: 49 additions & 17 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,37 @@ const clipboard = require('clipboard'),

aboutconfig = require('preferences-service'); // Deprecated

/* String constants */
// Preferences, deprecated
// about:config preferences, deprecated
const serviceurl_pref = 'extensions.copyshorturl.serviceURL';

// Possible service URLs
// Service defaults.
const default_service = 'isgd',
serviceurl_docs = 'http://copy-shorturl.readthedocs.org/en/latest/serviceurl.html';

// Specify 'url' for plain-text GET APIs, or request/result for more complex
// variants.
var serviceurls = {
isgd: 'http://is.gd/api.php?longurl=%URL%',
tinyurl: 'http://tinyurl.com/api-create.php?url=%URL%'
isgd: {
url: 'http://is.gd/api.php?longurl=%URL%'
},
tinyurl: {
url: 'http://tinyurl.com/api-create.php?url=%URL%'
},
googl: {
// https://developers.google.com/url-shortener/v1/getting_started
request: function(url) {
let req = new xhr.XMLHttpRequest();
req.open("POST", 'https://www.googleapis.com/urlshortener/v1/url', false);
req.setRequestHeader('Content-Type', 'application/json');
req.send(JSON.stringify({ longUrl: url }));
return req;
},
result: function(req) {
console.log('ohai');
let shortened = JSON.parse(req.responseText);
return shortened.id;
}
}
};
// Hook up pref button to service URL docs.
require('sdk/simple-prefs').on('serviceurl_docs', function() {
Expand All @@ -25,29 +46,40 @@ require('sdk/simple-prefs').on('serviceurl_docs', function() {


/** Determine service URL */
function getServiceUrl() {
if (prefs.service === 'custom' && prefs.customurl) {
return prefs.customurl;
} else {
// Restore default.
prefs.service = default_service;
function getShorteningService() {
if (prefs.service === 'custom') {
if (prefs.customurl) {
return {
url: prefs.customurl
};
} else {
// Restore default.
prefs.service = default_service;
}
}
return serviceurls[prefs.service];
}


/** Create a short URL from is.gd, tinyurl, etc. */
function createShortUrl(url) {
let req = new xhr.XMLHttpRequest();
let req,
service = getShorteningService();

try {
req.open("GET", getServiceUrl().replace(
'%URL%', encodeURIComponent(url)), false);
req.send(null);
if (service.request) {
req = service.request(url);
} else {
req = new xhr.XMLHttpRequest();
req.open("GET", service.url.replace(
'%URL%', encodeURIComponent(url)), false);
req.send(null);
}
if (req.status === 200) {
let result = service.result ? service.result(req) : req.responseText.trim();
notify("No short URL found. Created alternative URL instead:\n" +
url + ' --> ' + req.responseText);
return req.responseText.trim();
url + ' --> ' + result);
return result;
} else {
throw new Error('ZOMG, error creating short URL.');
}
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
}, {
"value": "tinyurl",
"label": "tinyurl.com (http://tinyurl.com/abcde)"
}, {
"value": "googl",
"label": "goo.gl (http://goo.gl/abcde)"
}, {
"value": "custom",
"label": "Custom (specify below)"
Expand Down

0 comments on commit 503d82a

Please sign in to comment.