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

#3200: run brick in top frame of window #3201

Merged
merged 1 commit into from
Apr 19, 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
51 changes: 36 additions & 15 deletions src/background/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ export async function waitForTargetByUrl(url: string): Promise<Target> {
return promise;
}

/**
* Run a brick in the window that opened the source window
* @param request
*/
export async function requestRunInOpener(
this: MessengerMeta,
request: RunBlock
Expand All @@ -91,6 +95,38 @@ export async function requestRunInOpener(
return safelyRunBrick(opener, subRequest);
}

/**
* Run a brick in the last window that was opened from the source window
* @see openTab
*/
export async function requestRunInTarget(
this: MessengerMeta,
request: RunBlock
): Promise<unknown> {
const sourceTabId = this.trace[0].tab.id;
const target = tabToTarget.get(sourceTabId);

if (!target) {
throw new BusinessError("Sender tab has no target");
}

const subRequest = { ...request, sourceTabId };
return safelyRunBrick({ tabId: target }, subRequest);
}

/**
* Run a brick in the topmost frame of the window/tab
*/
export async function requestRunInTop(
this: MessengerMeta,
request: RunBlock
): Promise<unknown> {
const sourceTabId = this.trace[0].tab.id;

const subRequest = { ...request, sourceTabId };
return safelyRunBrick({ tabId: sourceTabId }, subRequest);
Comment on lines +124 to +127
Copy link
Contributor

@fregante fregante Apr 19, 2022

Choose a reason for hiding this comment

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

Looks about right. I left some notes on a possible messenger implementation:

This would let content scripts call this directly:

runBrick({ tabId: "this" }, request)

and the messenger would take care of the forwarding. safelyRunBrick wouldn't be necessary because if the tab is closed, then the main runner also closes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, will give those notes a look

}

export async function requestRunInBroadcast(
this: MessengerMeta,
request: RunBlock
Expand Down Expand Up @@ -124,21 +160,6 @@ export async function requestRunInBroadcast(
return [...fulfilled].map(([, value]) => value);
}

export async function requestRunInTarget(
this: MessengerMeta,
request: RunBlock
): Promise<unknown> {
const sourceTabId = this.trace[0].tab.id;
const target = tabToTarget.get(sourceTabId);

if (!target) {
throw new BusinessError("Sender tab has no target");
}

const subRequest = { ...request, sourceTabId };
return safelyRunBrick({ tabId: target }, subRequest);
}

export async function openTab(
this: MessengerMeta,
createProperties: Tabs.CreateCreatePropertiesType
Expand Down
1 change: 1 addition & 0 deletions src/background/messenger/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export const requestRun = {
onServer: getMethod("REQUEST_RUN_ON_SERVER", bg),
inOpener: getMethod("REQUEST_RUN_IN_OPENER", bg),
inTarget: getMethod("REQUEST_RUN_IN_TARGET", bg),
inTop: getMethod("REQUEST_RUN_IN_TOP", bg),
inAll: getMethod("REQUEST_RUN_IN_ALL", bg),
};

Expand Down
3 changes: 3 additions & 0 deletions src/background/messenger/registration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
requestRunInTarget,
requestRunInBroadcast,
waitForTargetByUrl,
requestRunInTop,
} from "@/background/executor";
import * as registry from "@/registry/localRegistry";
import { ensureContentScript } from "@/background/util";
Expand Down Expand Up @@ -105,6 +106,7 @@ declare global {
REQUEST_RUN_ON_SERVER: typeof requestRunOnServer;
REQUEST_RUN_IN_OPENER: typeof requestRunInOpener;
REQUEST_RUN_IN_TARGET: typeof requestRunInTarget;
REQUEST_RUN_IN_TOP: typeof requestRunInTop;
REQUEST_RUN_IN_ALL: typeof requestRunInBroadcast;

HTTP_REQUEST: typeof serializableAxiosRequest;
Expand Down Expand Up @@ -174,6 +176,7 @@ export default function registerMessenger(): void {
REQUEST_RUN_ON_SERVER: requestRunOnServer,
REQUEST_RUN_IN_OPENER: requestRunInOpener,
REQUEST_RUN_IN_TARGET: requestRunInTarget,
REQUEST_RUN_IN_TOP: requestRunInTop,
REQUEST_RUN_IN_ALL: requestRunInBroadcast,

HTTP_REQUEST: serializableAxiosRequest,
Expand Down
9 changes: 8 additions & 1 deletion src/blocks/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,18 @@ export type ReaderConfig =
* - self: the current tab
* - opener: the tab that opened the current tab
* - target: the last tab that the current tab opened
* - top: the top-most frame in the window
* - broadcast: all tabs that PixieBrix has access to (the result is returned as an array)
* - remote: the server (currently only support identity, get, and http bricks)
* @see {@link BlockConfig.window}
*/
export type BlockWindow = "self" | "opener" | "target" | "broadcast" | "remote";
export type BlockWindow =
| "self"
| "opener"
| "target"
| "top"
| "broadcast"
| "remote";

/**
* Condition expression written in templateEngine for deciding if the step should be run.
Expand Down
1 change: 1 addition & 0 deletions src/pageEditor/tabs/effect/BlockConfiguration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const targetOptions: Array<Option<BlockWindow>> = [
{ label: "Current Tab (self)", value: "self" },
{ label: "Opener Tab (opener)", value: "opener" },
{ label: "Target Tab (target)", value: "target" },
{ label: "Top-level Frame (top)", value: "top" },
{ label: "All Tabs (broadcast)", value: "broadcast" },
{ label: "Server (remote)", value: "remote" },
];
Expand Down
4 changes: 4 additions & 0 deletions src/runtime/reducePipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ async function execute(
return requestRun.inTarget(request);
}

case "top": {
return requestRun.inTop(request);
}

case "broadcast": {
return requestRun.inAll(request);
}
Expand Down