{"record":{"id":"ff3e7ebf4dbbfb57","repo":"microsoft/playwright","slug":"passing-functions-requires-the-init-script-to-be-a","errorCode":null,"errorMessage":"Passing functions requires the init script to be a function","messagePattern":"Passing functions requires the init script to be a function","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/playwright-core/src/client/page.ts","lineNumber":977,"sourceCode":"      };\n      const result = await func(source, ...this._initializer.args.map(parseResult));\n      this._channel.resolve({ result: serializeArgument(result) }, kNoTimeout).catch(() => {});\n    } catch (e) {\n      this._channel.reject({ error: serializeError(e) }, kNoTimeout).catch(() => {});\n    }\n  }\n}\n\nfunction trimUrl(param: any): string | undefined {\n  if (isRegExp(param))\n    return `/${trimStringWithEllipsis(param.source, 50)}/${param.flags}`;\n  if (isString(param))\n    return `\"${trimStringWithEllipsis(param, 50)}\"`;\n}\n\nexport async function addInitScriptWithExposedFunctions(owner: Page | BrowserContext, script: Function | string | { path?: string, content?: string }, arg: any): Promise<DisposableStub> {\n  if (typeof script !== 'function')\n    throw new Error('Passing functions requires the init script to be a function');\n  const callbacks: { name: string, disposable: DisposableObject }[] = [];\n  const source = await owner._wrapApiCall(async () => {\n    return await initScriptSourceWithExposedFunctions(script, arg, async (name, callback) => {\n      const disposable = await owner._exposeCallbackBinding(name, callback);\n      callbacks.push({ name, disposable });\n    });\n  }, { internal: true });\n  const initScriptDisposable = DisposableObject.from((await owner._channel.addInitScript({ source }, kNoTimeout)).disposable);\n  return new DisposableStub(async () => {\n    for (const { name, disposable } of callbacks) {\n      owner._bindings.delete(name);\n      disposable.dispose().catch(() => {});\n    }\n    await initScriptDisposable.dispose();\n  });\n}\n","sourceCodeStart":959,"sourceCodeEnd":994,"githubUrl":"https://github.com/microsoft/playwright/blob/c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6/packages/playwright-core/src/client/page.ts#L959-L994","documentation":"Error \"Passing functions requires the init script to be a function\" thrown in microsoft/playwright.","triggerScenarios":"Thrown at packages/playwright-core/src/client/page.ts:977 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6","analyzedAt":"2026-08-12T07:26:36.950Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}