ripunzel/src/dissectLink.js

74 lines
2.1 KiB
JavaScript
Raw Normal View History

'use strict';
const UrlPattern = require('url-pattern');
const hosts = [{
method: 'self',
label: 'self',
pattern: new UrlPattern('http(s)\\://(www.)reddit.com/r/:subreddit/comments/:id/:uri/'),
}, {
method: 'redditImage',
label: 'reddit',
pattern: new UrlPattern('http(s)\\://i.redd.it/:id.:ext(?*)'),
}, {
method: 'redditImage',
label: 'reddit',
pattern: new UrlPattern('http(s)\\://i.reddituploads.com/:id(?*)'),
}, {
method: 'redditVideo',
label: 'reddit',
pattern: new UrlPattern('http(s)\\://v.redd.it/:id(?*)'),
}, {
method: 'imgurImage',
label: 'imgur',
pattern: new UrlPattern('http(s)\\://(:subdomain.)imgur.com/(:id_d)(:id)(.:ext)(?*)'),
}, {
method: 'imgurAlbum',
label: 'imgur',
pattern: new UrlPattern('http(s)\\://(:subdomain.)imgur.com/:type/:id(#:focus)(?*)'),
}, {
method: 'vidbleImage',
label: 'vidble',
pattern: new UrlPattern('http(s)\\://(www.)vidble.com/(show/):id(.:ext)(?*)'),
}, {
method: 'vidbleVideo',
label: 'vidble',
pattern: new UrlPattern('http(s)\\://(www.)vidble.com/watch?v=:id(?*)'),
}, {
method: 'vidbleAlbum',
label: 'vidble',
pattern: new UrlPattern('http(s)\\://(www.)vidble.com/album/:id(?*)'),
}, {
method: 'gfycat',
label: 'gfycat',
pattern: new UrlPattern('http(s)\\://(:server.)gfycat.com/(gifs/detail/)(:id-mobile)(:id-size_restricted)(:id)(.:ext)(?*)'),
}, {
2024-09-11 03:16:55 +00:00
method: 'eroshareAlbum',
label: 'eroshare',
pattern: new UrlPattern('http(s)\\://eroshare.com/:id(#)(:query)'),
2024-09-11 03:16:55 +00:00
}, {
method: 'eroshareItem',
label: 'eroshare',
pattern: new UrlPattern('http(s)\\://eroshare.com/i/:id(#)(:query)'),
}];
module.exports = function dissectLink(url) {
return hosts.reduce((acc, host) => {
if (acc) {
return acc;
}
const match = host.pattern.match(url.replace(/(https?:\/\/)|(\/)+/g, '$1$2')); // remove double slashes
if (match) {
return Object.assign(match, {
url,
method: host.method,
label: host.label,
});
}
return null;
}, null);
};