Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.

Cookies are not set in ExecuteDevToolsMethod

Post Reply
Chinyaev
Posts: 38
Joined: Mon Mar 13, 2017 3:23 pm

Cookies are not set in ExecuteDevToolsMethod

Post by Chinyaev »

Hi, Salvador! I found some problem in ExecuteDevToolsMethod.

1. When I set a cookie with symbol "-" in the name, the cookie is not set.
2. If I remove the "-" symbol, cookies are set. Cookies are also set if it is escaped with "\-", but then I get "\\-" as a result, so it is perceived as an independent character.
3. When there is symbol "-" in the value, everything is ok. The problem only with name.

My code:

Code: Select all

TempParams := TCefDictionaryValueRef.New;
TempParams.SetString('name', '__Secure-3PAPISID'); // сookies are not set if the name contains "-"
TempParams.SetString('value', 'some/value-87'); // сookies are even set with a "-" in the value
TempParams.SetString('domain', '.google.com');
TempChromium.ExecuteDevToolsMethod(1, 'Network.setCookie', TempParams);
How can this problem be solved? Any advice or fixes please.
User avatar
salvadordf
Posts: 4016
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Cookies are not set in ExecuteDevToolsMethod

Post by salvadordf »

Hi,

TChromiumCore.ExecuteDevToolsMethod is an easier to use version of the TChromiumCore.SendDevToolsMessage function.

ExecuteDevToolsMethod converts the parameters and method names to a UTF8-encoded JSON string in the "message_" parameter used by SendDevToolsMessage.

These are the code comments for SendDevToolsMessage :

Code: Select all

  ///
  // Send a function call message over the DevTools protocol. |message| must be
  // a UTF8-encoded JSON dictionary that contains "id" (int), "function"
  // (string) and "params" (dictionary, optional) values. See the DevTools
  // protocol documentation at https://chromedevtools.github.io/devtools-
  // protocol/ for details of supported functions and the expected "params"
  // dictionary contents. |message| will be copied if necessary. This function
  // will return true (1) if called on the UI thread and the message was
  // successfully submitted for validation, otherwise false (0). Validation will
  // be applied asynchronously and any messages that fail due to formatting
  // errors or missing parameters may be discarded without notification. Prefer
  // ExecuteDevToolsMethod if a more structured approach to message formatting
  // is desired.
  //
  // Every valid function call will result in an asynchronous function result or
  // error message that references the sent message "id". Event messages are
  // received while notifications are enabled (for example, between function
  // calls for "Page.enable" and "Page.disable"). All received messages will be
  // delivered to the observer(s) registered with AddDevToolsMessageObserver.
  // See cef_dev_tools_message_observer_t::OnDevToolsMessage documentation for
  // details of received message contents.
  //
  // Usage of the SendDevToolsMessage, ExecuteDevToolsMethod and
  // AddDevToolsMessageObserver functions does not require an active DevTools
  // front-end or remote-debugging session. Other active DevTools sessions will
  // continue to function independently. However, any modification of global
  // browser state by one session may not be reflected in the UI of other
  // sessions.
  //
  // Communication with the DevTools front-end (when displayed) can be logged
  // for development purposes by passing the `--devtools-protocol-log-
  // file=<path>` command-line flag.
  ///
These are the code comments for ExecuteDevToolsMethod :

Code: Select all

  ///
  // Execute a function call over the DevTools protocol. This is a more
  // structured version of SendDevToolsMessage. |message_id| is an incremental
  // number that uniquely identifies the message (pass 0 to have the next number
  // assigned automatically based on previous values). |function| is the
  // function name. |params| are the function parameters, which may be NULL. See
  // the DevTools protocol documentation (linked above) for details of supported
  // functions and the expected |params| dictionary contents. This function will
  // return the assigned message ID if called on the UI thread and the message
  // was successfully submitted for validation, otherwise 0. See the
  // SendDevToolsMessage documentation for additional usage information.
  ///
Perhaps there's an issue encoding the parameters in CEF. Try using SendDevToolsMessage.
Chinyaev
Posts: 38
Joined: Mon Mar 13, 2017 3:23 pm

Re: Cookies are not set in ExecuteDevToolsMethod

Post by Chinyaev »

Thanks, but unfortunately it doesn't work too:

Code: Select all

TempChromium.SendDevToolsMessage('{"id": 1, "method":"Network.setCookie","params":{"name": "__Secure-3PAPISID", "value": "d8SPPBN", "domain": ".google.com"}}');
The normal "\-" escape does not work, works "\\-", but name becomes invalid.
It's strange. A symbol "-" does not need to be escaped in JSON.
I use cef85, can you check the same on the latest version? Although I think the problem is not solved.
User avatar
salvadordf
Posts: 4016
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Cookies are not set in ExecuteDevToolsMethod

Post by salvadordf »

The DevTools method has an issue with those names apparently but the TChromium.SetCookie procedure has no problems with them.
Try the CookieVisitor demo.
Post Reply