-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
executable file
·194 lines (154 loc) · 5.25 KB
/
Copy pathbackground.js
File metadata and controls
executable file
·194 lines (154 loc) · 5.25 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const MAX_SUGGESTIONS = 8;
const DESTINATION_LABEL_MAX_LENGTH = 52;
let latestInputRequest = 0;
function formatUrl(url) {
if (!url.startsWith('http://') && !url.startsWith('https://')) {
return `https://${url}`;
}
return url;
}
function escapeOmniboxText(text) {
return String(text)
.replaceAll('&', '&')
.replaceAll('<', '<')
.replaceAll('>', '>')
.replaceAll('"', '"')
.replaceAll("'", ''');
}
function truncate(text, maxLength) {
if (text.length <= maxLength) {
return text;
}
return `${text.slice(0, maxLength - 1)}\u2026`;
}
function getDestinationLabel(url) {
try {
const parsedUrl = new URL(formatUrl(url));
const hostname = parsedUrl.hostname.replace(/^www\./, '');
const path = parsedUrl.pathname === '/' ? '' : parsedUrl.pathname;
return truncate(`${hostname}${path}`, DESTINATION_LABEL_MAX_LENGTH);
} catch {
return truncate(url, DESTINATION_LABEL_MAX_LENGTH);
}
}
function getMatchingLinks(items, text) {
const query = text.toLocaleLowerCase();
return Object.entries(items)
.filter(([, url]) => typeof url === 'string')
.map(([key, url]) => {
const lowerCaseKey = key.toLocaleLowerCase();
let rank = 2;
if (lowerCaseKey === query) {
rank = 0;
} else if (lowerCaseKey.startsWith(query)) {
rank = 1;
}
return { key, url, lowerCaseKey, rank };
})
.filter(({ lowerCaseKey }) => lowerCaseKey.includes(query))
.sort((first, second) =>
first.rank - second.rank ||
first.key.localeCompare(second.key, undefined, { sensitivity: 'base' })
);
}
function normalizeOmniboxInput(text) {
return /^\s*$/.test(text) ? '' : text;
}
function formatHighlightedKey(key, text) {
if (!text) {
return `<match>${escapeOmniboxText(key)}</match>`;
}
const matchIndex = key.toLocaleLowerCase().indexOf(text.toLocaleLowerCase());
if (matchIndex === -1) {
return escapeOmniboxText(key);
}
const prefix = key.slice(0, matchIndex);
const match = key.slice(matchIndex, matchIndex + text.length);
const suffix = key.slice(matchIndex + text.length);
return `${escapeOmniboxText(prefix)}<match>${escapeOmniboxText(match)}</match>${escapeOmniboxText(suffix)}`;
}
function formatSuggestionDescription(link, text) {
const key = formatHighlightedKey(link.key, text);
const destination = escapeOmniboxText(getDestinationLabel(link.url));
return `<match>TextLink</match><dim>: </dim>${key}<dim> \u2192 </dim><url>${destination}</url>`;
}
function setDefaultSuggestion(description) {
chrome.omnibox.setDefaultSuggestion({ description });
}
function updateDefaultSuggestion(text, matches, totalLinks) {
const escapedText = escapeOmniboxText(text);
if (!text) {
if (totalLinks === 0) {
setDefaultSuggestion('<dim>No saved TextLinks yet</dim>');
return;
}
const noun = totalLinks === 1 ? 'TextLink' : 'TextLinks';
setDefaultSuggestion(
`<match>${totalLinks} saved ${noun}</match><dim> \u2014 start typing to filter</dim>`
);
return;
}
if (matches.length === 0) {
setDefaultSuggestion(`<dim>No TextLinks match \u201c</dim>${escapedText}<dim>\u201d</dim>`);
return;
}
if (matches.length === 1) {
setDefaultSuggestion(formatSuggestionDescription(matches[0], text));
return;
}
setDefaultSuggestion(
`<match>${matches.length} TextLinks</match><dim> match \u201c</dim>${escapedText}<dim>\u201d \u2014 press \u2193 to choose</dim>`
);
}
function loadLinks(callback) {
chrome.storage.sync.get(null, (items) => callback(items || {}));
}
setDefaultSuggestion('<dim>Start typing to filter, or press Space to list all TextLinks</dim>');
chrome.omnibox.onInputStarted.addListener(() => {
const requestId = ++latestInputRequest;
loadLinks((items) => {
if (requestId !== latestInputRequest) {
return;
}
const links = getMatchingLinks(items, '');
updateDefaultSuggestion('', links, links.length);
});
});
chrome.omnibox.onInputChanged.addListener((text, suggest) => {
const requestId = ++latestInputRequest;
const query = normalizeOmniboxInput(text);
loadLinks((items) => {
if (requestId !== latestInputRequest) {
return;
}
const allLinks = getMatchingLinks(items, '');
const matches = getMatchingLinks(items, query);
const suggestions = matches.length === 1 ? [] : matches;
updateDefaultSuggestion(query, matches, allLinks.length);
suggest(
suggestions.slice(0, MAX_SUGGESTIONS).map((link) => ({
content: link.key,
description: formatSuggestionDescription(link, query),
}))
);
});
});
chrome.omnibox.onInputEntered.addListener((text) => {
++latestInputRequest;
const query = normalizeOmniboxInput(text);
loadLinks((items) => {
const matches = getMatchingLinks(items, query);
const exactMatch = matches.find(
({ key }) => key.toLocaleLowerCase() === query.toLocaleLowerCase()
);
const selectedLink = exactMatch || (matches.length === 1 ? matches[0] : null);
if (!selectedLink) {
return;
}
chrome.tabs.update({ url: formatUrl(selectedLink.url) });
});
});
chrome.omnibox.onInputCancelled.addListener(() => {
++latestInputRequest;
setDefaultSuggestion('<dim>Start typing to filter, or press Space to list all TextLinks</dim>');
});