追加のソフトウェアをインストールせずにFirefoxの現在のタブからURLを取得するには?

追加のソフトウェアをインストールせずにFirefoxの現在のタブからURLを取得するには?

xdotoolを使用して現在のタブをインポートできます。

# set focus to address on browser
xdotool search --onlyvisible --classname Navigator windowactivate --sync key F6

# copy address from browser tab to clipboard
xdotool search --onlyvisible --classname Navigator windowactivate --sync key Ctrl+c

# get off the focus from address from browser tab
xdotool search --onlyvisible --classname Navigator windowactivate --sync key F6

# delivery of clipboard content to variable
clipboard=`xclip -o -selection clipboard`

# clear clipboard
xsel -bc; xsel -c

# echo URL of active tab of active browser
echo $clipboard

xdotoolには時々上記のコードを停止せずにキー押下をトリガーする欠陥があるため、同じことをするにはwmctrlを使用する別の方法が必要です。

これはbashでwmctrlを使って試しました。

id=$(wmctrl -l | grep -oP "(?<=)(0x\w+)(?=.*Firefox)")

# set focus to address on browser
xdotool key --window $id "ctrl+l"

# copy address from browser tab
xdotool key --window $id "ctrl+c"

# delivery of clipboard content to variable
url=`xclip -o -selection clipboard`

# clear clipboard
xsel -bc; xsel -c

# echo URL of active tab of active browser
echo $url

ベストアンサー1

私はこれにxdotoolを使用しません。それは脆弱でエラーが発生しやすいです。次の適切なブラウザ自動化ツールを使用します。 人形。 maronatte_driver Pythonモジュールをインストールします。

pip3 install --user marionette_driver

--marionette オプションを使用して Firefox の新しいインスタンスを起動します。

firefox --marionette

次のget_url.pyを使用してください。

#!/usr/bin/env python3

import marionette_driver

m = marionette_driver.marionette.Marionette()

m.start_session()
print(m.get_url())
m.delete_session()

例:

$ ./get_url.py
https://unix.stackexchange.com/questions/629440/how-to-get-the-url-from-current-tab-of-firefox-by-help-of-wmctrl

./get_url.pyコマンド置換を使用して出力を変数に保存できます。

$ url="$(./get_url.py )"
$ echo $url
https://unix.stackexchange.com/questions/629440/how-to-get-the-url-from-current-tab-of-firefox-by-help-of-wmctrl/629447?noredirect=1#comment1177925_629447

おすすめ記事