MCPcopy Create free account
hub / github.com/Kilo-Org/cloud / evalInTab

Function evalInTab

apps/extension/src/shared/tab-debugger.ts:706–768  ·  view source on GitHub ↗
({
  code,
  debuggerApi,
  tabId,
  timeoutMs = DEFAULT_EVAL_TIMEOUT_MS,
}: {
  readonly code: string;
  readonly debuggerApi: ChromeDebuggerApi;
  readonly tabId: number;
  readonly timeoutMs?: number;
})

Source from the content-addressed store, hash-verified

704 */
705export const getInspectableTab = async ({
706 tabId,
707 tabsApi,
708}: {
709 readonly tabId: number;
710 readonly tabsApi: BrowserTabsApi;
711}): Promise<InspectableTabResolution> => {
712 const getTab = tabsApi.get?.bind(tabsApi);
713
714 if (getTab === undefined) {
715 return { ok: true, tab: undefined };
716 }
717
718 try {
719 const tab = await getTab(tabId);
720
721 return isInspectablePageUrl(tab.url)
722 ? { ok: true, tab }
723 : { error: TAB_NOT_INSPECTABLE_ERROR, ok: false };
724 } catch {
725 // A closed or never-existing tab id is not inspectable.
726 return { error: TAB_NOT_INSPECTABLE_ERROR, ok: false };
727 }
728};
729
730const getPngDimensions = (dataUrl: string): { height: number; width: number } | undefined => {
731 try {
732 const bytes = Uint8Array.from(
733 atob(dataUrl.slice('data:image/png;base64,'.length)),
734 character => character.codePointAt(0) ?? 0
735 );
736 const view = new DataView(bytes.buffer);
737
738 return { height: view.getUint32(20), width: view.getUint32(16) };
739 } catch {
740 return undefined;
741 }
742};
743
744// One global capture lock since captureVisibleTab grabs whichever tab is active in a window, so the activate/capture/restore window must not interleave (screenshots are rare; key per-window only if throughput matters).
745const ignoreSettled = (): void => {};
746// eslint-disable-next-line promise/prefer-await-to-then
747let screenshotCaptureChain: Promise<unknown> = Promise.resolve();
748const runScreenshotCaptureExclusively = <Result>(task: () => Promise<Result>): Promise<Result> => {
749 // The promise chain is the mutex; keep it alive after either outcome.
750 // eslint-disable-next-line promise/prefer-await-to-then
751 const run = screenshotCaptureChain.then(task, task);
752
753 // eslint-disable-next-line promise/prefer-await-to-then
754 screenshotCaptureChain = run.then(ignoreSettled, ignoreSettled);
755
756 return run;
757};
758
759const viewportScreenshotOf = (dataUrl: string): EvalTabResult => {
760 if (!dataUrl.startsWith('data:image/png;base64,')) {
761 return { error: 'Viewport screenshot API returned an invalid image.', ok: false };
762 }
763 const dimensions = getPngDimensions(dataUrl);

Callers 2

handleTabDebuggerRequestFunction · 0.90

Calls 3

getEvalExpressionFunction · 0.85
getExceptionMessageFunction · 0.85
toSerializableEvalResultFunction · 0.85

Tested by

no test coverage detected