rkd77 / elinks

Fork of elinks
Other
349 stars 38 forks source link

Keyboard sequence with Lua script. #220

Closed ZaxonXP closed 7 months ago

ZaxonXP commented 1 year ago

I use elinks on some site which does not keep the page position when I reload it (it scroll to top). My workaround is to set a mark, reload the page and jump to mark. I wonder if this can be also done by Lua script and assign to some keyboard shortcut?

Kind regards, Zaxon

rkd77 commented 1 year ago

I did not check it, but I guess you do not need lua for this. See macros #196.

ZaxonXP commented 1 year ago

I tried to get the latest code using git clone command and I defined the keyboard bind like this:

bind "main" "Ctrl-R" = "macro-0" "reload" "macro-1"

or like this

bind "main" "Ctrl-R" = "macro-0"; "reload"; "macro-1"

But elinks complains that this is not correct command. Did I get the correct version or should I use another command to get some specific branch with this feature which is described in #196 ?

ZaxonXP commented 1 year ago

Also I do not know what the macro can contain. I put in the macros following:

  ## macro.0 <str> - this should make mark a
  set macro.0 = "ma"

  ## macro.1 <str> - this should go to the mark a
  set macro.1 = "\'a"

Is this how it should be used? I could not find much of elinks macro exmples.

rkd77 commented 1 year ago

I tested it. It does not work yet. I thought about something like: set macro.0 = 'mark-set a; reload; mark-goto a'

I tried also to define konsole's macros, but also without success.

rkd77 commented 1 year ago

Not lua, but python:

import fcntl
import termios

def pre_format_html_hook(url, html):
    with open('/dev/stdin', 'w') as fd:
        for char in "Ma":
           fcntl.ioctl(fd, termios.TIOCSTI, char)
    # normal code of preformat hook follow

def try_to_reload():
    """Try to reload."""
    with open('/dev/stdin', 'w') as fd:
        for char in "maR":
           fcntl.ioctl(fd, termios.TIOCSTI, char)

elinks.bind_key("F8", try_to_reload)

F8 is bound to this function. These are not standard: m is bound to mark-set a is key R is bound to reload M is bound to mark-goto

Normally if open stdin to write, it writes to stdout, so this trick with fcntl. With curses you can even read from keyboard, so you do not have to hardcode 'a' here. pre_format_hook is similar as javascript's onload. Scrolled to top without.

ZaxonXP commented 1 year ago

I managed to enable Perl support, but I have a problem to compile it with the Python support (I use --with-python, but this does not find it). What should I have/do?

rkd77 commented 1 year ago

Are libpython3-dev and python3-dev installed? Any errors in config.log ?

ZaxonXP commented 1 year ago

Looks like it was my bad. I was changing the my configure script in another directory and compiling in the other.

I put the code you posted in hooks.py in the elinks config folder and restarted elinks. I scrolled bit down and pressed F8, but nothing happen. Should I use compete hooks.py from contrib folder and add your code? Or just your code should be enough?

ZaxonXP commented 1 year ago

I noticed that the first pre_format_html_hook code should be added to the current method definition. I added it to standard hooks.py. However still do not work.

rkd77 commented 1 year ago

I have such keybindings in elinks.conf:

##################################
# Automatically saved keybindings
#

bind "main" "m" = "mark-set"
bind "main" "M" = "mark-goto"
bind "main" "R" = "reload"

Do you have similar? Could you show beginning of hooks.py and modified code?

ZaxonXP commented 1 year ago

I have the same mappings to match your code. Here is also the hooks.py file. hooks.py.txt

rkd77 commented 1 year ago

Did page reload after CTRL-R or not?

ZaxonXP commented 1 year ago

I tried with your settings for F8 and it did not work. I changed to Ctrl-r and it also does not (page is not reloading). However when using Shift-r it does (so the binding works). Did it worked for you?

ZaxonXP commented 1 year ago

Should I send you also my config?

rkd77 commented 1 year ago

I noticed you have backtick at the end of line with elinks.bind('Ctrl-r') There was bug in code I pasted. Remove it. And try again.

rkd77 commented 1 year ago

Remove backtick, not code.

ZaxonXP commented 1 year ago

looks like I do not have the elinks Python library installed and I have some issues installing it. I discovered it when I wanted to run hooks.py with Python.

rkd77 commented 1 year ago

F9 -> Help -> About Is there Python? If not, install python3-distutils and recompile

ZaxonXP commented 1 year ago

Well, stupid me. I recompiled the elinks with Python support, checked locally and forgot to install it. :\ After installation it works.

Thanks a lot for patience and your help!

ZaxonXP commented 1 year ago

The solution is working when there is only one elinks instance. However I open more than one in a separate tmux windows. In such case when I do reload, then it reloads on the first instance and not in the current. Is there a solution for that?

rkd77 commented 1 year ago

I have no idea how to fix it. You can use tabs (keys t,<,>,c) instead of new elinks instances. Or start new elinkses with --no-connect.

ZaxonXP commented 1 year ago

OK. I managed to use tabs. So now everything works. Thanks for your help.

ZaxonXP commented 7 months ago

@rkd77:

Recently I am starting getting following errors when executing the Python code: 2024-02-26-172127_345x454_scrot The Pyhon code is following:

import elinks
import fcntl
import termios
import os

def pre_format_html_hook(url, html):
    with open('/dev/stdin', 'w') as fd:
        for char in "Ma":
           fcntl.ioctl(fd, termios.TIOCSTI, char)

def try_to_reload():
    """Try to reload."""
    with open('/dev/stdin', 'w') as fd:
        for char in "maR":
           fcntl.ioctl(fd, termios.TIOCSTI, char)

def copy_url():
    """ Copy current url """
    os.system('clear')
    print(elinks.current_url())

elinks.bind_key("F7", copy_url)
elinks.bind_key("Ctrl-r", try_to_reload)

What could be the reason for this?

rkd77 commented 7 months ago

What changed in system since April 2023? I read somewhere that TIOCSTI requires root or at least CAP_SYS_ADMIN capability.

ZaxonXP commented 7 months ago

Is there some workaround for this?

rkd77 commented 7 months ago

Is it an Android device or plain Linux?

ZaxonXP commented 7 months ago

Plain Linux. Debian 12.

rkd77 commented 7 months ago

https://stackoverflow.com/questions/75675261/alternatives-to-tiocsti-now-that-is-disable-on-default-for-kernels-6-2 dev.tty.legacy_tiocsti=1 in /etc/sysctl.conf You can try first as root: echo 1 > /proc/sys/dev/tty/legacy_tiocsti

ZaxonXP commented 7 months ago

When I do this:

echo 1 > /proc/sys/dev/tty/legacy_tiocsti

ten afterwards reloading page in Elinks does not display the error anymore. But what are the drawbacks of using it?

rkd77 commented 7 months ago

Does it work? Not only not display error, but also scrolls as before? I guess that paranoid security was reason of changes related to TIOCSTI, but with this legacy_tiocsti you can restore pre 6.2 kernel behaviour which worked for years. Some programs, for example viruses, can modify your input on some devices. It is a guess. But it you have viruses, they find out other ways to spread.

ZaxonXP commented 7 months ago

Yes, it reloads the page and scroll to the previous position. So it acts as before. Now I recall that this issue started to appear after I updated kernel to version 6.5. Before I had no such issue. Thanks for your help.