Skip to content

Commit

Permalink
refactor(options): use enums; get template from background script
Browse files Browse the repository at this point in the history
  • Loading branch information
7sDream committed Apr 21, 2021
1 parent 5973a7c commit c99bb87
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 22 deletions.
34 changes: 32 additions & 2 deletions src/background/menu.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MENU_ID } from '../const.js';
import { MENU_ID, OPTIONS, OPTIONS_DEFAULT_VALUE } from '../const.js';
import { assertCmdType, Cmd, Command } from '../types/commands.js';

browser.menus.create({
Expand Down Expand Up @@ -29,6 +29,23 @@ const openSendPanel = async (tab: browser.tabs.Tab, content: string) => {
console.log(`[firelomo] [background] send active panel message to tab ${tab.id} success`);
}

const getTemplate = async (name: OPTIONS): Promise<string> => {
switch (name) {
case OPTIONS.SELECTION_TEMPLATE:
case OPTIONS.PAGE_URL_TEMPLATE:
case OPTIONS.LINK_URL_TEMPLATE: {
return (await browser.storage.sync.get({ [name]: OPTIONS_DEFAULT_VALUE[name] }))[name];
}
}
console.error(`[firelomo] [background] unknown template: ${name}`);
return "";
};

const templateFill = async (name: OPTIONS, context: Record<string, string>): Promise<string> => {
const template = await getTemplate(name);
return template;
};

browser.menus.onClicked.addListener(async (info, tab) => {
if (info.menuItemId === MENU_ID.SEND_TO_FLOMO) {
try {
Expand All @@ -38,7 +55,20 @@ browser.menus.onClicked.addListener(async (info, tab) => {
return;
}

const content = info.linkUrl ?? info.selectionText ?? info.pageUrl ?? "";
const pageTitle = tab.title ?? "";
const pageUrl = tab.url ?? "";

let content = "";
if (info.selectionText) {
content = await templateFill(OPTIONS.SELECTION_TEMPLATE, { selection: info.selectionText, pageTitle, pageUrl });
} else if (info.linkUrl) {
content = await templateFill(OPTIONS.LINK_URL_TEMPLATE, { linkUrl: info.linkUrl, linkText: info.linkText ?? "", pageTitle, pageUrl });
} else if (info.pageUrl) {
content = await templateFill(OPTIONS.PAGE_URL_TEMPLATE, { pageTitle, pageUrl });
} else {
console.error("[firelomo] [background] received client event with no usable content");
return;
}

try {
await openSendPanel(tab, content);
Expand Down
16 changes: 16 additions & 0 deletions src/const.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
export enum MENU_ID {
SEND_TO_FLOMO = "FIRELOMO_MENU_ID_SEND_TO_FLOMO",
};

export enum OPTIONS {
API_URL = "apiUrl",
TIMEOUT = "timeout",
SELECTION_TEMPLATE = "selectionTemplate",
PAGE_URL_TEMPLATE = "pageUrlTemplate",
LINK_URL_TEMPLATE = "linkUrlTemplate",
}

export const OPTIONS_DEFAULT_VALUE = {
[OPTIONS.API_URL]: "",
[OPTIONS.TIMEOUT]: 3000,
[OPTIONS.SELECTION_TEMPLATE]: "{selection}\n\n#firelomo",
[OPTIONS.PAGE_URL_TEMPLATE]: "{pageUrl}\n\n#firelomo",
[OPTIONS.LINK_URL_TEMPLATE]: "{linkText}\n{linkUrl}\n\n#firelomo",
};
5 changes: 4 additions & 1 deletion src/content/sendPanel/sendPanel.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { OPTIONS, OPTIONS_DEFAULT_VALUE } from '../../const.js';
import { assertCmdType, Cmd, Command } from '../../types/commands.js';

const checkGuard = (window: Window, guardName: string): boolean => {
Expand Down Expand Up @@ -26,7 +27,9 @@ const enable = (element: Element) => {
element.removeAttribute("disabled");
}

const getApiUrl = async () => (await browser.storage.sync.get({ apiUrl: "" })).apiUrl;
const getApiUrl = async () => (await browser.storage.sync.get({
[OPTIONS.API_URL]: OPTIONS_DEFAULT_VALUE[OPTIONS.API_URL]
})).apiUrl;

const send = async (content: string, apiUrl: string) => {
let abortController = new AbortController();
Expand Down
36 changes: 17 additions & 19 deletions src/pages/options/options.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { OPTIONS, OPTIONS_DEFAULT_VALUE } from '../../const.js';

let Form: HTMLFormElement;
let ApiUrlInput: HTMLInputElement;
let TimeoutInput: HTMLInputElement;
Expand All @@ -6,33 +8,29 @@ let PageUrlTemplateInput: HTMLTextAreaElement;
let LinkUrlTemplateInput: HTMLTextAreaElement;
let SubmitButton: HTMLButtonElement;

const DefaultSelectionTemplate = "{selection}\n\n#firelomo";
const DefaultPageUrlTemplate = "{pageUrl}\n\n#firelomo";
const DefaultLinkUrlTemplate = "{linkText}\n{linkUrl}\n\n#firelomo";


const showCurrentOptions = async () => {
const options = await browser.storage.sync.get({
apiUrl: "",
timeout: 3000,
selectionTemplate: DefaultSelectionTemplate,
pageUrlTemplate: DefaultPageUrlTemplate,
linkUrlTemplate: DefaultLinkUrlTemplate,
});
ApiUrlInput.value = options.apiUrl;
TimeoutInput.value = options.timeout.toString();
SelectionTemplateInput.value = options.selectionTemplate;
PageUrlTemplateInput.value = options.pageUrlTemplate;
LinkUrlTemplateInput.value = options.linkUrlTemplate;
const options = await browser.storage.sync.get(OPTIONS_DEFAULT_VALUE);
ApiUrlInput.value = options[OPTIONS.API_URL];
TimeoutInput.value = options[OPTIONS.TIMEOUT].toString();
SelectionTemplateInput.value = options[OPTIONS.SELECTION_TEMPLATE];
PageUrlTemplateInput.value = options[OPTIONS.PAGE_URL_TEMPLATE];
LinkUrlTemplateInput.value = options[OPTIONS.LINK_URL_TEMPLATE];
};

const updateOptions = async () => {
const apiUrl = ApiUrlInput.value;
const timeout = parseInt(TimeoutInput.value || "3000", 10);
const timeout = parseInt(TimeoutInput.value || OPTIONS_DEFAULT_VALUE[OPTIONS.TIMEOUT].toString(), 10);
const selectionTemplate = SelectionTemplateInput.value;
const pageUrlTemplate = PageUrlTemplateInput.value;
const linkUrlTemplate = LinkUrlTemplateInput.value;
await browser.storage.sync.set({ apiUrl, timeout, selectionTemplate, pageUrlTemplate, linkUrlTemplate });

await browser.storage.sync.set({
[OPTIONS.API_URL]: apiUrl,
[OPTIONS.TIMEOUT]: timeout,
[OPTIONS.SELECTION_TEMPLATE]: selectionTemplate,
[OPTIONS.PAGE_URL_TEMPLATE]: pageUrlTemplate,
[OPTIONS.LINK_URL_TEMPLATE]: linkUrlTemplate,
});
};

const updateFormSubmitButtonState = () => {
Expand Down

0 comments on commit c99bb87

Please sign in to comment.