跳转至内容

Luakit

来自 ArchWiki

Luakit 是一个极快、轻量级且灵活的基于webkit引擎的网页浏览器。它可以通过lua脚本进行定制,并且完全支持键盘快捷键操作。它使用GTK 3和WebKit2GTK

安装

安装 luakit 包。

基本用法

本文或本节需要在语言、wiki 语法或风格方面进行改进。请参阅 Help:Style 获取参考。

原因: 我们真的需要重复上游或内部文档吗?(请在Talk:Luakit讨论)
注意 快捷键列表在一个特殊页面中,可以通过 :binds 命令访问。

按下 : 即可访问命令提示符。你可以通过它完成几乎所有操作。使用 Tab 键进行命令自动补全。

使用 :help 命令获取关于可用键盘快捷键及其功能的帮助信息。(要查看特定键盘快捷键的操作是如何在Lua中实现的,请点击其帮助文本中的任意位置。)

要退出,请使用 :quit 命令,或者按 Shift+z 然后按 Shift+q。你也可以通过使用 :writequit 命令,或者连续按两次 Shift+z 来关闭浏览器,同时记住会话(即恢复标签页)。

浏览

  • o 键打开一个带 :open 命令的提示符,并输入你想访问的URI。按 Shift+o 键编辑当前URI。
  • 如果它不是一个已识别的URI,Luakit将使用默认搜索引擎。参见 #自定义搜索引擎
  • 通过在输入前加上相应的关键词来指定要使用的搜索引擎(例如,:open google foobar 将在Google上搜索foobar)。
  • 使用常用快捷键进行导航。对于 EmacsVim爱好者,提供了一些他们常用的快捷键。你也可以使用鼠标。
  • f 键显示所有可见链接的索引。输入相应的数字或字符串的一部分即可打开链接。
  • Shift+f 键在新标签页中打开链接。
  • Ctrl+t 打开一个新标签页,按 Ctrl+w 关闭它。按 t 键弹出URI输入框,在新标签页中打开;按 Shift+t 键编辑当前URI,并在新标签页中打开。
  • w 键弹出URI输入框,在新窗口中打开;按 Shift+w 键编辑当前URI,并在新窗口中打开。
  • 通过按 g 然后按 tShift+t 来切换标签页,或者使用 Ctrl+PageUpCtrl+PageDown
  • 你可以通过 Alt+数字 切换到指定的标签页。
  • Shift+h 返回浏览器历史记录。
  • Shift+l 前进到浏览器历史记录。
  • 使用 <> 重新排序标签页。
  • r 键重新加载页面,按 Ctrl+c 停止加载。
  • u 键重新打开最后一个关闭的标签页。
  • g 然后按 d 打开下载页面(或按 Shift+d 在新标签页中打开)。
  • y 键将URI复制到主选择区。
  • 使用 :viewsource 查看页面源代码。使用 :viewsource! 返回正常视图。
  • ; 然后按 i 查看图片源代码(或按 Shift+i 在新标签页中打开)。
  • 使用 :inspect 检查元素。重复输入以在新窗口中打开。使用 :inspect! 禁用检查器。

输入框

许多网页有可编辑的元素,如下拉列表、复选框、文本框等。虽然用鼠标操作它们很方便,但在使用跟随命令时可能会遇到一些麻烦。在这种情况下,按下方向键可能会有帮助。或者,可以使用 g i 快捷键来聚焦输入框。

书签

如果已启用(默认配置),则可以在Luakit内部使用书签。

  • :bookmarks 命令打开书签页面。(快捷键:g 后跟 b,或 Shift+b 在新标签页中打开)。
  • :bookmark [URI [tags]] 命令将指定的URI(或省略时为当前标签页的URI)添加到书签,并指定标签。从版本2012-09-13-r1开始,书签页面将在保存前打开(新标签页)并进入新的书签编辑模式。(快捷键:Shift+b)。

配置

配置在 ~/.config/luakit/userconf.lua 中完成。不再需要复制和修改 rc.lua。一些设置也可以使用 :settings 命令进行修改,除非你在 userconf.lua 中进行设置,例如:

~/.config/luakit/userconf.lua
local settings = require "settings"
settings.example = "some value"

按键绑定

大多数绑定需要一些Luakit的知识,但你至少可以做一些简单的重新绑定操作

~/.config/luakit/userconf.lua
local modes = require "modes"

-- Creates new bindings from old ones.
modes.remap_binds("normal", -- This is the mode in which the bindings are active.
  {
  --  new     old     removes the old binding (defaults to false)
     {"O",    "t",    true},
  -- define as many as you wish
    {"Control-=", "zi"},
    ...
  })

要将按键绑定到命令,可以使用以下模板

~/.config/luakit/userconf.lua
modes.add_binds("normal", {
-- {"<key>",
--  "<description>",
--  function (w) w:enter_cmd("<command>") end}
  {"O", "Open URL in a new tab.",
   function (w) w:enter_cmd(":tabopen ") end},
   ...
})

作为灵感,请参阅 /usr/share/luakit/lib/binds.lua,其中定义了默认绑定。

主页

如下设置你的主页

~/.config/luakit/userconf.lua
settings.window.home_page = "www.example.com"

自定义搜索引擎

要使用默认搜索引擎进行搜索,请按 o 并输入短语。要使用不同的搜索引擎进行搜索,请在 o 之后输入其名称,然后输入短语。

你可以添加几乎任何你想要的搜索引擎。在你想搜索的网站上进行搜索,然后将URI复制粘贴到Luakit配置中,并将搜索的术语替换为 %s。例如:

~/.config/luakit/userconf.lua
local engines = settings.window.search_engines
engines.aur          = "https://aur.archlinux.org/packages?K=%s"
engines.aw           = "https://wiki.archlinux.org.cn/index.php/Special:Search?fulltext=Search&search=%s"
engines.googleseceng = "https://www.google.com/search?name=f&hl=en&q=%s"

该变量用作Luakit中 :open 命令的关键字。

使用相同的关键字设置默认搜索引擎

~/.config/luakit/userconf.lua
engines.default = engines.aur

而不是字符串,你可以将搜索引擎定义为返回字符串的函数。例如,这是一个维基百科搜索引擎,允许你指定语言(默认为英语)

~/.config/luakit/userconf.lua
engines.wikipedia = function (arg)
  local l, s = arg:match("^(%a%a):%s*(.+)")
  if l then
    return "https://" .. l .. ".wikipedia.org/wiki/Special:Search?search=" .. s
  else
    return "https://en.wikipedia.org/wiki/Special:Search?search=" .. arg
  end
end,

如果调用 :open wikipedia arch linux,这将打开英文维基百科上的Arch Linux页面;如果使用 :open wikipedia fr: arch linux,则会改用法语维基百科。

下载位置

指定下载位置

~/.config/luakit/userconf.lua
require "downloads"
downloads.default_dir = os.getenv("HOME") .. "/mydir"

默认位置是 $XDG_DOWNLOAD_DIR(如果存在),否则是 $HOME/downloads

广告拦截

Adblock 默认加载,但你需要

  • 获取一个adblock兼容列表,例如 Easylist,并将其保存到 ~/.local/share/luakit/adblock
  • 重新启动Luakit以加载扩展。
  • 在Luakit内部使用 :adblock-list-enable 数字 命令来启用你下载的Adblock列表。Adblock本身会在启动时启用。

可以在 luakit://adblock/ 内部页面使用 :adblockg Shift+a 获取已启用列表和AdBlock状态的完整信息,前提是启用了 adblock_chrome 模块,但这不是必需的。

注意 要让Adblock在正常模式下运行,easylist.txt 及其他文件必须放在 ~/.local/share/luakit/adblock 目录中。

书签管理

同步

从2012.09.13版本开始,Luakit书签存储在SQLite数据库中:~/.local/share/luakit/bookmarks.db

你可以将默认文件替换为符号链接,将书签存储在机器上的任何位置。这样,如果你使用Dropbox等云同步应用程序,就可以在不同的计算机之间保持书签的同步。

将纯文本书签转换为SQLite格式

以前书签存储在一个简单的纯文本文件中:~/.local/share/luakit/bookmarks。每一行是一个书签。它包含2个字段,链接,它们由一个制表符字符分隔。

警告 如果插入的是空格而不是制表符,链接将无法正确地被标记为书签。
注意 组和链接是按字母顺序排序的,所以不需要手动排序。

要使用最新Luakit版本的书签,必须转换该文件。一个示例Lua脚本可以完成此操作

bookmarks_plain_to_sqlite.lua
local usage = [[Usage: luakit -c bookmarks_plain_to_sqlite.lua [bookmark plaintext path] [bookmark db path]
]]

local old_db_path, new_db_path = unpack(uris)

if not old_db_path or not new_db_path then
   io.stdout:write(usage)
   luakit.quit(1)
end

-- One-pass file read into 'data' var.
old_db = assert(io.open(old_db_path, "r"))
local data = old_db:read("*all")
assert(old_db:close())

-- Init new_db, otherwise sqlite queries will fail.
new_db = sqlite3{ filename = new_db_path }
new_db:exec("CREATE TABLE IF NOT EXISTS bookmarks (id INTEGER PRIMARY KEY, uri TEXT NOT NULL, title TEXT NOT NULL, desc TEXT NOT NULL, tags TEXT NOT NULL, created INTEGER, modified INTEGER )")

-- Fill
local url,tag

for line in data:gmatch("[^\n]*\n?") do

   if string.len(line) > 1 then

      print ("["..line.."]")

      -- Get url and tag (if present) from first line.
      _, _, url, tag = string.find(line, "([^\n\t]+)\t*([^\n]*)\n?")

      -- Optional yet convenient output.
      io.write(url)
      io.write("\t")
      io.write(tag)
      io.write("\n")

      -- DB insertion. Nothing will be overwritten. If URL and/or tag already exists, then a double is created.
      new_db:exec("INSERT INTO bookmarks VALUES (NULL, ?, ?, ?, ?, ?, ?)",
                  {
                     url, "", "", tag or "",
                     os.time(), os.time()
                  })
      end
end

print("Import finished.")
print("\nVacuuming database...")
new_db:exec "VACUUM"
print("Vacuum done.")

luakit.quit(0)

如脚本开头所述,它必须用Luakit运行

$ luakit -c bookmarks_plain_to_sqlite.lua path/to/plaintext/bookmark path/to/db

旧的纯文本书签将保持不变。如果DB书签不存在,将创建该文件。如果它存在,请不用担心,之前的所有书签都不会被触及。但是,这种行为意味着你可能会得到一些重复项。

从Firefox导入

要从Firefox导入书签,首先必须使用其书签管理器将其导出为HTML文件。之后,XML文件就可以转换为Luakit格式。

下面的单行awk命令将完成此操作

$ cat bookmarks.html | awk '
{gsub(/\"/," ")}
/<\/H3>/{FS=">";gsub(/</,">");og=g;g=$(NF-2);FS=" "}
/<DL>/{x++;if(x>= 3)gl[x-3]=g}
/<\/DL>/{x--;if(x==2)g=og"2"}
/HREF/{gsub(/</," ");gsub(/>/," ");if(g!=""){if(og!=g){printf "\n";og=g};printf "%s\t",$4;if(x>=3){for(i=0;i<=x-4;i++){printf "%s-",gl[i]}printf "%s\n",gl[x-3]}else{printf "\n"}}}'

脚本的可读性更高的版本

ff2lk.awk
# Notes: 'folders' for Firefox bookmarks mean 'groups' for Luakit.

# Put spaces where it is needed to delimit words properly.
{gsub(/\"/," ")}

# Since the folder name may have spaces, delimiter must be ">" here.
/<\/H3>/ {
    FS=">"
    gsub(/</,">")
    oldgroup=group
    group=$(NF-2)
    FS=" "
}

# Each time a <DL> is encountered, it means we step into a subfolder.
# 'count' is the depth level.
# Base level starts at 2 (Firefox fault).
# 'groupline' is an array of all parent folders.
/<DL>/ {
    count++
    if ( count >= 3 )
        groupline[count-3]=group
}

# On </DL>, we step out.
# If if return to the base level (i.e. not in a folder), then we give 'group' a fake name different
# from 'oldgroup' to make sure a line will be skipped (see below).
/<\/DL>/ {
    count--
    if( count == 2 )
        group=oldgroup"ROOT"
}

# The bookmark name.
# If oldgroup is different than group, (i.e. folder changed) then we skip a line.
# If we are in a folder, then we print the group name, i.e. all parents plus the current folder
# separated by an hyphen.
/HREF/ {
    gsub(/</," ")
    gsub(/>/," ")
    if (group != "")
    {
        if(oldgroup != group)
        {
            printf "\n"
            oldgroup=group
        }
        printf "%s\t",$4
        if ( count >= 3 )
        {
            for ( i=0 ; i <= count-4 ; i++ )
            {printf "%s-" , groupline[i]}
            printf "%s" , groupline[count-3]
        }
        printf "\n"
    }
}

用以下方式运行它

$ awk -f ff2lk.awk bookmarks.html >> bookmarks

然后,按照 #将纯文本书签转换为SQLite格式 中所述,将生成的纯文本书签转换为SQLite格式。

导出书签

下面的脚本允许你将Luakit书签从SQLite格式导出为纯文本文件。生成的文件可能适用于其他网页浏览器,或者可以被导入脚本轻松解析。

bookmarks_sqlite_to_plain.lua
-- USER CONFIG

local sep = " "

-- END OF USER CONFIG

local usage = [[Usage: luakit -c bookmarks_sqlite_to_plain.lua [bookmark db path] [bookmark plain path]

DB scheme is

    bookmarks (
        id INTEGER PRIMARY KEY,
        uri TEXT NOT NULL,
        title TEXT NOT NULL,
        desc TEXT NOT NULL,
        tags TEXT NOT NULL,
        created INTEGER,
        modified INTEGER
    );
]]

local old_db_path, new_db_path = unpack(uris)

if not old_db_path or not new_db_path then
   io.stdout:write(usage)
   luakit.quit(1)
end

-- One-pass file read into 'data' var.
new_db = assert(io.open(new_db_path, "w"))

-- Open old_db
old_db = sqlite3{ filename = old_db_path }

-- Load all db values to a string variable.
local rows = old_db:exec [[ SELECT * FROM bookmarks ]]

-- Iterate over all entries.
-- Note: it could be faster to use one single concatenation for all entries, but
-- it would be much more code and not so flexible. It is desirable to focus on
-- clarity. After all, only a few hundred lines are handled.
for _, b in ipairs(rows) do

   -- Change %q for %s to remove double quotes if needed.
   -- You can toggle the desired fields with comments.
   local outputstr =
      string.format("%q%s", b.uri or "", sep) ..
      string.format("%q%s", b.title or "", sep) ..
      string.format("%q%s", b.desc or "", sep) ..
      string.format("%q%s- ", b.tags or "", sep) ..
      ((b.created or "" ) .. sep) ..
      ((b.modified or "" ) .. sep) ..
      "\n"

   -- Write entry to file.
   new_db:write(outputstr)
end


print("Export done.")

assert(new_db:close())

luakit.quit(0)

如脚本开头所述,它必须用Luakit运行

$ luakit -c bookmarks_plain_to_sqlite.lua path/to/plaintext/bookmarks path/to/database

Tor

一旦 Tor 设置完成,只需运行

$ torsocks luakit --nounique
警告 为了确保匿名性,你还需要更改Luakit的设置,例如禁用Flash和更改用户代理字符串。

自定义CSS

找到luakit数据存储目录下的 styles 子目录。通常,它位于 ~/.local/share/luakit/styles/。如果该目录不存在,请创建它。将任何CSS规则移动到该目录下的新文件中。文件名必须以 .css 结尾。确保指定你的样式表应应用于哪些网站。方法是使用 @-moz-document 规则。Stylish wiki页面 Applying styles to specific sites 可能有帮助。运行 :styles-reload 来检测新的样式表文件并重新加载对现有样式表文件的任何更改;无需重新启动luakit。

要打开样式菜单,请运行命令 :styles-list。在这里你可以启用/禁用样式表,在文本编辑器中打开样式表,并查看哪些样式表是活动的。

如果一个样式表对所有页面都禁用,其状态将显示为“Disabled”。如果一个样式表对所有页面都启用,但不适用于当前页面,其状态将显示为“Enabled”。如果一个样式表对所有页面都启用_并且_它适用于当前页面,其状态将显示为“Active”。

参见

© . This site is unofficial and not affiliated with Arch Linux.

Content is available under GNU Free Documentation License 1.3 or later unless otherwise noted.