Skip to content

Commit

Permalink
Remove defunct is.gd and default to tinyurl. Add cuttly/goly/vurl opt…
Browse files Browse the repository at this point in the history
…ions. Fix #122.
  • Loading branch information
fwenzel committed Oct 24, 2021
1 parent fca5801 commit 6215edc
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 9 deletions.
11 changes: 7 additions & 4 deletions src/data/html/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@
<label for="service" data-i18n="service_title">service</label>
<select id="service">
<option value="none" data-i18n="service_none">none</option>
<option value="isgd">is.gd (https://is.gd/abcde)</option>
<option value="tinyurl">tinyurl.com (http://tinyurl.com/abcde)</option>
<!--<option value="isgd">is.gd (https://is.gd/abcde)</option>-->
<option value="tinyurl">tinyurl.com (https://tinyurl.com/abcde)</option>
<option value="cuttly">cutt.ly (https://cutt.ly/abcde)</option>
<option value="goly">go.ly (https://go.ly/abcde)</option>
<option value="vurl">vurl.com (https://vurl.com/abcde)</option>
<option value="bitly">bit.ly (http://bit.ly/abcde)</option>
<option value="custom" data-i18n="service_custom">custom</option>
</select>
Expand All @@ -41,7 +44,7 @@
<p id="bitly_details" style="display:none">
<label for="bitly_apikey" data-i18n="bitly_apikey_title">bitly api key</label>
<input type="text" id="bitly_apikey">
<a href="https://bitly.com/a/oauth_apps"><em data-i18n="bitly_apikey_description">acquire API key</em></a>
<a href="https://app.bitly.com/settings/api/"><em data-i18n="bitly_apikey_description">acquire API key</em></a>
</p>

<!-- Custom URL + documentation link -->
Expand All @@ -58,7 +61,7 @@
<em data-i18n="shorten_canonical_description">0 = never</em>
</p>

<!-- Use special? -->
<!-- Use special, known shorteners? -->
<p>
<input type="checkbox" id="use_special">
<label for="use_special" data-i18n="use_special_title">use special?</label>
Expand Down
58 changes: 53 additions & 5 deletions src/lib/shortener.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,67 @@ import notify from './notify.js';
const _ = browser.i18n.getMessage;

// Service default.
const default_service = 'isgd';
const DEFAULT_SERVICE = 'tinyurl';

// Specify 'url' for plain-text GET APIs, or request/result for more complex
// variants. Note: return a fetch() promise.
// Do not forget to add origins to permissions in manifest.
const serviceUrls = {
none: {}, // Placeholder.
isgd: {
url: 'https://is.gd/api.php?longurl=%URL%'
},

// TODO: isgd appears defunct (issue #122). Remove if permanently gone.
// isgd: {
// url: 'https://is.gd/api.php?longurl=%URL%'
// },

tinyurl: {
url: 'https://tinyurl.com/api-create.php?url=%URL%',
force_https: true
},

cuttly: {
request: url => {
let form = new FormData();
form.append('url', url);
form.append('domain', 0); // Needed?
return fetch('https://cutt.ly/scripts/shortenUrl.php', {
method: 'POST',
body: form
});
},
result: response => response.text()
},

goly: {
request: url => fetch('https://api.go.ly/api/v1/link', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
'DomainID': 2,
'Payload': {
'Destination': url
},
'Type': 0,
'UserID': 1
})
}),
result: async response => {
const res = await response.json();
if (res['Status'] === 200) {
return `https://go.ly/${res['Slug']}`;
} else {
throw new Error(res['Message']);
}
}
},

vurl: {
url: 'https://vurl.com/api.php?url=%URL%',
force_https: true
},

bitly: {
// https://dev.bitly.com/v4/#operation/createBitlink
request: async url => {
Expand Down Expand Up @@ -79,7 +126,8 @@ function createShortUrl(url, force_service) {
case 'none':
return url; // Skip shortening altogether.
default:
service = serviceUrls[prefs['service'] || default_service];
service = serviceUrls[prefs.service];
service ||= serviceUrls[DEFAULT_SERVICE]; // Fallback to default.
break;
}
}
Expand Down

0 comments on commit 6215edc

Please sign in to comment.