Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for search parameters in named targets (?id=UUID) #57

Merged
merged 3 commits into from
Feb 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 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
@@ -1,6 +1,6 @@
{
"name": "webext-messenger",
"version": "0.16.0",
"version": "0.17.0-0",
"description": "Browser Extension component messaging framework",
"keywords": [],
"repository": "pixiebrix/webext-messenger",
Expand Down
34 changes: 29 additions & 5 deletions source/thisTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,39 @@ import { messenger } from "./sender.js";
import { registerMethods } from "./receiver.js";
import { AnyTarget, MessengerMeta, Sender } from "./types.js";
import { debug } from "./shared.js";
import { Entries } from "type-fest";

// Soft warning: Race conditions are possible.
// This CANNOT be awaited because waiting for it means "I will handle the message."
// If a message is received before this is ready, it will just have to be ignored.
let thisTarget: AnyTarget | undefined;

function compareTargets(to: AnyTarget, thisTarget: AnyTarget): boolean {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked into using URLPattern to compare the URLs, but unfortunately that API does not care for search parameters because it treats them as a plain string so only exact matches are possible.

This function instead will match the target a.html?id=1 even if the current page is a.html?something=else&id=1

for (const [key, value] of Object.entries(to) as Entries<typeof to>) {
if (thisTarget[key] === value) {
continue;
}

if (key !== "page") {
return false;
}

const toUrl = new URL(to.page!, location.origin);
const thisUrl = new URL(thisTarget.page!, location.origin);
if (toUrl.pathname !== thisUrl.pathname) {
return false;
}

for (const [parameterKey, parameterValue] of toUrl.searchParams) {
if (thisUrl.searchParams.get(parameterKey) !== parameterValue) {
return false;
}
}
}

return true;
}

export function getActionForMessage(
from: Sender,
{ ...to }: AnyTarget // Clone object because we're editing it
Expand Down Expand Up @@ -44,10 +71,7 @@ export function getActionForMessage(
}

// Every `target` key must match `thisTarget`
const isThisTarget = Object.entries(to).every(
// @ts-expect-error Optional properties
([key, value]) => thisTarget[key] === value
);
const isThisTarget = compareTargets(to, thisTarget);

if (!isThisTarget) {
debug("The message’s target is", to, "but this is", thisTarget);
Expand All @@ -62,7 +86,7 @@ export async function nameThisTarget() {
if (!nameRequested && !thisTarget && !isContentScript()) {
nameRequested = true;
thisTarget = await messenger("__getTabData", {}, { page: "any" });
thisTarget.page = location.pathname;
thisTarget.page = location.pathname + location.search;
}
}

Expand Down