Skip to content

Commit 4fe50b5

Browse files
refactor storage. add automatic redirect config
1 parent ceeccf1 commit 4fe50b5

File tree

10 files changed

+87
-89
lines changed

10 files changed

+87
-89
lines changed

package-lock.json

+3-41
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
"devDependencies": {
33
"@types/firefox": "^0.0.34",
44
"@types/firefox-webext-browser": "^111.0.4",
5-
"@types/react": "^18.2.37",
6-
"@types/react-dom": "^18.2.15",
75
"esbuild": "^0.19.5",
86
"typescript": "^5.2.2",
97
"web-ext": "^7.8.0",
@@ -19,5 +17,8 @@
1917
},
2018
"author": "MaddyUnderStars",
2119
"license": "AGPL-3.0-or-later",
22-
"description": "Redirect fediverse links to your home instance"
20+
"description": "Redirect fediverse links to your home instance",
21+
"dependencies": {
22+
"deepmerge": "^4.3.1"
23+
}
2324
}

public/settings.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@ <h3>Mastodon</h3>
6767
</div>
6868
</div>
6969

70-
<!-- <div class="section">
70+
<div class="section">
7171
<h2>Settings</h2>
72-
<form>
72+
<form id="settings">
7373
<label for="automatic_redirects">Automatically redirect:</label>
7474
<input type="checkbox" name="automatic_redirects" id="automatic_redirects">
7575
</form>
76-
</div> -->
76+
</div>
7777
</div>
7878

7979
<dialog id="code_dialog">

src/background.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { getHandlersForUrl } from "./lib/index";
2-
import { RedirectSettings } from "./types/mastodon";
3-
import { validateUrl } from "./utils";
2+
import { getSettings, validateUrl } from "./utils";
43

54
const handler = async () => {
65
const tab = (
@@ -16,10 +15,11 @@ const handler = async () => {
1615

1716
const handler = handlers[0];
1817

19-
const opts = (await browser.storage.local.get())[
20-
handler.type
21-
] as RedirectSettings;
22-
if (new URL(opts.instance).origin == url.origin) return;
18+
const opts = await getSettings();
19+
if (!opts.automatic_redirects) return;
20+
const handleropts = opts.handlers?.[handler.type];
21+
if (!handleropts?.instance) return;
22+
if (new URL(handleropts.instance).origin == url.origin) return;
2323

2424
const post = await handler.findRemote(url);
2525

src/lib/mastodon.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import {
2-
MastodonApplication,
3-
MastodonSearchResults,
4-
RedirectSettings,
5-
} from "../types/mastodon";
1+
import { MastodonApplication, MastodonSearchResults } from "../types/mastodon";
2+
import { getSettings } from "../utils";
63
import { Handler } from "./index";
74

85
const MASTODON_REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob";
@@ -34,8 +31,8 @@ const makeAuthUrl = (instance: URL, app: MastodonApplication) => {
3431
};
3532

3633
const findRemote = async (url: URL) => {
37-
const opts = (await browser.storage.local.get())
38-
.mastodon as RedirectSettings;
34+
const opts = (await getSettings())?.handlers?.mastodon;
35+
if (!opts?.instance) return;
3936
const requrl = new URL(opts.instance);
4037
requrl.pathname = "/api/v2/search";
4138
requrl.searchParams.append("q", url.toString());

src/settings/index.ts

+42-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import handlers from "../lib/index";
2-
import { RedirectSettings } from "../types/mastodon";
3-
import { validateUrl } from "../utils";
2+
import { getSettings, setSettings, validateUrl } from "../utils";
43

54
let currentlyHandling: string;
65
const dialog = document.getElementById("code_dialog") as HTMLDialogElement;
@@ -19,13 +18,15 @@ const handler = async (event: SubmitEvent) => {
1918

2019
const app = await handler.createApp(instance);
2120

22-
await browser.storage.local.set({
23-
[handler.type]: {
24-
code: undefined,
25-
instance: instance.toString(),
26-
client_id: app.client_id,
27-
client_secret: app.client_secret,
28-
} as RedirectSettings,
21+
await setSettings({
22+
handlers: {
23+
[handler.type]: {
24+
code: undefined,
25+
instance: instance.toString(),
26+
client_id: app.client_id,
27+
client_secret: app.client_secret,
28+
},
29+
},
2930
});
3031

3132
currentlyHandling = handler.type;
@@ -36,17 +37,17 @@ const handler = async (event: SubmitEvent) => {
3637
});
3738
};
3839

39-
const forms = document.querySelectorAll(
40+
const handlerforms = document.querySelectorAll(
4041
".handler form",
4142
) as NodeListOf<HTMLFormElement>;
42-
[...forms].forEach(async (x) => {
43+
[...handlerforms].forEach(async (x) => {
4344
x.addEventListener("submit", handler);
4445

45-
const opts = (await browser.storage.local.get())[
46+
const opts = (await getSettings())?.handlers?.[
4647
x.getAttribute("data-type")!
47-
] as RedirectSettings;
48+
];
4849

49-
if (opts.instance)
50+
if (opts?.instance)
5051
(x.elements.namedItem("url") as HTMLInputElement).value = opts.instance;
5152
});
5253

@@ -55,17 +56,36 @@ document
5556
.addEventListener("submit", async (event) => {
5657
event.preventDefault();
5758

58-
const opts = (await browser.storage.local.get())[
59-
currentlyHandling
60-
] as RedirectSettings;
59+
const opts = (await getSettings())?.handlers?.[currentlyHandling];
6160

6261
const data = new FormData(event.target as HTMLFormElement);
63-
await browser.storage.local.set({
64-
[currentlyHandling]: {
65-
...opts,
66-
code: data.get("code"),
67-
} as RedirectSettings,
62+
await setSettings({
63+
handlers: {
64+
[currentlyHandling]: {
65+
code: data.get("code")?.toString(),
66+
},
67+
},
6868
});
6969

7070
dialog.close();
7171
});
72+
73+
const settings = document.querySelectorAll(
74+
"#settings input",
75+
) as NodeListOf<HTMLInputElement>;
76+
[...settings].forEach(async (x) => {
77+
const key = x.getAttribute("name");
78+
if (!key) return alert("you forgot to set the input name");
79+
80+
(x as HTMLInputElement).addEventListener("change", async (event) => {
81+
const target = event.target as HTMLInputElement;
82+
let value: string | boolean = target.value;
83+
if (target.getAttribute("type") == "checkbox") value = target.checked;
84+
await setSettings({ [key]: value });
85+
});
86+
87+
const settings = await getSettings();
88+
const value = settings[key as keyof typeof settings]; // silly
89+
if (x.getAttribute("type") == "checkbox") x.checked = value as boolean;
90+
else x.value = value as any as string;
91+
});

src/types/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./mastodon";
2+
export * from "./settings";

src/types/mastodon.ts

-7
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,6 @@ export type MastodonApplication = {
88
vapid_key: string;
99
};
1010

11-
export type RedirectSettings = Partial<{
12-
code: string;
13-
instance: string;
14-
client_id: string;
15-
client_secret: string;
16-
}>;
17-
1811
export type MastodonStatus = {
1912
id: string;
2013
url: string;

src/types/settings.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export type HandlerSettings = Partial<{
2+
code: string;
3+
instance: string;
4+
client_id: string;
5+
client_secret: string;
6+
}>;
7+
8+
export type ExtensionSettings = Partial<{
9+
automatic_redirects: boolean;
10+
handlers: { [key: string]: HandlerSettings };
11+
}>;

src/utils.ts

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import merge from "deepmerge";
2+
import { ExtensionSettings } from "./types";
3+
14
export const validateUrl = (url: string | undefined | null) => {
25
if (!url) return undefined;
36
try {
@@ -6,3 +9,12 @@ export const validateUrl = (url: string | undefined | null) => {
69
return undefined;
710
}
811
};
12+
13+
export const getSettings = async (): Promise<ExtensionSettings> => {
14+
return await browser.storage.local.get();
15+
};
16+
17+
export const setSettings = async (settings: Partial<ExtensionSettings>) => {
18+
const old = await getSettings();
19+
await browser.storage.local.set(merge(old, settings));
20+
};

0 commit comments

Comments
 (0)