Skip to content

Commit

Permalink
localize create tag filter context menus; create constant for tag URL…
Browse files Browse the repository at this point in the history
… REGEX; better implementation of dynamic context menu functionality
  • Loading branch information
rthaut committed Sep 18, 2021
1 parent 2b2e7d0 commit a47774a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
13 changes: 12 additions & 1 deletion app/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -345,5 +345,16 @@
},
"CreateFiltersFromDeviation_Error_Instructions_HasError": {
"message": "Please refresh the page and try again. If the problem persists, please report it with the following information:"
},
"CreateKeywordFilterFromTag_ContextMenuLabel": {
"message": "Create Keyword Filter for this Tag"
},
"CreateKeywordFilterForTag_ContextMenuLabel": {
"message": "Create Keyword Filter for \"$TAG$\"",
"placeholders": {
"tag": {
"content": "$1"
}
}
}
}
}
34 changes: 22 additions & 12 deletions app/scripts/background/menus.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { AddFilter } from "./filters";
import { SHOW_FILTER_DEVIATION_MODAL } from "../constants/messages";
import { TAG_URL_REGEX } from "../constants/url";

export const MENUS = [
{
id: "filter-tag",
title: "Create Keyword Filter for this Tag",
title: browser.i18n.getMessage(
"CreateKeywordFilterFromTag_ContextMenuLabel"
),
contexts: ["link"],
targetUrlPatterns: ["*://*.deviantart.com/tag/*"],
},
Expand All @@ -14,7 +17,10 @@ export const MENUS = [
"CreateFiltersFromDeviation_ContextMenuLabel"
),
contexts: ["link"],
targetUrlPatterns: ["*://*.deviantart.com/*/art/*"],
targetUrlPatterns: [
"*://*.deviantart.com/*/art/*",
"*://*.deviantart.com/*/journal/*",
],
},
];

Expand All @@ -30,11 +36,12 @@ export const InitMenus = async () => {
console.error("Failed to setup context menus", ex);
}

try {
browser.contextMenus.onShown.addListener(OnMenuShown);
} catch (ex) {
// chrome doesn't support the onShown event, but we don't use it for major functionality, so just ignore it
void ex;
if (typeof browser.contextMenus.onShown !== "undefined") {
try {
browser.contextMenus.onShown.addListener(OnMenuShown);
} catch (ex) {
void ex;
}
}
};

Expand All @@ -46,9 +53,9 @@ export const InitMenus = async () => {
export const OnMenuClicked = (info, tab) => {
switch (info.menuItemId) {
case "filter-tag":
if (/\/tag\/([^\/]+)/i.test(info.linkUrl)) {
if (TAG_URL_REGEX.test(info.linkUrl)) {
// eslint-disable-next-line no-case-declarations
const keyword = /\/tag\/([^\/]+)/i.exec(info.linkUrl)[1];
const keyword = TAG_URL_REGEX.exec(info.linkUrl)[1];
AddFilter("keywords", { keyword, wildcard: false });
}
break;
Expand All @@ -74,11 +81,14 @@ export const OnMenuClicked = (info, tab) => {
*/
// TODO: match linkUrl RegExps used here to items in MENUS array? or derive the RegExp from the targetUrlPatterns?
export const OnMenuShown = (info, tab) => {
if (/\/tag\/([^\/]+)/i.test(info.linkUrl)) {
if (TAG_URL_REGEX.test(info.linkUrl)) {
// filter-tag menu
const keyword = /\/tag\/([^\/]+)/i.exec(info.linkUrl)[1];
const keyword = TAG_URL_REGEX.exec(info.linkUrl)[1];
UpdateMenuItem("filter-tag", {
title: `Create Keyword Filter for "${keyword}"`,
title: browser.i18n.getMessage(
"CreateKeywordFilterForTag_ContextMenuLabel",
keyword
),
});
}
};
Expand Down
3 changes: 3 additions & 0 deletions app/scripts/constants/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ export const PAGES = {
},
};

export const TAG_URL_REGEX = /\/tag\/([^\/]+)/i;

export default {
WILDCARD,
REGEX,
PAGES,
TAG_URL_REGEX,
};

0 comments on commit a47774a

Please sign in to comment.