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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert javascript snippets to standalone embedded js files (part 1) #852

Merged
merged 5 commits into from Jul 12, 2021

Conversation

ZekeLu
Copy link
Member

@ZekeLu ZekeLu commented Jun 8, 2021

The purpose is to make it easy to maintain the js source code.

This PR contains three parts:

  1. refactoring:
    • use dom.ScrollIntoViewIfNeeded() to replace the js implementation
    • extract parseRemoteObject function and improve the doc
    • encapsulate CallFunctionOn and refactor the poll action to use the this func
  2. replace Evaluate with CallFunctionOn to execuate js on node. Pros and cons:
    • 馃憤 pro: use valid js functions instead of js snippets (a prerequisite to move js to standalone file)
    • 馃憤 pro: easy to pass arguments to js functions (no constructing js code dynamically any more)
    • 馃憥 con: CallFunctionOn needs 3 commands to execute js on a node (while Evaluate just needs one):
      • DOM.resolveNode
      • Runtime.callFunctionOn
      • Runtime.releaseObject.
  3. move js function into standalone js files and load them with //go:embed
    • //go:embed is a new feature in go 1.16, so we should put this PR on hold until we decide to drop support for go 1.15 (maybe when go 1.17 is released?).

Maybe it's better to split this PR into 3 PRs.

Update: We will split it into 2 PRs. Item 1 and item 2 will be released first. Item 3 will be put into another PR and it's planed to be released once go 1.17 is released.

The advantages of runtime.CallFunctionOn are:

1. it's safe to pass arguments into js;
2. the js function can be bound to a remote object (accessed by "this").

So that we can use runtime.CallFunctionOn to run js function without
constructing js dynamically.
Note: there is a performance cost here.

Before the changes, just one CDP command is sent:
- Runtime.evaluate

After the changes, three CDP command are sent:
- DOM.resolveNode: to get the RemoteObjectId from NodeId
- Runtime.callFunctionOn
- Runtime.releaseObject
@ZekeLu ZekeLu changed the title convert javascript snippets to standalone embedded js files convert javascript snippets to standalone embedded js files (part 1) Jul 12, 2021
@ZekeLu ZekeLu merged commit eb823ff into chromedp:master Jul 12, 2021
@ZekeLu ZekeLu deleted the js branch July 12, 2021 17:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant