# Path: vise/communicate.py
# def python_to_js(page_or_tab, name, *args):
#     page = page_or_tab.page() if isinstance(page_or_tab, QWebEngineView) else page_or_tab
#     page.runJavaScript('window.send_message_to_javascript(%s, %s)' % (json.dumps(name), json.dumps(args)), QWebEngineScript.ScriptWorldId.ApplicationWorld)


from functools import partial
from gettext import gettext as _
from PyQt6.QtCore import QEvent, Qt, QUrl
from PyQt6.QtGui import QKeyEvent, QClipboard
from PyQt6.QtWidgets import QApplication
from PyQt6.QtWebEngineCore import QWebEnginePage
from .communicate import python_to_js
    from .downloads import DOWNLOADS_URL
def _paste_url(selection_start, selection_end, source_frame_id, node_id):
    window = QApplication.instance().activeWindow()

    def do_paste(text):
        text = text or ''
        text = text.partition(' ')[2].strip()
        t = getattr(window, 'current_tab', None)
        if t is not None:
            python_to_js(t, 'insert_text_in_saved_node', text, selection_start,
                         selection_end, source_frame_id, node_id)

    window.ask('copyurl ', do_paste)


def paste_url(window, *a, **k):
    if window.current_tab is not None:
        window.current_tab.callback_on_save_edit_text_node = _paste_url
        python_to_js(window.current_tab, 'get_active_text_input_node')
        return True


def paste_selection(window, *a, **k):
    if window.current_tab is not None:
        text = QApplication.clipboard().text(QClipboard.Mode.Selection)
        if text:
            window.current_tab.send_text_using_keys(text)


def fill_login_form(window, *args, **kwargs):
    if window.current_tab is not None:

# Path: vise/communicate.py
# def python_to_js(page_or_tab, name, *args):
#     page = page_or_tab.page() if isinstance(page_or_tab, QWebEngineView) else page_or_tab
#     page.runJavaScript('window.send_message_to_javascript(%s, %s)' % (json.dumps(name), json.dumps(args)), QWebEngineScript.ScriptWorldId.ApplicationWorld)


# Path: vise/actions.py
from functools import partial
from gettext import gettext as _
from PyQt6.QtCore import QEvent, Qt, QUrl
from PyQt6.QtGui import QKeyEvent, QClipboard
from PyQt6.QtWidgets import QApplication
from PyQt6.QtWebEngineCore import QWebEnginePage
from .communicate import python_to_js
    from .downloads import DOWNLOADS_URL

def _paste_url(selection_start, selection_end, source_frame_id, node_id):
    window = QApplication.instance().activeWindow()

    def do_paste(text):
        text = text or ''
        text = text.partition(' ')[2].strip()
        t = getattr(window, 'current_tab', None)
        if t is not None:
            python_to_js(t, 'insert_text_in_saved_node', text, selection_start,
                         selection_end, source_frame_id, node_id)

    window.ask('copyurl ', do_paste)


def paste_url(window, *a, **k):
    if window.current_tab is not None:
        window.current_tab.callback_on_save_edit_text_node = _paste_url
        python_to_js(window.current_tab, 'get_active_text_input_node')
        return True


def paste_selection(window, *a, **k):
    if window.current_tab is not None:
        text = QApplication.clipboard().text(QClipboard.Mode.Selection)
        if text:
            window.current_tab.send_text_using_keys(text)


def fill_login_form(window, *args, **kwargs):
    if window.current_tab is not None:

        python_to_js(window.current_tab, 'get_url_for_current_login_form')