Skip to content

Commit

Permalink
Cleaning code
Browse files Browse the repository at this point in the history
  • Loading branch information
ManeraKai committed Feb 16, 2022
1 parent 9f90a84 commit b157cc8
Show file tree
Hide file tree
Showing 23 changed files with 322 additions and 373 deletions.
44 changes: 21 additions & 23 deletions src/assets/javascripts/helpers/imgur.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,39 +61,39 @@ function setRimgoCustomRedirects(val) {
console.log("rimgoCustomRedirects: ", val)
}

let disableImgur;
const getDisableImgur = () => disableImgur;
function setDisableImgur(val) {
disableImgur = val;
browser.storage.sync.set({ disableImgur })
let disable;
const getDisable = () => disable;
function setDisable(val) {
disable = val;
browser.storage.sync.set({ disableImgur: disable })
}

function redirect(url, initiator, type) {
function isImgur(url, initiator) {
if (disable) return false;
if (url.pathname == "/") return false;
if (
initiator &&
([...redirects.rimgo.normal, ...rimgoCustomRedirects].includes(initiator.origin) || targets.includes(initiator.host))
) return false;
return targets.some((rx) => rx.test(url.href));
}

function redirect(url, type) {
// https://imgur.com/gallery/s4WXQmn
// https://imgur.com/a/H8M4rcp
// https://imgur.com/gallery/gYiQLWy
// https://imgur.com/gallery/cTRwaJU
// https://i.imgur.com/CFSQArP.jpeg

if (disableImgur) return null;

if (url.pathname == "/") return null;

if (type != "main_frame" && "sub_frame" && "xmlhttprequest" && "other") return null;

let instancesList = [...rimgoRedirectsChecks, ...rimgoCustomRedirects];
if (instancesList.length === 0) return null;
let randomInstance = commonHelper.getRandomInstance(instancesList)

if (initiator && (instancesList.includes(initiator.origin) || targets.includes(initiator.host))) return null;

return `${randomInstance}${url.pathname}${url.search}`;
}

function isImgur(url) {
return targets.some((rx) => rx.test(url.href));
}

async function init() {
return new Promise((resolve) => {
browser.storage.sync.get(
Expand All @@ -104,9 +104,9 @@ async function init() {
"rimgoCustomRedirects",
],
(result) => {
disableImgur = result.disableImgur ?? false;
if (result.imgurRedirects)
redirects = result.imgurRedirects;
disable = result.disableImgur ?? false;

if (result.imgurRedirects) redirects = result.imgurRedirects;

rimgoRedirectsChecks = result.rimgoRedirectsChecks ?? [...redirects.rimgo.normal];
rimgoCustomRedirects = result.rimgoCustomRedirects ?? [];
Expand All @@ -118,14 +118,12 @@ async function init() {
}

export default {
targets,

getRedirects,
getCustomRedirects,
setRedirects,

getDisableImgur,
setDisableImgur,
getDisable,
setDisable,

getRimgoRedirectsChecks,
setRimgoRedirectsChecks,
Expand Down
58 changes: 25 additions & 33 deletions src/assets/javascripts/helpers/instagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ function setRedirects(val) {
setBibliogramRedirectsChecks(bibliogramRedirectsChecks);
}


let bibliogramRedirectsChecks;
const getBibliogramRedirectsChecks = () => bibliogramRedirectsChecks;
function setBibliogramRedirectsChecks(val) {
Expand Down Expand Up @@ -84,45 +83,40 @@ const reservedPaths = [
"tv",
"reel",
];

const bypassPaths = /\/(accounts\/|embeds?.js)/;

let disableInstagram;
const getDisableInstagram = () => disableInstagram;
function setDisableInstagram(val) {
disableInstagram = val;
browser.storage.sync.set({ disableInstagram })
let disable;
const getDisable = () => disable;
function setDisable(val) {
disable = val;
browser.storage.sync.set({ disableInstagram: disable })
}

function isInstagram(url, initiator) {
if (disable) return false;
if (
initiator &&
([...redirects.bibliogram.normal, ...bibliogramCustomRedirects].includes(initiator.origin) || targets.includes(initiator.host))
)
return false; // Do not redirect Bibliogram view on Instagram links
return targets.includes(url.host)
}

function redirect(url, initiator, type) {
if (disableInstagram) return null;
function redirect(url, type) {
if (type !== "main_frame" || url.pathname.match(bypassPaths))
return 'CANCEL'; // Do not redirect /accounts, /embeds.js, or anything other than main_frame

let instancesList = [...bibliogramRedirectsChecks, ...bibliogramCustomRedirects];
if (instancesList.length === 0) return null;
let randomInstance = commonHelper.getRandomInstance(instancesList)

// Do not redirect Bibliogram view on Instagram links
if (initiator && (instancesList.includes(initiator.origin) || targets.includes(initiator.host)))
return null;

// Do not redirect /accounts, /embeds.js, or anything other than main_frame
if (type !== "main_frame" || url.pathname.match(bypassPaths))
return 'CANCEL';

console.log("Hello", url.href)

if (url.pathname === "/" || reservedPaths.includes(url.pathname.split("/")[1])) {
console.log("wewe")
if (url.pathname === "/" || reservedPaths.includes(url.pathname.split("/")[1]))
return `${randomInstance}${url.pathname}${url.search}`;
}
else {
console.log("A user profile")
return `${randomInstance}/u${url.pathname}${url.search}`;
} // Likely a user profile, redirect to '/u/...'
else
return `${randomInstance}/u${url.pathname}${url.search}`; // Likely a user profile, redirect to '/u/...'
}

function isInstagram(url) {
return targets.includes(url.host)
}

async function init() {
return new Promise((resolve) => {
Expand All @@ -134,28 +128,26 @@ async function init() {
"bibliogramCustomRedirects",
],
(result) => {
disableInstagram = result.disableInstagram ?? false;
disable = result.disableInstagram ?? false;

if (result.instagramRedirects) redirects = result.instagramRedirects

bibliogramRedirectsChecks = result.bibliogramRedirectsChecks ?? [...redirects.bibliogram.normal];

bibliogramCustomRedirects = result.bibliogramCustomRedirects ?? [];

resolve();
}
)
})

}

export default {
getRedirects,
getCustomRedirects,
setRedirects,

getDisableInstagram,
setDisableInstagram,
getDisable,
setDisable,

getBibliogramRedirectsChecks,
setBibliogramRedirectsChecks,
Expand Down
49 changes: 20 additions & 29 deletions src/assets/javascripts/helpers/maps.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,12 @@ function addressToLatLng(address, callback) {
if (xmlhttp.readyState === XMLHttpRequest.DONE) {
if (xmlhttp.status === 200) {
const json = JSON.parse(xmlhttp.responseText)[0];
if (json) {
callback(
`${json.lat}%2C${json.lon}`,
`${json.boundingbox[2]},${json.boundingbox[1]},${json.boundingbox[3]},${json.boundingbox[0]}`
);
}
} else {
if (json) callback(
`${json.lat}%2C${json.lon}`,
`${json.boundingbox[2]},${json.boundingbox[1]},${json.boundingbox[3]},${json.boundingbox[0]}`
);
} else
console.info("Error: Status is " + xmlhttp.status);
}
}
};
xmlhttp.open(
Expand All @@ -48,18 +45,20 @@ function addressToLatLng(address, callback) {
xmlhttp.send();
}

let disableMaps;
const getDisableMaps = () => disableMaps;
function setDisableMaps(val) {
disableMaps = val;
browser.storage.sync.set({ disableMaps })
let disable;
const getDisable = () => disable;
function setDisable(val) {
disable = val;
browser.storage.sync.set({ disableMaps: disable })
}

function redirect(url, initiator) {
if (disableMaps) return null;

if (initiator && initiator.host === "earth.google.com") return null;
function isMaps(url, initiator) {
if (disable) return false;
if (initiator && initiator.host === "earth.google.com") return false;
return url.href.match(targets);
}

function redirect(url) {
let redirect;
let link = commonHelper.getRandomInstance(redirects.normal);
let mapCentre = "";
Expand Down Expand Up @@ -104,10 +103,7 @@ function redirect(url, initiator) {
addressToLatLng(url.searchParams.get("destination"), (coords) => destination = coords);
redirect = `${link}/directions?engine=${travelMode}&route=${origin}%3B${destination}${mapCentre}${params}`;
// Get marker from data attribute
} else if (
url.pathname.includes("data=") &&
url.pathname.match(dataLatLngRegex)
) {
} else if (url.pathname.includes("data=") && url.pathname.match(dataLatLngRegex)) {
const [mlat, mlon] = url.pathname.match(dataLatLngRegex);
redirect = `${link}/?mlat=${mlat.replace("!3d", "")}&mlon=${mlon.replace("!4d", "")}${mapCentre}${params}`;
// Get marker from ll param
Expand All @@ -130,26 +126,21 @@ function redirect(url, initiator) {
return redirect;
}

function isMaps(url) {
return url.href.match(targets)
}

async function init() {
return new Promise((resolve) => {
browser.storage.sync.get(
"disableMaps",
(result) => {
disableMaps = result.disableMaps ?? false
disable = result.disableMaps ?? false
resolve();
}
);
});
}

export default {
addressToLatLng,
getDisableMaps,
setDisableMaps,
getDisable,
setDisable,
redirect,
isMaps,
init,
Expand Down
48 changes: 21 additions & 27 deletions src/assets/javascripts/helpers/medium.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,42 +64,35 @@ function setScribeCustomRedirects(val) {
console.log("scribeCustomRedirects: ", val)
}

let disableMedium;
const getDisableMedium = () => disableMedium;
function setDisableMedium(val) {
disableMedium = val;
browser.storage.sync.set({ disableMedium })
let disable;
const getDisable = () => disable;
function setDisable(val) {
disable = val;
browser.storage.sync.set({ disableMedium: disable })
}

function isMedium(url, initiator) {
if (disable) return false;
if (url.pathname == "/") return false;

if (
commonHelper.isFirefox() &&
initiator && ([...redirects.scribe.normal, ...scribeCustomRedirects].includes(initiator.origin) || targets.includes(initiator.host))
) return false;

function redirect(url, initiator, type) {

if (disableMedium) return null;

if (url.pathname == "/") return null;
return targets.some((rx) => rx.test(url.host));
}
function redirect(url, type) {

if (type != "main_frame" && "sub_frame" && "xmlhttprequest" && "other") return null;


let instancesList = [...scribeRedirectsChecks, ...scribeCustomRedirects];
if (instancesList.length === 0) return null;
let randomInstance = commonHelper.getRandomInstance(instancesList)

if (
commonHelper.isFirefox() && initiator &&
(instancesList.includes(initiator.origin) || targets.includes(initiator.host))
) {
browser.storage.sync.set({ redirectBypassFlag: true });
return null;
}
return `${randomInstance}${url.pathname}${url.search}`;
}

function isMedium(url) {
return targets.some((rx) => rx.test(url.host));
}

async function init() {
return new Promise((resolve) => {
browser.storage.sync.get(
Expand All @@ -110,12 +103,13 @@ async function init() {
"scribeCustomRedirects",
],
(result) => {
disableMedium = result.disableMedium ?? false;
if (result.mediumRedirects)
redirects = result.mediumRedirects;
disable = result.disableMedium ?? false;

if (result.mediumRedirects) redirects = result.mediumRedirects;

scribeRedirectsChecks = result.scribeRedirectsChecks ?? [...redirects.scribe.normal];
scribeCustomRedirects = result.scribeCustomRedirects ?? [];

resolve();
}
)
Expand All @@ -130,8 +124,8 @@ export default {
getCustomRedirects,
setRedirects,

getDisableMedium,
setDisableMedium,
getDisable,
setDisable,

getScribeRedirectsChecks,
setScribeRedirectsChecks,
Expand Down
Loading

0 comments on commit b157cc8

Please sign in to comment.