Luakit

出自 ArchWiki

Luakit 是一款极其快速、轻量级且灵活的网络浏览器,使用 webkit 引擎。它可以通过 lua 脚本进行自定义,并完全可以使用键盘快捷键操作。它使用 GTK 3 和 WebKit2GTK

安装

安装 luakit 软件包 或 luakit-gitAUR 开发版本,正如官方网站所推荐的那样。

基本用法

本条目或章节需要语言、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*)。
  • 使用常用快捷键进行导航。对于 emacs 和 vim 爱好者,提供了一些他们常用的快捷键。您也可以使用鼠标。
  • 使用 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! 禁用检查器。

输入框

许多网页都有可编辑元素,如下拉列表、复选框、文本字段等。虽然它们可以使用鼠标完美工作,但您在使用*follow*命令时可能会遇到一些问题。在这种情况下,按箭头键可能会有所帮助。或者,可以使用 g i 快捷键来聚焦输入。

书签

如果启用(默认配置),则可以从 Luakit 中使用书签。

  • :bookmarks 命令打开书签页面。(快捷键:g,然后 b,或 Shift+b 用于新标签页)。
  • :bookmark [URI [标签]] 命令将指定的 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

除了字符串,您还可以将搜索引擎定义为返回字符串的函数。例如,这是一个 Wikipedia 搜索引擎,可让您指定语言(默认为英语)

~/.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,这将打开英文 Wikipedia 上的 Arch Linux 页面;使用 :open wikipedia fr: arch linux,这将改用法语 Wikipedia。

下载位置

指定下载位置

~/.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 本身将在启动时启用。

有关已启用列表和 AdBlock 状态的完整信息,可以在 luakit://adblock/ 内部页面上使用 :adblockg Shift+a 找到,前提是启用了 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 和更改 useragent 字符串。

自定义 CSS

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

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

如果样式表对所有页面都禁用,则其状态将列为“已禁用”。如果样式表对所有页面都启用,但不适用于当前页面,则其状态将列为“已启用”。如果样式表对所有页面都启用_并且_它适用于当前页面,则其状态将列为“活动”。

参见