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

eval: Support evaluating variables out of scope of the callback #67

Closed
4 tasks
ebebbington opened this issue Jun 14, 2021 · 3 comments · Fixed by #122
Closed
4 tasks

eval: Support evaluating variables out of scope of the callback #67

ebebbington opened this issue Jun 14, 2021 · 3 comments · Fixed by #122
Assignees

Comments

@ebebbington
Copy link
Member

Credit to @saragee3, as I had the initial discussion with her and we came to the conclusion below.

This issue is open to suggestions/criticism, and needs to be vetted/investigated more before work can properly start

Summary

What:

Support using variables/data defined outside the scope of the callback, to be used inside the callback, for example:

const selectors = {
  userForm: "form#user-form"
}
await Sinco.evaluatePage((args) => {
  return document.querySelector(args.selectors.userForm
}, selectors

Could use the arguments in https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-callFunctionOn ?

Why:

Because the environment that the command is ran is, is completely different, the chrome API has no knowledge of variables out of scope, as far is it's concerned, (using first example above), selectors is an undefined variable

Acceptance Criteria

Below is a list of tasks that must be completed before this issue can be closed.

  • Write documentation
  • Write unit tests
  • Write integration tests
  • {add more checkboxes required by this issue as needed}

Example Pseudo Code (for implementation)

type Callback = (args: Record<string, unknown) => any
type EvaluatePage = ( (args: , ...args: unknown[]) => any

function evaluatePage (cb: Callback, args: unknown[]): any {
}

evaluatePage((args) => {
  return document.querySelector(args.selectors.user
}, selectors
@saragee3
Copy link
Member

quick note but otherwise looks good 😄 User should be able to access args with dot notation so lets make it a object and not array.

is this how you kids do TS typings? 😜

interface SomeStuffArgs { [key: string]: string; }

function evaluatePage (cb: Callback, args: SomeStuffArgs): any {
    // some more code stuff
}

evaluatePage((args) => {
  return document.querySelector(args.selectors.edOrEddOrEddy);
}, { selectors });

@saragee3 saragee3 self-assigned this Jun 16, 2021
@ebebbington
Copy link
Member Author

Note that we can do

const cmd = `console.log(${name})`
await Sinco.evaluatePage(cmd)

It's just this data becomes out of scope when using the callback

@ebebbington
Copy link
Member Author

we actually should be able to do this with using an fn passed to evalatePage - the callMethodOn accepts args: https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-callFunctionOn

Like how puppeteer does it: await page.evaliate(() => { return document.querySelector(selector) }, selector)|evalauate(pageFn: Function, ...args: string[]

Im guessing they'll then use the args for the callFunctionOn method. So we should be able to do something like

const selector = "#name"
await sincol.evaluatePage(() => {
  return docu.selector(selector)
}, selector)

And inside our own evaluatePage fn, we:

const { result = await this.sendWebSocketMessage)"Runtime.callFunctionOn", {
  ...,
+args: args
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

3 participants