Yun*_*nus 14 scripting bash chrome firefox python
我曾经使用 python 命令在 Firefox 上执行此操作,但最近发生了一些变化,我无法再获取 URL!
过去与 Firefox 一起使用的是什么:
#!/bin/bash
current_tab_num () {
python2 <<< $'import json\nf = open("/home/username/.mozilla/firefox/xxxxx.default/sessionstore.js", "r")\njdata = json.loads(f.read())\nf.close()\nprint str(jdata["windows"][0]["selected"])'
}
current_tab_url () {
sed -n "$(current_tab_num)p" <(python2 <<< $'import json\nf = open("/home/username/.mozilla/firefox/xxxxx.default/sessionstore.js", "r")\njdata = json.loads(f.read())\nf.close()\nfor win in jdata.get("windows"):\n\tfor tab in win.get("tabs"):\n\t\ti = tab.get("index") - 1\n\t\tprint tab.get("entries")[i].get("url")')
}
current_tab_url
Run Code Online (Sandbox Code Playgroud)谁能告诉我如何在 Firefox 和/或 chrome 中做到这一点?
注意:我不懂 python,我只是在某处找到了这些命令并在 bash 中使用了它们!
小智 6
我遇到了同样的问题并在尝试解决它时解决了它,所以我会在这里发布我的解决方案(这很糟糕)。
我使用 wmctrl(您可以使用 xprop 代替)和 xdotool 来执行此操作。以前,我使用扩展程序使 URL 在标题栏中可见(然后您可以通过 xprop 或 wmctrl 访问该 URL)。这两种方法都工作得很好,尽管它不是真正“干净”。
id=$(wmctrl -l | grep -oP "(?<=)(0x\w+)(?=.*Chromium)") //Put here the regex for the browser you use
xdotool key --window $id "ctrl+l"
xdotool key --window $id "ctrl+c"
Run Code Online (Sandbox Code Playgroud)
您现在在剪贴板中有该 url。然后我使用 xclip 来处理 URL。
如果有人找到,我很想看到一个真正的解决方案。
小智 3
您能否说出执行后出现的错误,因为该脚本正在为我工作。这里有同样的问题,使用php
and回答perl
:
由于主要部分是Python,所以这里是一个纯粹的Python脚本,要做同样的事情,也尝试一下:
#!/usr/bin/python
import json
f = open("recovery.js","r")
jdata = json.loads(f.read())
f.close()
number_of_selected_tab = jdata["windows"][0]["selected"]
tab_number = 1
for win in jdata.get("windows"):
for tab in win.get("tabs"):
if number_of_selected_tab == tab_number :
tab_index = tab.get("index") - 1
print tab.get("entries")[tab_index].get("url")
tab_number = tab_number + 1
Run Code Online (Sandbox Code Playgroud)
我将/home/username/.mozilla/firefox/xxxxx.default/sessionstore.js
文件替换为recovery.js
. 就我而言(Mozilla Firefox 44.0、openSUSE 13.1),文件是~/.mozilla/firefox/*.default/sessionstore-backups/recovery.js
. 最后但并非最不重要的一点是,如果您有两个正在运行的实例,则脚本将不起作用Firefox
。