|
12 | 12 | pass |
13 | 13 |
|
14 | 14 |
|
15 | | -async def read() -> Optional[str]: |
16 | | - """Read text from the clipboard. |
| 15 | +class Clipboard: |
| 16 | + """Wrapper for clipboard helper functions.""" |
17 | 17 |
|
18 | | - Note: This function only works in secure contexts (HTTPS or localhost). |
19 | | - """ |
20 | | - result = await run_javascript(''' |
21 | | - if (navigator.clipboard) { |
22 | | - return navigator.clipboard.readText() |
23 | | - } |
24 | | - else { |
25 | | - console.error('Clipboard API is only available in secure contexts (HTTPS or localhost).') |
26 | | - } |
27 | | - ''') |
28 | | - if result is None: |
29 | | - log.warning('Clipboard API is only available in secure contexts (HTTPS or localhost).') |
30 | | - return result |
| 18 | + async def read(self) -> Optional[str]: |
| 19 | + """Read text from the clipboard. |
31 | 20 |
|
| 21 | + Note: This function only works in secure contexts (HTTPS or localhost). |
| 22 | + """ |
| 23 | + result = await run_javascript(''' |
| 24 | + if (navigator.clipboard) { |
| 25 | + return navigator.clipboard.readText() |
| 26 | + } |
| 27 | + else { |
| 28 | + console.error('Clipboard API is only available in secure contexts (HTTPS or localhost).') |
| 29 | + } |
| 30 | + ''') |
| 31 | + if result is None: |
| 32 | + log.warning('Clipboard API is only available in secure contexts (HTTPS or localhost).') |
| 33 | + return result |
32 | 34 |
|
33 | | -def write(text: str) -> None: |
34 | | - """Write text to the clipboard. |
35 | | -
|
36 | | - Note: This function only works in secure contexts (HTTPS or localhost). |
| 35 | + def write(self, text: str) -> None: |
| 36 | + """Write text to the clipboard. |
37 | 37 |
|
38 | | - :param text: text to write |
39 | | - """ |
40 | | - run_javascript(f''' |
41 | | - if (navigator.clipboard) {{ |
42 | | - navigator.clipboard.writeText({json.dumps(text)}) |
43 | | - }} |
44 | | - else {{ |
45 | | - console.error('Clipboard API is only available in secure contexts (HTTPS or localhost).') |
46 | | - }} |
47 | | - ''') |
| 38 | + Note: This function only works in secure contexts (HTTPS or localhost). |
48 | 39 |
|
| 40 | + :param text: text to write |
| 41 | + """ |
| 42 | + run_javascript(f''' |
| 43 | + if (navigator.clipboard) {{ |
| 44 | + navigator.clipboard.writeText({json.dumps(text)}) |
| 45 | + }} |
| 46 | + else {{ |
| 47 | + console.error('Clipboard API is only available in secure contexts (HTTPS or localhost).') |
| 48 | + }} |
| 49 | + ''') |
49 | 50 |
|
50 | | -async def read_image() -> Union['PIL_Image.Image', None]: |
51 | | - """Read PIL images from the clipboard. |
| 51 | + async def read_image(self) -> Union['PIL_Image.Image', None]: |
| 52 | + """Read PIL images from the clipboard. |
52 | 53 |
|
53 | | - Note: This function only works in secure contexts (HTTPS or localhost) and requires Pillow to be installed. |
| 54 | + Note: This function only works in secure contexts (HTTPS or localhost) and requires Pillow to be installed. |
54 | 55 |
|
55 | | - *Added in version 2.10.0* |
56 | | - """ |
57 | | - if not optional_features.has('pillow'): |
58 | | - log.warning('Pillow is not installed, so we cannot read images from the clipboard.') |
59 | | - return None |
60 | | - content = await run_javascript(''' |
61 | | - if (navigator.clipboard) { |
62 | | - const items = await navigator.clipboard.read(); |
63 | | - for (const item of items) { |
64 | | - if (item.types.length > 0 && /^image/.test(item.types[0])) { |
65 | | - return await item.getType(item.types[0]); |
| 56 | + *Added in version 2.10.0* |
| 57 | + """ |
| 58 | + if not optional_features.has('pillow'): |
| 59 | + log.warning('Pillow is not installed, so we cannot read images from the clipboard.') |
| 60 | + return None |
| 61 | + content = await run_javascript(''' |
| 62 | + if (navigator.clipboard) { |
| 63 | + const items = await navigator.clipboard.read(); |
| 64 | + for (const item of items) { |
| 65 | + if (item.types.length > 0 && /^image/.test(item.types[0])) { |
| 66 | + return await item.getType(item.types[0]); |
| 67 | + } |
66 | 68 | } |
67 | 69 | } |
68 | | - } |
69 | | - else { |
70 | | - console.error('Clipboard API is only available in secure contexts (HTTPS or localhost).'); |
71 | | - } |
72 | | - ''', timeout=5) |
73 | | - if not content: |
74 | | - return None |
75 | | - buffer = io.BytesIO(content) |
76 | | - return PIL_Image.open(buffer) |
| 70 | + else { |
| 71 | + console.error('Clipboard API is only available in secure contexts (HTTPS or localhost).'); |
| 72 | + } |
| 73 | + ''', timeout=5) |
| 74 | + if not content: |
| 75 | + return None |
| 76 | + buffer = io.BytesIO(content) |
| 77 | + return PIL_Image.open(buffer) |
| 78 | + |
| 79 | + |
| 80 | +clipboard = Clipboard() |
0 commit comments