Skip to content

Commit 32560b1

Browse files
committed
Add cookies.py snippet and README-snippets.md
1 parent 4d4fd50 commit 32560b1

File tree

5 files changed

+133
-40
lines changed

5 files changed

+133
-40
lines changed

README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,16 @@ pip install cefpython3==66.0
133133

134134
## Tutorial
135135

136-
See the [Tutorial.md](docs/Tutorial.md) file.
136+
See the [Tutorial.md](docs/Tutorial.md) document.
137137

138138

139139
## Examples
140140

141-
See the [README-examples.md](examples/README-examples.md) file.
141+
See the [README-examples.md](examples/README-examples.md) document.
142+
143+
For small and easy to understand code snippets that show various CEF
144+
features see the [README-snippets.md](examples/snippets/README-snippets.md)
145+
document.
142146

143147

144148
## Support
@@ -299,8 +303,8 @@ Additional information for v31.2 release:
299303
- [Tutorial](docs/Tutorial.md)
300304

301305

302-
### API categories
303-
306+
### API categories
307+
304308
#### Modules
305309

306310
* [cefpython](api/cefpython.md#cefpython) module
@@ -356,9 +360,9 @@ Additional information for v31.2 release:
356360
* [StringVisitor](api/StringVisitor.md#stringvisitor-interface) interface
357361
* [WebRequestClient](api/WebRequestClient.md#webrequestclient-interface) interface
358362

359-
360-
### API index
361-
363+
364+
### API index
365+
362366
* [AccessibilityHandler (interface)](api/AccessibilityHandler.md#accessibilityhandler-interface)
363367
* [_OnAccessibilityTreeChange](api/AccessibilityHandler.md#_onaccessibilitytreechange)
364368
* [_OnAccessibilityLocationChange](api/AccessibilityHandler.md#_onaccessibilitylocationchange)

examples/README-examples.md

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,27 +45,9 @@ workarounds.
4545

4646
### Snippets
4747

48-
See small code snippets that show various CEF features in the
49-
[examples/snippets/](snippets/) directory:
50-
51-
- [javascript_bindings.py](snippets/javascript_bindings.py) - Communicate
52-
between Python and Javascript asynchronously using
53-
inter-process messaging with the use of Javascript Bindings.
54-
- [javascript_errors.py](snippets/javascript_errors.py) - Two ways for
55-
intercepting Javascript errors.
56-
- [mouse_clicks.py](snippets/mouse_clicks.py) - Perform mouse clicks
57-
and mouse movements programmatically.
58-
- [network_cookies.py](snippets/network_cookies.py) - Implement
59-
interfaces to block or allow cookies over network requests.
60-
- [onbeforeclose.py](snippets/onbeforeclose.py) - Implement interface
61-
to execute custom code before browser window closes.
62-
- [ondomready.py](snippets/ondomready.py) - Execute custom Python code
63-
on a web page as soon as DOM is ready.
64-
- [onpagecomplete.py](snippets/onpagecomplete.py) - Execute custom
65-
Python code on a web page when page loading is complete.
66-
- [setcookie.py](snippets/setcookie.py) - Shows how to set a cookie
67-
- [window_size.py](snippets/window_size.py) - Set initial window size
68-
without use of any third party GUI framework.
48+
For small code snippets that show various CEF features and are easy to
49+
understand see the [README-snippets.md](snippets/README-snippets.md)
50+
document.
6951

7052

7153
### GUI frameworks
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Snippets README
2+
3+
Table of contents:
4+
* [Hello World!](#hello-world)
5+
* [Snippets](#snippets)
6+
7+
8+
## Hello World!
9+
10+
Instructions to install the cefpython3 package, clone
11+
the repository and run the `javascript_bindings.py` snippet:
12+
13+
```
14+
pip install cefpython3==66.0
15+
git clone https://github.com/cztomczak/cefpython.git
16+
cd cefpython/examples/snippets/
17+
python javascript_bindings.py
18+
```
19+
20+
21+
## Snippets
22+
23+
Below are small code snippets that show various CEF features and
24+
are easy to understand. These are available in the [examples/snippets/](./)
25+
directory. If looking for non-trivial examples then see the
26+
[README-examples.md](../README-examples.md) document.
27+
28+
29+
- [cookies.py](snippets/cookies.py) - Shows how to fetch all cookies,
30+
all cookies for a given url and how to delete a specific cookie.
31+
- [javascript_bindings.py](snippets/javascript_bindings.py) - Communicate
32+
between Python and Javascript asynchronously using
33+
inter-process messaging with the use of Javascript Bindings.
34+
- [javascript_errors.py](snippets/javascript_errors.py) - Two ways for
35+
intercepting Javascript errors.
36+
- [mouse_clicks.py](snippets/mouse_clicks.py) - Perform mouse clicks
37+
and mouse movements programmatically.
38+
- [network_cookies.py](snippets/network_cookies.py) - Implement
39+
interfaces to block or allow cookies over network requests.
40+
- [onbeforeclose.py](snippets/onbeforeclose.py) - Implement interface
41+
to execute custom code before browser window closes.
42+
- [ondomready.py](snippets/ondomready.py) - Execute custom Python code
43+
on a web page as soon as DOM is ready.
44+
- [onpagecomplete.py](snippets/onpagecomplete.py) - Execute custom
45+
Python code on a web page when page loading is complete.
46+
- [setcookie.py](snippets/setcookie.py) - Shows how to set a cookie
47+
- [window_size.py](snippets/window_size.py) - Set initial window size
48+
without use of any third party GUI framework.

examples/snippets/cookies.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""Shows how to fetch all cookies, all cookies for
2+
a given url and how to delete a specific cookie. For
3+
an example on how to set a cookie see the 'setcookie.py'
4+
snippet."""
5+
6+
from cefpython3 import cefpython as cef
7+
8+
9+
def main():
10+
cef.Initialize()
11+
browser = cef.CreateBrowserSync(
12+
url="http://www.html-kit.com/tools/cookietester/",
13+
window_title="Cookies")
14+
browser.SetClientHandler(LoadHandler())
15+
cef.MessageLoop()
16+
del browser
17+
cef.Shutdown()
18+
19+
20+
class LoadHandler(object):
21+
def OnLoadingStateChange(self, browser, is_loading, **_):
22+
if is_loading:
23+
print("Page loading complete - start visiting cookies")
24+
manager = cef.CookieManager.GetGlobalManager()
25+
# Must keep a strong reference to the CookieVisitor object
26+
# while cookies are being visited.
27+
self.cookie_visitor = CookieVisitor()
28+
# Visit all cookies
29+
result = manager.VisitAllCookies(self.cookie_visitor)
30+
if not result:
31+
print("Error: could not access cookies")
32+
# To visit cookies only for a given url uncomment the
33+
# code below.
34+
"""
35+
url = "http://www.html-kit.com/tools/cookietester/"
36+
http_only_cookies = False
37+
result = manager.VisitUrlCookies(url, http_only_cookies,
38+
self.cookie_visitor)
39+
if not result:
40+
print("Error: could not access cookies")
41+
"""
42+
43+
44+
class CookieVisitor(object):
45+
def Visit(self, cookie, count, total, delete_cookie_out):
46+
"""This callback is called on the IO thread."""
47+
print("Cookie {count}/{total}: '{name}', '{value}'"
48+
.format(count=count+1, total=total, name=cookie.GetName(),
49+
value=cookie.GetValue()))
50+
# Set a cookie named "delete_me" and it will be deleted.
51+
# You have to refresh page to see whether it succeeded.
52+
if cookie.GetName() == "delete_me":
53+
# 'delete_cookie_out' arg is a list passed by reference.
54+
# Set its '0' index to True to delete the cookie.
55+
delete_cookie_out[0] = True
56+
print("Deleted cookie: {name}".format(name=cookie.GetName()))
57+
# Return True to continue visiting more cookies
58+
return True
59+
60+
61+
if __name__ == '__main__':
62+
main()

tools/apidocs.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,15 @@
2222

2323
def main():
2424
"""Main entry point."""
25-
# Call toc.py for docs/ and api/ directories
26-
print("Running toc.py in {}/ dir".format(os.path.basename(API_DIR)))
27-
retcode = subprocess.call([sys.executable,
28-
os.path.join(TOOLS_DIR, "toc.py"),
29-
API_DIR])
30-
assert retcode == 0, "Executing toc.py for API_DIR failed"
31-
32-
print("Running toc.py in {}/ dir".format(os.path.basename(DOCS_DIR)))
33-
retcode = subprocess.call([sys.executable,
34-
os.path.join(TOOLS_DIR, "toc.py"),
35-
DOCS_DIR])
36-
assert retcode == 0, "Executing toc.py for DOCS_DIR failed"
25+
# Call toc.py for docs/, api/, examples/ and examples/snippets/
26+
# directories.
27+
toc_dirs = [API_DIR, DOCS_DIR, EXAMPLES_DIR, SNIPPETS_DIR]
28+
for toc_dir in toc_dirs:
29+
print("Running toc.py in {}/ dir".format(os.path.basename(toc_dir)))
30+
retcode = subprocess.call([sys.executable,
31+
os.path.join(TOOLS_DIR, "toc.py"),
32+
toc_dir])
33+
assert retcode == 0, "Executing toc.py failed"
3734

3835
# Generate API reference in api/ dir and in root/README.md
3936
api_links = generate_api_links()

0 commit comments

Comments
 (0)