Skip to content

Commit

Permalink
#3200: run brick in top frame of window (#3201)
Browse files Browse the repository at this point in the history
  • Loading branch information
twschiller authored Apr 19, 2022
1 parent a5767b3 commit f3355f3
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 16 deletions.
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);
}

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

0 comments on commit f3355f3

Please sign in to comment.