Automatic web-page refresh using xdotool - not sending key after window focus

旧城冷巷雨未停 提交于 2020-01-22 14:52:38

问题


Given:

I have Firefox with the [Firefox Page Title] page open on my Ubuntu computer.

Here is my command:

xdotool search "[Firefox Page Title]" windowactivate --sync key --clearmodifiers ctrl+r

Documentation:

xdotool website with documentation/examples is here.

Example straight from the xdotool website:

# As of version 2.20100623, you can do this simpler version of above:
xdotool search "Mozilla Firefox" windowactivate --sync key --clearmodifiers ctrl+l

Notes:

I'm using xdotool version 2.20110530.1.

The command correctly focuses my screen to whatever window title I choose, but it doesn't send the ctrl+r key to the window, as the website doesn't refresh. I receive no error messages from the command. (Refresh shortcut in Firefox is ctrl+r)

Wanted Behavior:

The command will hopefully (when it works) be applied to a web server statistics page that is open 24/7 on my server computer, I'd like it to refresh the page automatically so I can view new traffic to my website without me having to do anything.

EDIT: I'm open to using other applications that can provide this functionality, if you know of something else that's easy/similar, please provide it as an answer! If I can't get this working I'll default to the next best thing.


回答1:


I was trying to send keystrokes to an application and I also concluded that xdotool just doesn't work as described. I ended up using xvkbd to do the same thing.

For your example the following command refreshes a page in Firefox:

xvkbd -window Firefox -text "\Cr"



回答2:


After trying several different approaches to get xdotool to work correctly, I'm inclined to believe that xdotool itself is the issue. Here is what I tried, none worked.

  • Running the command (and variations - removing/adding args) from Terminal.
  • Running the command (and variations - removing/adding args) from a SH script.
  • Changing between F5 and ctrl+r keys, as they should both refresh a Firefox page.
  • Trying other parameters, such as:
    • --window to set the window the keys are to be sent to.
    • --delay to add a delay before the key is sent, after the window is focused.
    • Adding a sleep before the key is sent, after the window is focused.

I also tried these commands in a script, as the frontpage for xdotool recommends, although it states this is the "older" version, as it is separated into multiple commands. The "new" version was the version I was trying to execute before and is a single command (see question).

WID=`xdotool search "Firefox Page Title"`
xdotool windowactivate --sync $WID
xdotool key --clearmodifiers ctrl+r

All of the above attempts ALWAYS correctly focused to the window I wanted, but it does not send the key whether it was F5 or ctrl+r.

However, the following worked correctly:

xdotool selectwindow key ctrl+r

OR

xdotool selectwindow key F5

The selectwindow command, when executed, turns your cursor into a rectangular selection tool at which point you can select the window you want to be focused and, in this case, what window to send either the ctrl+r or F5 key to. Unfortunately, this is not what I was looking for, as it requires user input to work correctly.

Final Solution:

My solution (since I was attempting to use xdotool to constantly refresh a web-page) was to use the ReloadEvery Firefox add-on, which refreshes any page you set it on in any time interval you choose. It is intended to be a replica of the Opera browser's built-in automatic page refresh feature, and thus far, it works well.

For those of you who use Chrome and are looking for a similar solution, there are plenty of add-ons available for you too. https://chrome.google.com/webstore/search/auto%20refresh




回答3:


xdotool search --onlyvisible --classname Navigator windowactivate --sync key F5

runing arch linux




回答4:


I have key bind win + shift + s to get the window id using

xdotool getactivewindow getwindowgeometry

and win + s to refresh

xdotool key --window savedWindowID ctrl+r



回答5:


The fact that xdotool doesn't seem to work is probably related to applications detecting and discarding synthesized events:

Sending keystrokes to a specific window uses a different API than simply typing to the active window.

[...]

Many programs observe this flag and reject these events.

With that in mind I was able to make it work with the series of commands below. This reloads both Chromium and Firefox.

cwid=$(xdotool getwindowfocus) # Save the current window
twid=$(xdotool search --name somename)
xdotool windowactivate $twid
sleep 0.1 # The key event might be sent before the window has been fully activated
xdotool key --window $twid F5
xdotool windowactivate $cwid # Done, now go back to where we were



回答6:


For me, the following works:

xdotool search --onlyvisible --class Firefox key F5

as well as

{ 
   xdotool search --onlyvisible --class Firefox windowfocus
   sleep 0.1
   xdotool key ctrl+r
}

but

xdotool search --onlyvisible --class Firefox key ctrl+r

and

xdotool search --onlyvisible --class Firefox windowfocus key ctrl+r

do not work.

So it seems that using a key combo with modifiers requires a little delay, at least with Firefox (I can send ctrl-keys to Emacs with no such issues, however).




回答7:


The following xdotool command works perfectly for me (switches to the first found Firefox window, refreshes current tab):

xdotool search "Navigator" windowactivate key 'ctrl+r'

Running Ubuntu 14.04.1 on xdotool 3.20140217.1



来源:https://stackoverflow.com/questions/12026953/automatic-web-page-refresh-using-xdotool-not-sending-key-after-window-focus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!