-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChromeTabCopyLink.lua
58 lines (47 loc) · 1.74 KB
/
ChromeTabCopyLink.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
-- Kopiert den Titel und die URL aus Chrome und speichert sie als Markdown in die Zwischenablage
fileInfo()
local function getTLD(url)
local hostname = url:match("://(.-)/") or url
hostname = hostname:gsub("^www%.", "")
return hostname:match("([^%.]+%.[^%.]+)$") -- Holt das letzte Domain-Level
end
local function processTitleAndURL(output)
local json = hs.json.decode(output)
if not json or json.error then
print("Failed to extract URL and title: " .. (json and json.error or "Unknown error"))
return
end
local tld = getTLD(json.url)
if not tld then
print("Could not determine TLD for URL: " .. json.url)
return
end
-- TODO make as config
local markdown = string.format("%s: [%s](%s)", tld, json.title, json.url)
hs.pasteboard.setContents(markdown)
print("Copied to clipboard:\n" .. markdown)
end
local function fetchTitleAndURLFromChrome()
print("Fetching title and URL from Google Chrome...")
local script = [[
(function() {
const app = Application("Google Chrome");
const frontWindow = app.windows[0];
if (!frontWindow) return JSON.stringify({ error: "No Chrome window found" });
const activeTab = frontWindow.activeTab;
return JSON.stringify({
title: activeTab.title(),
url: activeTab.url()
});
})();
]]
local ok, output, message = hs.osascript.javascript(script)
if ok and output and output ~= "" then
processTitleAndURL(output)
else
print("Error fetching data from Chrome: " .. tostring(message))
end
end
hs.hotkey.bind(hyper, "c", keyInfo("Copy ChromeTab URL"), function()
fetchTitleAndURLFromChrome()
end)