mirror of
https://codeberg.org/LibRedirect/libredirect.git
synced 2025-01-10 17:22:42 +01:00
117 lines
3.3 KiB
JavaScript
117 lines
3.3 KiB
JavaScript
'use strict';
|
|
|
|
const nitterDefault = 'https://nitter.net';
|
|
const twitterRegex = /((www|mobile)\.)?twitter\.com/;
|
|
const invidiousDefault = 'https://invidio.us';
|
|
const youtubeRegex = /((www|m)\.)?youtube(-nocookie)?\.com/;
|
|
const bibliogramDefault = 'https://bibliogram.art';
|
|
const instagramRegex = /((www|about|help)\.)?instagram\.com/;
|
|
const instagramPathsRegex = /(\/a|\/admin|\/api|\/favicon.ico|\/static|\/imageproxy|\/p|\/u|\/developer|\/about|\/legal|\/explore|\/director)/;
|
|
|
|
let nitterInstance;
|
|
let invidiousInstance;
|
|
let bibliogramInstance;
|
|
let disableNitter;
|
|
let disableInvidious;
|
|
let disableBibliogram;
|
|
|
|
chrome.storage.sync.get(
|
|
[
|
|
'nitterInstance',
|
|
'invidiousInstance',
|
|
'bibliogramInstance',
|
|
'disableNitter',
|
|
'disableInvidious',
|
|
'disableBibliogram'
|
|
],
|
|
result => {
|
|
disableNitter = result.disableNitter;
|
|
disableInvidious = result.disableInvidious;
|
|
disableBibliogram = result.disableBibliogram;
|
|
nitterInstance = result.nitterInstance || nitterDefault;
|
|
invidiousInstance = result.invidiousInstance || invidiousDefault;
|
|
bibliogramInstance = result.bibliogramInstance || bibliogramDefault;
|
|
}
|
|
);
|
|
|
|
chrome.storage.onChanged.addListener(changes => {
|
|
if ('nitterInstance' in changes) {
|
|
nitterInstance = changes.nitterInstance.newValue || nitterDefault;
|
|
}
|
|
if ('invidiousInstance' in changes) {
|
|
invidiousInstance = changes.invidiousInstance.newValue || invidiousDefault;
|
|
}
|
|
if ('bibliogramInstance' in changes) {
|
|
bibliogramInstance = changes.bibliogramInstance.newValue || bibliogramDefault;
|
|
}
|
|
if ('disableNitter' in changes) {
|
|
disableNitter = changes.disableNitter.newValue;
|
|
}
|
|
if ('disableInvidious' in changes) {
|
|
disableInvidious = changes.disableInvidious.newValue;
|
|
}
|
|
if ('disableBibliogram' in changes) {
|
|
disableBibliogram = changes.disableBibliogram.newValue;
|
|
}
|
|
});
|
|
|
|
function redirectYouTube(url) {
|
|
if (url.host.split('.')[0] === 'studio') {
|
|
// Avoid redirecting `studio.youtube.com`
|
|
return null;
|
|
} else {
|
|
return `${invidiousInstance}${url.pathname}${url.search}`;
|
|
}
|
|
}
|
|
|
|
function redirectTwitter(url) {
|
|
return `${nitterInstance}${url.pathname}${url.search}`
|
|
}
|
|
|
|
function redirectInstagram(url) {
|
|
if (url.pathname === '/' || url.pathname.match(instagramPathsRegex)) {
|
|
return `${bibliogramInstance}${url.pathname}${url.search}`;
|
|
} else {
|
|
// Redirect user profile requests to '/u/...'
|
|
return `${bibliogramInstance}/u${url.pathname}${url.search}`;
|
|
}
|
|
}
|
|
|
|
chrome.webRequest.onBeforeRequest.addListener(
|
|
details => {
|
|
const url = new URL(details.url);
|
|
let redirect;
|
|
if (url.host.match(youtubeRegex)) {
|
|
if (!disableInvidious) {
|
|
redirect = {
|
|
redirectUrl: redirectYouTube(url)
|
|
};
|
|
}
|
|
} else if (url.host.match(twitterRegex)) {
|
|
if (!disableNitter) {
|
|
redirect = {
|
|
redirectUrl: redirectTwitter(url)
|
|
};
|
|
}
|
|
} else if (url.host.match(instagramRegex)) {
|
|
if (!disableBibliogram) {
|
|
redirect = {
|
|
redirectUrl: redirectInstagram(url)
|
|
};
|
|
}
|
|
}
|
|
if (redirect && redirect.redirectUrl) {
|
|
console.log(
|
|
'Redirecting', `"${url.toString()}"`, '=>', `"${redirect.redirectUrl}"`
|
|
);
|
|
console.log('Details', details);
|
|
}
|
|
return redirect;
|
|
},
|
|
{
|
|
urls: ["<all_urls>"],
|
|
types: ['main_frame', 'sub_frame',]
|
|
},
|
|
['blocking']
|
|
);
|