forked from QIanGua/Alfred-Workflow-for-Arc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist-tabs.js
executable file
·135 lines (127 loc) · 4.14 KB
/
list-tabs.js
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env osascript -l JavaScript
function run(args) {
let browser = "Arc";
console.log("browser: ", browser);
if (!Application(browser).running()) {
return JSON.stringify({
items: [
{
title: `${browser} is not running`,
subtitle: `Press enter to launch ${browser}`,
},
],
});
}
var excludeLocation = args[0] || "";
let chrome = Application(browser);
let favorite = args[1] ? getDomain(args[1]) : null;
chrome.includeStandardAdditions = true;
let tabsMap = {};
let windowCount = chrome.windows.length;
// console.log("windownCount: ", windowCount);
if (excludeLocation == "topApp") {
ListTopApps(chrome, tabsMap, false);
}
else if (excludeLocation == "") {
ListSpaceTabs(chrome, tabsMap, browser, windowCount, excludeLocation, true);
ListTopApps(chrome, tabsMap, true);
}
else {
ListSpaceTabs(chrome, tabsMap, browser, windowCount, excludeLocation, false);
}
let items = Object.keys(tabsMap).reduce((acc, url) => {
if (favorite === null || getDomain(url) === favorite) {
acc.push(tabsMap[url]);
}
return acc;
}, []);
return JSON.stringify({ items });
}
function ListTopApps(chrome, tabsMap, ifshowlocation) {
let tabsTitle = chrome.windows[0].tabs.title();
let tabsUrl = chrome.windows[0].tabs.url();
let tabsLocation = chrome.windows[0].tabs.location();
for (let t = 0; t < tabsTitle.length; t++) {
let url = tabsUrl[t] || "";
let matchUrl = url.replace(/(^\w+:|^)\/\//, "");
let title = tabsTitle[t] || matchUrl;
let location = tabsLocation[t] || "";
if (location != "topApp") {
continue;
}
args = `${0},undefined,${t},${url}`;
// console.log("args: ", args);
if (ifshowlocation) {
match = `${title} ${decodeURIComponent(matchUrl).replace(/[^\w]/g, " ")} Favorites ${location}`;
subtitle = `Favorites: ${url}`;
}
else {
match = `${title} ${decodeURIComponent(matchUrl).replace(/[^\w]/g, " ")}`;
subtitle = `${url}`;
}
tabsMap[url] = {
title,
url,
subtitle: subtitle,
windowIndex: 0,
tabIndex: t,
quicklookurl: url,
arg: args,
match: match,
};
}
}
function ListSpaceTabs(chrome, tabsMap, browser, windowCount, excludeLocation, ifshowlocation) {
let spaceCount = chrome.windows.spaces.length;
// console.log("spaceCount: ", spaceCount);
let tabsTitle =
browser === "Safari"
? chrome.windows.spaces.tabs.name()
: chrome.windows.spaces.tabs.title();
// console.log("tabsTitle: ", tabsTitle);
let tabsUrl = chrome.windows.spaces.tabs.url();
// console.log("tabsUrl: ", tabsUrl);
let tabsLocation = chrome.windows.spaces.tabs.location();
for (let w = 0; w < windowCount; w++) {
for (let s = 0; s < spaceCount; s++) {
let spacesTitle = chrome.windows.spaces.name()[w][s];
if (tabsTitle[w][s]) {
for (let t = 0; t < tabsTitle[w][s].length; t++) {
let url = tabsUrl[w][s][t] || "";
let matchUrl = url.replace(/(^\w+:|^)\/\//, "");
let title = tabsTitle[w][s][t] || matchUrl;
let location = tabsLocation[w][s][t] || "";
// exclude tabs from the current location
if (location == excludeLocation) {
continue;
}
args = `${0},${s},${t},${url}`;
// console.log("args: ", args);
if (ifshowlocation) {
match = `${title} ${decodeURIComponent(matchUrl).replace(/[^\w]/g, " ")} ${spacesTitle} ${location}`;
subtitle = `${spacesTitle}-${location}: ${url}`;
}
else {
match = `${title} ${decodeURIComponent(matchUrl).replace(/[^\w]/g, " ")} ${spacesTitle}`;
subtitle = `${spacesTitle}: ${url}`;
}
tabsMap[url] = {
title,
url,
subtitle: subtitle,
windowIndex: w,
spaceIndex: s,
tabIndex: t,
quicklookurl: url,
arg: args,
match: match,
};
}
}
}
}
}
function getDomain(url) {
const result = url.match(/^(?:https?:\/\/)?(?:[^@\n]+@)?([^:\/\n?]+)/im);
return result[1] ?? null;
}