From a089b1f209405240da05e8c93c03cd5225857989 Mon Sep 17 00:00:00 2001 From: Carl Furtado Date: Thu, 20 Aug 2026 10:35:35 -0400 Subject: [PATCH] ensure tabs always feign focus --- src/rewards_tasks.py | 4 ++++ src/tab_utils.py | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/rewards_tasks.py b/src/rewards_tasks.py index 62f1765..c2e7333 100644 --- a/src/rewards_tasks.py +++ b/src/rewards_tasks.py @@ -21,6 +21,8 @@ class RewardsTaskUtils: self.driver.get("https://rewards.bing.com/") self.tab_utils = tab_utils.TabUtils(driver) + self.tab_utils.ensure_focus() + self.mouse = mouse_trajectory.MouseUtils(driver) self.keyboard = mimic_typing.KeyboardUtils(driver) self.elements = element_selectors.ElementSelectionUtils(driver) @@ -150,6 +152,7 @@ class RewardsTaskUtils: searches_needed = (max_pts - points_earned) // 5 self.driver.get("https://www.bing.com/") + self.tab_utils.ensure_focus() self.wait_for_element(self.elements.get_bing_search_bar) @@ -167,6 +170,7 @@ class RewardsTaskUtils: self.move_to_and_click(self.elements.get_clear_bing_search_query_button()) self.driver.get("https://rewards.bing.com/") + self.tab_utils.ensure_focus() self.switch_to_earn_page() diff --git a/src/tab_utils.py b/src/tab_utils.py index 6b7364d..84b0611 100644 --- a/src/tab_utils.py +++ b/src/tab_utils.py @@ -8,6 +8,15 @@ class TabUtils: self.driver = driver self.problematic_tabs = set() + def ensure_focus(self): + self.driver.execute_script(""" +Object.defineProperty(document, 'hidden', { get: () => false }); +Object.defineProperty(document, 'visibilityState', { get: () => 'visible' }); +Document.prototype.hasFocus = function() { return true; }; +window.hasFocus = function() { return true; }; +document.dispatchEvent(new Event('visibilitychange')); +""") + def switch_to_other_tab(self): current_window = self.driver.current_window_handle @@ -16,6 +25,8 @@ class TabUtils: self.driver.switch_to.window(handle) if self.driver.current_url == GHOST_TAB_URL: continue + + self.ensure_focus() return def close_all_other_tabs(self, exceptions: list[str] = None):