diff --git a/src/background/modules/AutoRemoteFollow.js b/src/background/modules/AutoRemoteFollow.js index 3ee6499..6648ece 100644 --- a/src/background/modules/AutoRemoteFollow.js +++ b/src/background/modules/AutoRemoteFollow.js @@ -5,13 +5,20 @@ */ import {INTERACTION_TYPE} from "./data/INTERACTION_TYPE.js"; + import * as MastodonDetect from "./Detect/Mastodon.js"; +import * as GnuSocialDetect from "./Detect/GnuSocial.js"; + import * as MastodonRedirect from "./MastodonRedirect.js"; const FEDIVERSE_TYPE = Object.freeze({ MASTODON: Symbol("mastodon"), GNU_SOCIAL: Symbol("GNU Social") }); +const FEDIVERSE_MODULE = Object.freeze({ + [FEDIVERSE_TYPE.MASTODON]: MastodonDetect, + [FEDIVERSE_TYPE.GNU_SOCIAL]: GnuSocialDetect +}); /** * Listens for Mastodon requests at tab update. @@ -42,6 +49,9 @@ async function handleTabUpdate(tabId, changeInfo) { case FEDIVERSE_TYPE.MASTODON: detectModule = MastodonDetect; break; + case FEDIVERSE_TYPE.GNU_SOCIAL: + detectModule = GnuSocialDetect; + break; default: throw new Error(`unknown fediverse type: ${software.toString()}`); } @@ -73,9 +83,11 @@ async function handleTabUpdate(tabId, changeInfo) { * @returns {[FEDIVERSE_TYPE, Symbol]|null} */ function getInteractionType(url) { - for (const [checkRegEx, interactionType] of MastodonDetect.CATCH_URLS) { - if (url.pathname.match(checkRegEx)) { - return [FEDIVERSE_TYPE.MASTODON, interactionType]; + for (const fedType of Object.values(FEDIVERSE_TYPE)) { + for (const [checkRegEx, interactionType] of FEDIVERSE_MODULE[fedType].CATCH_URLS) { + if (url.pathname.match(checkRegEx)) { + return [fedType, interactionType]; + } } } diff --git a/src/background/modules/Detect/GnuSocial.js b/src/background/modules/Detect/GnuSocial.js new file mode 100644 index 0000000..81ed8f3 --- /dev/null +++ b/src/background/modules/Detect/GnuSocial.js @@ -0,0 +1,50 @@ +/** + * Module, that detects a GNU Social instance and returns the required values. + * + * @module Detect/GnuSocial + */ + +import {NotSupportedError} from "/common/modules/Errors.js"; +import {INTERACTION_TYPE} from "../data/INTERACTION_TYPE.js"; + +// https://regex101.com/r/8vTzty/1 +const REMOTE_FOLLOW_REGEX = /\/main\/ostatus\/nickname\/(.+)\/?$/; + +/** The URLs to intercept and pass to this module. */ +export const CATCH_URLS = new Map(); +CATCH_URLS.set(REMOTE_FOLLOW_REGEX, INTERACTION_TYPE.FOLLOW); + +/** + * Find the follow URL. + * + * @function + * @private + * @returns {Promise} + */ +export function getTootUrl() { + throw new NotSupportedError("getTootUrl() is not supported"); +} + +/** + * Returns the username of the given OStatus page. + * + * @private + * @function + * @param {URL} url + * @returns {string|undefined} + */ +export function getUsername(url) { + const match = REMOTE_FOLLOW_REGEX.exec(url.pathname); + return match[1]; +} + +/** + * Returns the server from the required URL. + * + * @function + * @param {URL} url + * @returns {string|undefined} + */ +export function getServer(url) { + return url.host; +}