0

So I am trying to inject a script to run on any page using addScriptToEvaluateOnNewDocument on chrome 79, but it doesn't seem to be working.

I am using the ruby gem chrome_remote, which gives a pretty basic access to the CDP.

Here is an example ruby:

scpt =<<EOF
    window.THIS_WAS_SET = 1
EOF

ChromeRemote.client.send_cmd 'Page.addScriptToEvaluateOnNewDocument',{source: scpt}
ChromeRemote.client.send_cmd "Page.navigate", url: "http://localhost:4567/test"

I then start chrome with --remote-debugging-port=9222

The Page.addScriptToEvaluateOnNewDocument will always return {"identifier"=>"1"} (even if I call it multiple times, say with different scripts).

And when I open console on the opened tab in Chrome (which works, so I know CDP in general is working), and check the value of window.THIS_WAS_SET, it is undefined.

Is there any way to verify the command was sent to the browser, such as a log in the browser it was received? Any way to see what scripts were injected? Why does each call always return a ScriptIdentifier of 1, that seems problematic?

Anyone have a similar example working?

Dan Pisarski
  • 337
  • 1
  • 2
  • 10
  • BTW, I am not using the headless flag or mode, this is a full browser just being remote controlled. I did try it with and without the additional `--enable-automation` flag and it made no difference to the behavior above. – Dan Pisarski Jan 17 '20 at 20:15

1 Answers1

6

you should call "page.enable" first.

yinan
  • 61
  • 5
  • 1
    When you leave an answer it is good practice to make sure that the solution is easily understood. It would be helpful with an example that includes your answer along with an explanation. – Philip Feb 22 '20 at 12:08