Skip to content

Commit

Permalink
#7300: Enable contextMenus on srcdoc iframes (#7305)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante authored Jan 18, 2024
1 parent 13f0d36 commit 784c10b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 28 deletions.
10 changes: 7 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
"webext-detect-page": "^5.0.0",
"webext-inject-on-install": "^2.0.0",
"webext-messenger": "^0.25.0-0",
"webext-patterns": "^1.3.0",
"webext-patterns": "^1.4.0",
"webext-permissions": "^3.1.2",
"webext-polyfill-kinda": "^1.0.2",
"webext-storage": "^1.2.2",
Expand Down
26 changes: 15 additions & 11 deletions src/bricks/available.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,22 @@ describe("testMatchPatterns", () => {
"https://www.example.com/*",
"https://*.pixiebrix.com/update/*",
];
expect(testMatchPatterns(patterns, "https://www.example.com")).toBeTrue();
expect(
testMatchPatterns(patterns, "https://pixiebrix.com/update/"),
).toBeTrue();
const test = testMatchPatterns;
expect(test(patterns, "https://www.example.com")).toBeTrue();
expect(test(patterns, "https://pixiebrix.com/update/")).toBeTrue();

expect(testMatchPatterns(patterns, "https://example.com")).toBeFalse();
expect(
testMatchPatterns(patterns, "https://www.example.comunication"),
).toBeFalse();
expect(
testMatchPatterns(patterns, "https://www.pixiebrix.com/"),
).toBeFalse();
expect(test(patterns, "https://example.com")).toBeFalse();
expect(test(patterns, "https://www.example.comunication")).toBeFalse();
expect(test(patterns, "https://www.pixiebrix.com/")).toBeFalse();
expect(test(patterns, "about:srcdoc")).toBeFalse();
});

test("can match <all_urls>", async () => {
const test = testMatchPatterns;

expect(test(["<all_urls>"], "https://www.example.com")).toBeTrue();
expect(test(["<all_urls>"], "about:srcdoc")).toBeTrue();
expect(test(["<all_urls>"], "chrome://extensions")).toBeFalse();
});

test("will throw BusinessError or invalid patterns", async () => {
Expand Down
24 changes: 12 additions & 12 deletions src/bricks/available.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { patternToRegex } from "webext-patterns";
import { doesUrlMatchPatterns, isValidPattern } from "webext-patterns";
import { castArray } from "lodash";
import { type Availability } from "@/bricks/types";
import { type Entries } from "type-fest";
Expand All @@ -26,19 +26,19 @@ export function testMatchPatterns(
patterns: string[],
url: string = document.location.href,
): boolean {
for (const pattern of patterns) {
try {
if (patternToRegex(pattern).test(url)) {
return true;
}
} catch {
throw new BusinessError(
`Pattern not recognized as valid match pattern: ${pattern}`,
);
}
if (url === "about:srcdoc") {
// <all_urls> doesn't officially include about:srcdoc, but it works in some cases
return patterns.includes("<all_urls>");
}

return false;
try {
return doesUrlMatchPatterns(url, ...patterns);
} catch {
const invalidPattern = patterns.find((pattern) => !isValidPattern(pattern));
throw new BusinessError(
`Pattern not recognized as valid match pattern: ${invalidPattern}`,
);
}
}

function testUrlPattern(
Expand Down
1 change: 0 additions & 1 deletion src/contentScript/contextMenus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { getReloadOnNextNavigate } from "@/contentScript/ready";

type MenuHandler = (args: Menus.OnClickData) => Promise<void>;

// TODO: Replace with `SimpleEventTarget` (e.g. target.emit(extensionId))
// eslint-disable-next-line local-rules/persistBackgroundData -- Functions
const handlers = new Map<UUID, MenuHandler>();

Expand Down

0 comments on commit 784c10b

Please sign in to comment.