Skip to content

Commit

Permalink
Add pref for special shortening services, and use it for github. Fix #95
Browse files Browse the repository at this point in the history
.
  • Loading branch information
fwenzel committed May 28, 2019
1 parent 14201d9 commit 2b31deb
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 37 deletions.
File renamed without changes.
10 changes: 10 additions & 0 deletions src/data/js/discover/github.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(function() {
// Just pass through document location, but enforce git.io shortener.
browser.runtime.sendMessage({
short: false,
url: document.location.href,
hash: document.location.hash,
title: document.querySelector('title').text,
force_service: 'gitio'
});
}());
40 changes: 21 additions & 19 deletions src/lib/discover.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import processUrl from './shortener';
const _ = browser.i18n.getMessage;


/** Default discovery content script for any tab. */
function genericDiscovery() {
/** Execute discovery content script in the current tab. */
function runDiscoveryScript(script) {
browser.tabs.executeScript({
file: '/data/js/find-short-url.js'
file: `/data/js/discover/${script}.js`
}).catch(e => {
if (e.message && /Missing host permission for the tab/.test(e.message)) {
notify(_('error_host_permission'));
Expand All @@ -23,42 +23,44 @@ function genericDiscovery() {
* then listen to it being returned via message.
*/
export default function discoverUrl() {
// browser.tabs.getCurrent()
browser.storage.local.get('prefs').then(ret => {
const prefs = ret['prefs'] || {};

browser.tabs.query({active: true, currentWindow: true})
// Do not investigate special cases if preffed off.
if (prefs.use_special === false) {
return 'default';
}

return browser.tabs.query({active: true, currentWindow: true})
.then(tabs => {
const url = new URL(tabs[0].url);

// Trigger special URL logic for certain origins.
// Most the time, just generically discover URLs.
switch (prefs.use_special !== false) { // Will never match if preffed off.
case /(www\.)amazon\.com/.test(url.hostname):
browser.tabs.executeScript({
file: '/data/js/discover/amazon.js'
});
break;
switch (true) {
case /(www\.)?amazon\.com/.test(url.hostname):
return 'amazon';

case /(www\.)youtube\.com/.test(url.hostname):
browser.tabs.executeScript({
file: '/data/js/discover/youtube.js'
});
break;
case /(www\.)?github\.com/.test(url.hostname):
return 'github';

case /(www\.)?youtube\.com/.test(url.hostname):
return 'youtube';

default:
genericDiscovery();
return 'default';
}
});
});
})
.then(script => runDiscoveryScript(script));
}

/** Listen to content scripts posting back after discovery */
browser.runtime.onMessage.addListener(msg => {
// If specialty discoverer returns it did not find anything, apply default
// discovery mechanism and listen again.
if (!msg.url) {
genericDiscovery();
runDiscoveryScript('default');
} else {
processUrl(msg);
}
Expand Down
56 changes: 38 additions & 18 deletions src/lib/shortener.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const serviceUrls = {
// http://dev.bitly.com/links.html#v3_shorten
request: url => {
return browser.storage.local.get('prefs').then(ret => {
let prefs = ret['prefs'] || {};
const prefs = ret['prefs'] || {};
if (!prefs.bitly_apikey) {
throw new Error(_('apikey_error'));
}
Expand All @@ -33,28 +33,48 @@ const serviceUrls = {
},
result: response => response.text(),
force_https: true
}
},

/* Special services: Cannot be chosen manually. */
gitio: {
// https://github.blog/2011-11-10-git-io-github-url-shortener/
request: url => fetch('https://git.io/', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
redirect: 'manual',
body: `url=${encodeURIComponent(url)}`
}),
result: response => response.headers.get('Location'),
},
}


/** Create a short URL from is.gd, tinyurl, etc. */
function createShortUrl(url) {
function createShortUrl(url, force_service) {
let req;

try {
// Get shortening service from prefs.
return browser.storage.local.get('prefs').then(ret => {
let prefs = ret['prefs'] || {};
const prefs = ret['prefs'] || {};
let service;
switch (prefs.service) {
case 'custom':
service = { url: prefs.custom_url };
break;
case 'none':
return url; // Skip shortening altogether.
default:
service = serviceUrls[prefs['service'] || default_service];
break;

// Hardcoded special-cased websites can enforce a shortening service.
if (prefs.service !== 'none' && force_service) {
service = serviceUrls[force_service];
} else {
switch (prefs.service) {
case 'custom':
service = { url: prefs.custom_url };
break;
case 'none':
return url; // Skip shortening altogether.
default:
service = serviceUrls[prefs['service'] || default_service];
break;
}
}

if (service.request) {
Expand Down Expand Up @@ -84,6 +104,7 @@ function createShortUrl(url) {
});

}).catch(err => {
console.log(err.message);
throw new Error(_('shorten_error'));
});

Expand All @@ -97,7 +118,7 @@ function createShortUrl(url) {
/** Finalize (notify and copy to clipboard) a detected or generated URL. */
function finalizeUrl(longUrl, shortUrl, title) {
browser.storage.local.get('prefs').then(ret => {
let prefs = ret['prefs'] || {};
const prefs = ret['prefs'] || {};
let copyText;

// Remove whitespace from final URL.
Expand Down Expand Up @@ -125,7 +146,7 @@ export default function processUrl(found_url) {
let hash = found_url['hash'] || '';

browser.storage.local.get('prefs').then(ret => {
let prefs = ret['prefs'] || {};
const prefs = ret['prefs'] || {};

if (prefs.strip_urm || prefs.keep_hash) {
let parsedUrl = new URL(url);
Expand All @@ -151,9 +172,8 @@ export default function processUrl(found_url) {

// Shorten URL if it's not considered "short" or exceeds length limit.
if (!found_url['short'] || (prefs.shorten_canonical > 0 && url.length > prefs.shorten_canonical)) {
createShortUrl(url).then(result => {
finalizeUrl(url, result, title);
});
createShortUrl(url, found_url['force_service'])
.then(result => finalizeUrl(url, result, title));
} else {
finalizeUrl(null, url, title);
}
Expand Down

0 comments on commit 2b31deb

Please sign in to comment.