diff --git a/core/browser.py b/core/browser.py index 0a003db..aef2b67 100644 --- a/core/browser.py +++ b/core/browser.py @@ -76,6 +76,7 @@ class BrowserView(QWebEngineView): self.select_input_text_js = self.read_js_content("select_input_text.js") self.dark_mode_js = self.read_js_content("dark_mode.js") self.get_selection_text_js = self.read_js_content("get_selection_text.js") + self.focus_input_js = self.read_js_content("focus_input.js") def open_download_manage_page(self): self.open_url_new_buffer("file://" + (os.path.join(os.path.dirname(__file__), "aria2-webui", "index.html"))) @@ -336,6 +337,10 @@ class BrowserView(QWebEngineView): self.set_focus_text_js = self.set_focus_text_raw.replace("%1", string_to_base64(new_text)); self.eval_js(self.set_focus_text_js) + def focus_input(self): + self.execute_js(self.focus_input_js) + # self.eval_js(self.read_js_content("focus_input.js")) + def clear_focus(self): self.eval_js(self.clear_focus_js) @@ -466,7 +471,7 @@ class BrowserBuffer(Buffer): "scroll_left", "scroll_right", "scroll_up", "scroll_down", "scroll_up_page", "scroll_down_page", "scroll_to_begin", "scroll_to_bottom", "refresh_page", "undo_action", "redo_action", "get_url", "exit_fullscreen", - "set_focus_text", "clear_focus", "dark_mode", "view_source"]: + "set_focus_text", "clear_focus", "dark_mode", "view_source", "focus_input"]: self.build_widget_method(method_name) self.build_widget_method("history_backward", "back") @@ -482,7 +487,7 @@ class BrowserBuffer(Buffer): "refresh_page", "zoom_in", "zoom_out", "zoom_reset", "save_as_bookmark", "edit_url", "download_youtube_video", "download_youtube_audio", "toggle_device", "close_buffer", "save_as_pdf", "view_source", "save_as_single_file", "select_left_tab", "select_right_tab", - "copy_code"]: + "copy_code", "focus_input"]: self.build_insert_or_do(method_name) def notify_print_message(self, file_path, success): diff --git a/core/js/focus_input.js b/core/js/focus_input.js new file mode 100644 index 0000000..67c7c45 --- /dev/null +++ b/core/js/focus_input.js @@ -0,0 +1,46 @@ +(function() { + function getVisibleElements(filter) { + var all = Array.from(document.documentElement.getElementsByTagName("*")); + var visibleElements = []; + for (var i = 0; i < all.length; i++) { + var e = all[i]; + // include elements in a shadowRoot. + if (e.shadowRoot) { + var cc = e.shadowRoot.querySelectorAll('*'); + for (var j = 0; j < cc.length; j++) { + all.push(cc[j]); + } + } + var rect = e.getBoundingClientRect(); + if ( (rect.top <= window.innerHeight) && (rect.bottom >= 0) + && (rect.left <= window.innerWidth) && (rect.right >= 0) + && rect.height > 0 + && getComputedStyle(e).visibility !== 'hidden' + ) { + filter(e, visibleElements); + } + } + return visibleElements; + } + var cssSelector = "input"; + + var elements = getVisibleElements(function(e, v) { + if (e.matches(cssSelector) && !e.disabled && !e.readOnly + && (e.type === "text" || e.type === "search" || e.type === "password")) { + v.push(e); + } + }); + + if (elements.length === 0 && document.querySelector(cssSelector) !== null) { + document.querySelector(cssSelector).scrollIntoView(); + elements = getVisibleElements(function(e, v) { + if (e.matches(cssSelector) && !e.disabled && !e.readOnly) { + v.push(e); + } + }); + } + + if (elements.length >= 1) { + elements[0].focus(); + } +})(); diff --git a/eaf.el b/eaf.el index 994444e..8fd527c 100644 --- a/eaf.el +++ b/eaf.el @@ -307,7 +307,7 @@ Try not to modify this alist directly. Use `eaf-setq' to modify instead." ("L" . "insert_or_history_forward") ("t" . "insert_or_new_blank_page") ("T" . "insert_or_recover_prev_close_page") - ("i" . "insert_or_open_download_manage_page") + ("i" . "insert_or_focus_input") ("r" . "insert_or_refresh_page") ("g" . "insert_or_scroll_to_begin") ("x" . "insert_or_close_buffer")