X Tutup
Skip to content

Commit 53e7988

Browse files
committed
Add "Change user agent string" section to Tutorial and update tutorial.py
1 parent 79623c8 commit 53e7988

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

docs/Tutorial.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,13 @@ Here are some settings worth noting:
241241
customize context menu
242242
- [locale](../api/ApplicationSettings.md#locale) - set language
243243
for localized resources
244+
- [product_version](../api/ApplicationSettings.md#product_version) -
245+
set the product portion of the default User-Agent string.
246+
If user_agent option (below) is used then product_version will
247+
be ignored.
248+
- [user_agent](../api/ApplicationSettings.md#user_agent) - set
249+
value that will be returned as the User-Agent HTTP header
250+
and js navigator.userAgent
244251

245252
To enable debugging set these settings:
246253
```python
@@ -290,6 +297,45 @@ wasn't yet exposed to CEF Python, see [Issue #244](../../../issues/244)
290297
for details.
291298

292299

300+
## Change user agent string
301+
302+
There are two options in [application settings](../api/ApplicationSettings.md)
303+
for changing User-Agent string: [product_version](../api/ApplicationSettings.md#product_version)
304+
and [user_agent](../api/ApplicationSettings.md#user_agent).
305+
306+
The "product_version" sets the product portion of the default
307+
User-Agent string. If "user_agent" option is used then
308+
"product_version" will be ignored. For example if you set
309+
"product_version" to "MyProduct/10.00" then User-Agent will
310+
be:
311+
312+
```text
313+
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)
314+
MyProduct/10.00 Safari/537.36
315+
```
316+
317+
To change the whole user agent string use the "user_agent"
318+
option. For example set it to "MyApp/40.00 MyProduct/10.00"
319+
and both User-Agent HTTP header and js navigator.userAgent will be:
320+
321+
```text
322+
MyAgent/20.00 MyProduct/10.00
323+
```
324+
325+
Uncomment appropriate lines in [tutorial.py](../examples/tutorial.py)
326+
example to see the effect:
327+
328+
```Python
329+
# To change user agent use either "product_version"
330+
# or "user_agent" options. Explained in Tutorial in
331+
# "Change user agent string" section.
332+
settings = {
333+
# "product_version": "MyProduct/10.00",
334+
# "user_agent": "MyAgent/20.00 MyProduct/10.00",
335+
}
336+
```
337+
338+
293339
## Client handlers
294340

295341
In CEF [client handlers](../api/API-categories.md#client-handlers-interfaces)

examples/tutorial.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,14 @@
6060
def main():
6161
check_versions()
6262
sys.excepthook = cef.ExceptHook # To shutdown all CEF processes on error
63-
cef.Initialize()
63+
# To change user agent use either "product_version"
64+
# or "user_agent" options. Explained in Tutorial in
65+
# "Change user agent string" section.
66+
settings = {
67+
# "product_version": "MyProduct/10.00",
68+
# "user_agent": "MyAgent/20.00 MyProduct/10.00",
69+
}
70+
cef.Initialize(settings=settings)
6471
set_global_handler()
6572
browser = cef.CreateBrowserSync(url=html_to_data_uri(HTML_code),
6673
window_title="Tutorial")

0 commit comments

Comments
 (0)
X Tutup