| 48 | |
| 49 | |
| 50 | class TextObervationProcessor(ObservationProcessor): |
| 51 | def __init__( |
| 52 | self, |
| 53 | observation_type: str, |
| 54 | current_viewport_only: bool, |
| 55 | viewport_size: ViewportSize, |
| 56 | ): |
| 57 | self.observation_type = observation_type |
| 58 | self.current_viewport_only = current_viewport_only |
| 59 | self.viewport_size = viewport_size |
| 60 | self.observation_tag = "text" |
| 61 | self.meta_data = ( |
| 62 | create_empty_metadata() |
| 63 | ) # use the store meta data of this observation type |
| 64 | |
| 65 | def fetch_browser_info( |
| 66 | self, |
| 67 | page: Page, |
| 68 | client: CDPSession, |
| 69 | ) -> BrowserInfo: |
| 70 | # extract domtree |
| 71 | tree = client.send( |
| 72 | "DOMSnapshot.captureSnapshot", |
| 73 | { |
| 74 | "computedStyles": [], |
| 75 | "includeDOMRects": True, |
| 76 | "includePaintOrder": True, |
| 77 | }, |
| 78 | ) |
| 79 | |
| 80 | # calibrate the bounds, in some cases, the bounds are scaled somehow |
| 81 | bounds = tree["documents"][0]["layout"]["bounds"] |
| 82 | b = bounds[0] |
| 83 | n = b[2] / self.viewport_size["width"] |
| 84 | bounds = [[x / n for x in bound] for bound in bounds] |
| 85 | tree["documents"][0]["layout"]["bounds"] = bounds |
| 86 | |
| 87 | # extract browser info |
| 88 | win_top_bound = page.evaluate("window.pageYOffset") |
| 89 | win_left_bound = page.evaluate("window.pageXOffset") |
| 90 | win_width = page.evaluate("window.screen.width") |
| 91 | win_height = page.evaluate("window.screen.height") |
| 92 | win_right_bound = win_left_bound + win_width |
| 93 | win_lower_bound = win_top_bound + win_height |
| 94 | device_pixel_ratio = page.evaluate("window.devicePixelRatio") |
| 95 | assert device_pixel_ratio == 1.0, "devicePixelRatio is not 1.0" |
| 96 | |
| 97 | config: BrowserConfig = { |
| 98 | "win_top_bound": win_top_bound, |
| 99 | "win_left_bound": win_left_bound, |
| 100 | "win_width": win_width, |
| 101 | "win_height": win_height, |
| 102 | "win_right_bound": win_right_bound, |
| 103 | "win_lower_bound": win_lower_bound, |
| 104 | "device_pixel_ratio": device_pixel_ratio, |
| 105 | } |
| 106 | |
| 107 | # assert len(tree['documents']) == 1, "More than one document in the DOM tree" |