This repository was archived by the owner on Nov 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuser-content.lua
92 lines (83 loc) · 2.74 KB
/
user-content.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
function description()
return "Loads custom CSS and JS"
end
-- Loads a CSS or JS file into the current document
function run()
if #arguments > 0 then
local file_name = arguments[1]
log_info("loading " .. file_name)
window_index = focused_window_index()
webview_index = focused_webview_index(window_index)
if string.ends_with(file_name, ".css") then
return load_css(file_name, window_index, webview_index)
elseif string.ends_with(file_name, ".js") then
return load_js(file_name, window_index, webview_index)
end
end
return false
end
-- Loads all CSS and JS files from the configuration option
-- `user-content.default-paths` into every page, as well as the files from
-- `user-content.site-paths` based on the domain name of the page.
function on_load_uri()
load_default_files()
load_site_files()
end
function load_site_files()
host = string.gmatch(string.match(requested_uri, "://(.*)"), "[^/]+")()
base_paths = lookup_strings(config_file_path, "user-content.site-paths")
for _, base_path in ipairs(base_paths) do
load_css(string.format("%s/%s.css", base_path, host), window_index, webview_index)
load_js(string.format("%s/%s.js", base_path, host), window_index, webview_index)
end
end
function load_default_files()
default_paths = lookup_strings(config_file_path, "user-content.default-paths")
for _, base_path in ipairs(default_paths) do
for _, file_name in ipairs(list_files(base_path)) do
if string.ends_with(file_name, ".css") then
load_css(string.format("%s/%s", base_path, file_name), window_index, webview_index)
elseif string.ends_with(file_name, ".js") then
load_js(string.format("%s/%s.js", base_path, file_name), window_index, webview_index)
end
end
end
end
function load_css(path, window_index, webview_index)
css = load_file(path)
if css then
log_info(string.format("Loading CSS: %s", path))
add_styles(window_index, webview_index, css)
return true
end
return false
end
function load_js(path, window_index, webview_index)
js = load_file(path)
if js then
log_info(string.format("Loading JS: %s", path))
run_javascript(window_index, webview_index, js)
return true
end
return false
end
function load_file(file_path)
local file = io.open(file_path)
if not file then return nil end
local content = file:read("*a")
file:close()
return content
end
function list_files(directory)
local index, files = 0, {}
local list_handle = io.popen('ls "' .. directory .. '"')
for filename in list_handle:lines() do
index = index + 1
files[index] = filename
end
list_handle:close()
return files
end
function string.ends_with(text, ending)
return ending == '' or string.sub(text, -string.len(ending)) == ending
end