Skip to content
This repository has been archived by the owner on Aug 16, 2023. It is now read-only.

Commit

Permalink
Add option for /explore redirect, fixes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
raikasdev committed Nov 23, 2022
1 parent 601a48e commit 6e2fa17
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 7 deletions.
26 changes: 23 additions & 3 deletions chrome/m4redirect.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var chrome;
// If someone knows a better way to detect Mastodon4 instances, please send me a message!
// [email protected]
const MASTODON_DIV = document.getElementById("mastodon");
const EXTERNAL_USER_REGEX = /(@[a-zA-Z0-9_]+)(?!.*@)/; // @username matches, @[email protected] doesn't
const EXTERNAL_USER_REGEX = /^(@[a-zA-Z0-9_]+)(?!.*@)(?!\/.+)$/; // @username matches, @[email protected] @username/following doesn't
const EXTERNAL_POST_REGEX = /(@[a-zA-Z0-9_]+)(?!.*@)\/(\d+)/; // @username matches, @[email protected] doesn't

if (MASTODON_DIV) {
Expand All @@ -29,7 +29,23 @@ if (MASTODON_DIV) {
) {
return; // Disabled
}
// Match users and posts and redirect if needed
// Lets first parse some generic URLs out of the way
// For example: /explore -> /home
// Using switch case if there comes more pages that this should be done to
if (
item.redirectExplore == null &&
item.redirectExplore == "true" &&
item.redirectExplore === true
) {
switch (window.location.pathname) {
case "/explore":
url.pathname = "/home";
window.location.replace(url.toString());
break;
}
}

// Then use Regex to find if a page is a user or a post
if (window.location.pathname.match(EXTERNAL_POST_REGEX)) {
url.pathname = `/authorize_interaction`;
url.searchParams.set("uri", window.location.href);
Expand Down Expand Up @@ -61,6 +77,10 @@ if (MASTODON_DIV) {
);
}

const getting = (browser || chrome).storage.sync.get(["url", "enabled"]);
const getting = (browser || chrome).storage.sync.get([
"url",
"enabled",
"redirectExplore",
]);
getting.then(onGot, onError);
}
26 changes: 23 additions & 3 deletions src/m4redirect.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var chrome;
// If someone knows a better way to detect Mastodon4 instances, please send me a message!
// [email protected]
const MASTODON_DIV = document.getElementById("mastodon");
const EXTERNAL_USER_REGEX = /(@[a-zA-Z0-9_]+)(?!.*@)/; // @username matches, @[email protected] doesn't
const EXTERNAL_USER_REGEX = /^(@[a-zA-Z0-9_]+)(?!.*@)(?!\/.+)$/; // @username matches, @[email protected] @username/following doesn't
const EXTERNAL_POST_REGEX = /(@[a-zA-Z0-9_]+)(?!.*@)\/(\d+)/; // @username matches, @[email protected] doesn't

if (MASTODON_DIV) {
Expand All @@ -29,7 +29,23 @@ if (MASTODON_DIV) {
) {
return; // Disabled
}
// Match users and posts and redirect if needed
// Lets first parse some generic URLs out of the way
// For example: /explore -> /home
// Using switch case if there comes more pages that this should be done to
if (
item.redirectExplore == null &&
item.redirectExplore == "true" &&
item.redirectExplore === true
) {
switch (window.location.pathname) {
case "/explore":
url.pathname = "/home";
window.location.replace(url.toString());
break;
}
}

// Then use Regex to find if a page is a user or a post
if (window.location.pathname.match(EXTERNAL_POST_REGEX)) {
url.pathname = `/authorize_interaction`;
url.searchParams.set("uri", window.location.href);
Expand Down Expand Up @@ -61,6 +77,10 @@ if (MASTODON_DIV) {
);
}

const getting = (browser || chrome).storage.sync.get(["url", "enabled"]);
const getting = (browser || chrome).storage.sync.get([
"url",
"enabled",
"redirectExplore",
]);
getting.then(onGot, onError);
}
5 changes: 5 additions & 0 deletions src/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ <h1>Thank you for using Mastodon4 Redirect!</h1>
<input placeholder="https://mastodon.social" type="url" id="url" name="url" />
</label>
<br />
<label>
<input type="checkbox" id="redirect_explore" name="redirect_explore" />
<b>Redirect /explore to /home</b>
</label>
<br />
<button type="submit">Save</button>
<br />
<p id="saved" style="display: none;color:green;">Saved!</p>
Expand Down
8 changes: 7 additions & 1 deletion src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function saveOptions(e) {
e.preventDefault();
(browser || chrome).storage.sync.set({
url: document.querySelector("#url").value,
redirectExplore: document.querySelector("#redirect_explore").checked,
});
document.getElementById("saved").style.display = "block";
}
Expand All @@ -14,13 +15,18 @@ function restoreOptions() {
function setCurrentChoice(result) {
document.querySelector("#url").value =
result.url || "https://mastodon.social";
document.querySelector("#redirect_explore").checked =
result.redirectExplore || result.redirectExplore == null ? true : false;
}

function onError(error) {
console.log(`Error: ${error}`);
}

let getting = (browser || chrome).storage.sync.get("url");
let getting = (browser || chrome).storage.sync.get([
"url",
"redirectExplore",
]);
getting.then(setCurrentChoice, onError);
}

Expand Down

0 comments on commit 6e2fa17

Please sign in to comment.