您如何使用剧作家在浏览器上下文中导入本地模块?

发布于 2025-01-22 13:35:34 字数 404 浏览 4 评论 0 原文

这是我要做的事情的一个示例:

../ src/foo/bar.ts 是本地打字稿文件,

该文件中的代码旨在在浏览器上下文中运行(IT访问它 websockets ),因此我需要在 page.evaluate

page.evaluate( async () => {
    const { namedExportExample } = await import('../src/Foo/Bar;)

    // ...
});

我获取 page.evaluate:typeError:无法解决模块指定器和i''毫不奇怪,我不确定浏览器如何评估该导入,但是我想知道是否有办法执行此操作?

Here's an example of what I'm trying to do:

../src/Foo/Bar.ts is a local typescript file

The code in the file is meant to run in a browser context (it accesses WebSockets), so I need to run it in page.evaluate

page.evaluate( async () => {
    const { namedExportExample } = await import('../src/Foo/Bar;)

    // ...
});

I get page.evaluate: TypeError: Failed to resolve module specifier and I'm not surprised, I'm not sure how the browser can evaluate that import, but I'd like to know if there's a way to do this if possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幼儿园老大 2025-01-29 13:35:34

我不确定浏览器如何评估该导入,但是我想知道是否有办法执行此操作?

由于 @Max-Schmitt解释的原因,您无法导入模块。但是剧作家的“上下文”提供了一种在浏览器实例中注入新功能的方法。

browserContext.exposeFunction(name, callback)

示例
import * as crypto from "node:crypto" 

const browser = await chromium.launch(...)
const context = await browser.newContext()
    
// !!! Exposing a new function !!!
await context.exposeFunction("sha256", text => crypto.createHash('sha256').update(text).digest("hex")) 

const page = await context.newPage()
await page.goto(URL)

...
await someBox.evaluate((box : HTMLElement) => {
  console.log("!!!", window["sha256"]) // available !!!
  ...
})

I'm not sure how the browser can evaluate that import, but I'd like to know if there's a way to do this if possible?

You can't import modules for reasons explained by @max-schmitt. But Playwright's "Context" provides a way to inject new functions in the browser instance.

browserContext.exposeFunction(name, callback)

https://playwright.dev/docs/api/class-browsercontext#browser-context-expose-function

Example
import * as crypto from "node:crypto" 

const browser = await chromium.launch(...)
const context = await browser.newContext()
    
// !!! Exposing a new function !!!
await context.exposeFunction("sha256", text => crypto.createHash('sha256').update(text).digest("hex")) 

const page = await context.newPage()
await page.goto(URL)

...
await someBox.evaluate((box : HTMLElement) => {
  console.log("!!!", window["sha256"]) // available !!!
  ...
})
拧巴小姐 2025-01-29 13:35:34

剧作家脚本在您的剧作家环境中运行。您的页面
脚本在浏览器页面环境中运行。这些环境没有
相交,它们在不同的虚拟机中运行
过程,甚至可能在不同的计算机上。

在您的情况下,您尝试将文件导入页面内,这对该文件的何处不了解。您需要将功能串起并将其传递给页面以进行评估。

有关更多信息,请参见此处: https://playwright.dev/docs/evaluating

Playwright scripts run in your Playwright environment. Your page
scripts run in the browser page environment. Those environments don't
intersect, they are running in different virtual machines in different
processes and even potentially on different computers.

In your scenario you try to import your file inside the page, which does not know anything about where to import that file. You need to stringify your function and pass it over the page to evaluate it.

See here for more information: https://playwright.dev/docs/evaluating

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文