如何从终端的firefox选项卡获取URL?
有没有办法使用某种命令获取此类URL?
包含有关会话信息的文件很少:
-
~/.mozilla/firefox/*.default/sessionstore-backups/recovery.js
包含有关当前会话的信息,还包含已关闭选项卡和上一个会话的信息。 Firefox每隔15秒在此文件中创建一个备份 。 Firefox关闭时,此文件不可用。 -
~/.mozilla/firefox/*.default/sessionstore.js
,其中包含有关Firefox浏览器关闭时最后一个会话的信息。 Firefox打开时,此文件不可用。 -
~/.mozilla/firefox/*.default/sessionstore-backups/previous.js
,其中包含有关上一个会话的信息。
对recovery.js
内容的分析表明,对于每个选项卡,只有当前URL的条目包含字符串attributes
。
I.当Firefox打开时:
1.A. 如果要从当前会话中获取打开的选项卡的所有URL,可以使用以下命令:
cat $HOME/.mozilla/firefox/*.default/sessionstore-backups/recovery.js | \ sed "s/\\_closedTabs.*//" | \ sed "s/{/\n{/g" | \ egrep -o 'url.*attributes' | \ cut -d\" -f3
*请注意,您必须copy
所有行copy
/ paste
到终端窗口中,然后按Enter键 。
哪里:
-
cat $HOME/.mozilla/firefox/*.default/sessionstore-backups/recovery.js
将打印此文件的内容; -
sed "s/\\_closedTabs.*//"
将删除字符串_closedTabs
之后的所有内容; -
sed "s/{/\n{/g" | \
sed "s/{/\n{/g" | \
将在每个{
之前放一个换行符; -
egrep -o 'url.*attributes'
将仅过滤以url
开头并以attributes
结尾的行的那些部分。 如果没有-o
选项,将过滤包含该字符串的整行; -
cut -d\" -f3
将使用"
作为分隔符,仅过滤第3列。
在我的例子中,命令的输出是:
https://askubuntu.com/ https://www.mozilla.org/en-US/
1.B. 如果您想立即获取当前和上一个会话的数据,可以使用:
printf "\n# CurrentSession:\n"; \ cat $HOME/.mozilla/firefox/*.default/sessionstore-backups/recovery.js | \ sed "s/\\_closedTabs/\{\"url\":\"# ClosedTabs:\"attributes/g" | \ sed "s/\\lastSessionState/\{\"url\":\"# LastSession:\"attributes/" | \ sed "s/{/\n{/g" | \ egrep -o 'url":"*.*attributes*' | \ cut -d\" -f3 | \ sed "s/#/\n#/" \ ; echo
哪里:
-
printf "\n# CurrentSession:\n";
将打印# CurrentSession:
两个换行符之间; -
sed "s/\\_closedTabs/\{\"url\":\"# ClosedTabs:\"attributes/g"
将字符串_closedTabs
替换为"url":"# ClosedTabs:"attributes
在整个“文件”中(选项g
); -
sed "s/\\lastSessionState/\{\"url\":\"# LastSession:\"attributes/"
将lastSessionState
替换为"url":"# LastSession:"attributes
; -
sed "s/#/\n#/"
将在每个#
之前添加换行符。 -
; echo
; echo
将在底部添加一个空行。
在我的例子中,命令的输出是:
# CurrentSession: https://askubuntu.com/ https://www.mozilla.org/en-US/ # ClosedTabs: https://www.yahoo.com/ # LastSession: https://askubuntu.com/ https://www.abv.bg/ # ClosedTabs: https://www.google.com/gmail/about/ https://www.yahoo.com/
2.A. 如果你想得到历史,你可以使用:
cat $HOME/.mozilla/firefox/*.default/sessionstore-backups/recovery.js | sed "s/\\_closedTabs.*//" | sed "s/{/\n{/g" | egrep 'url":"http*' | cut -d\" -f4
在我的例子中,命令的输出是:
https://askubuntu.com/ https://www.google.bg/search?client=ubuntu&channel=fs&q=firefox&ie=utf-8&oe=utf-8&gfe_rd=cr&ei=pTKyWIitGqTs8wewj4KgDQ https://www.mozilla.org/bg/firefox/new/ https://www.mozilla.org/en-US/
2.B. 您可以在每个选项卡的数据之间放置一个分隔符:
cat $HOME/.mozilla/firefox/*.default/sessionstore-backups/recovery.js | \ sed "s/\\_closedTabs.*//" | \ sed "s/{/\n{/g" | \ sed "s/entries/url\":\"# TAB:/g" | \ egrep 'url":"*' | \ cut -d\" -f4 | \ sed "s/#/\n#/"
在我的例子中,命令的输出是:
# TAB: about:startpage https://askubuntu.com/ # TAB: https://www.google.bg/search?client=ubuntu&channel=fs&q=firefox&ie=utf-8&oe=utf-8&gfe_rd=cr&ei=pTKyWIitGqTs8wewj4KgDQ https://www.mozilla.org/bg/firefox/new/ https://www.mozilla.org/en-US/
3. 1.B. 和2.B. 一起:
printf "\n# CurrentSession:\n"; \ cat $HOME/.mozilla/firefox/*.default/sessionstore-backups/recovery.js | \ sed "s/\\_closedTabs/\{\"url\":\"# ClosedTabs:/g" | \ sed "s/\\lastSessionState/\{\"url\":\"# LastSession:/" | \ sed "s/entries/url\":\"# TAB:/g" | \ sed "s/{/\n{/g" | \ egrep 'url":"*' | \ cut -d\" -f4 | \ sed "s/#/\n#/" \ ; echo
在我的例子中,命令的输出是:
# CurrentSession: # TAB: https://host.bg/ https://admin.host.bg/ # TAB: https://www.mediawiki.org/wiki/MediaWiki # TAB: https://en.wikipedia.org/wiki/Main_Page # ClosedTabs: # TAB: about:startpage https://www.yahoo.com/ # LastSession: # TAB: about:startpage https://askubuntu.com/ # ClosedTabs: # TAB: https://www.mozilla.org/en-US/ https://www.google.com/gmail/about/
II。 当Firefox关闭时:
关闭Firefox后,您可以获取上次会话的数据。 方法与上面解释的相同,但不是recovery.js
你必须使用sessionstore.js
(或previous.js
):
cat $HOME/.mozilla/firefox/*.default/sessionstore.js \ ...
参考文献:
- 类似的问题在AskUbuntu ;
- Sed – Bruce Barnett的介绍和教程 ;
-
man sed
; -
man egrep
; -
man cut
。
拖放
拖动它然后放下它。 将其拖动到地址字段左侧的图标。 在大多数浏览器中,您可能会看到i用于图标。
复制粘贴
您也可以使用复制和粘贴。 复制地址字段中的文本。 将其粘贴到终端中。