-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
36 lines (30 loc) · 1.04 KB
/
background.js
File metadata and controls
36 lines (30 loc) · 1.04 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
const MENU_ID = "gitlab-search";
const DEFAULT_GITLAB_URL = "https://gitlab.example.com";
function buildSearchUrl(gitlabUrl, query) {
const base = gitlabUrl.replace(/\/$/, "");
return `${base}/search?search=${encodeURIComponent(query)}`;
}
function createContextMenu(gitlabUrl) {
browser.contextMenus.removeAll(() => {
browser.contextMenus.create({
id: MENU_ID,
title: 'Search GitLab for "%s"',
contexts: ["selection"],
});
});
}
browser.storage.local.get({ gitlabUrl: DEFAULT_GITLAB_URL }).then((settings) => {
createContextMenu(settings.gitlabUrl);
});
browser.storage.onChanged.addListener((changes) => {
if (changes.gitlabUrl) {
createContextMenu(changes.gitlabUrl.newValue);
}
});
browser.contextMenus.onClicked.addListener((info) => {
if (info.menuItemId !== MENU_ID || !info.selectionText) return;
browser.storage.local.get({ gitlabUrl: DEFAULT_GITLAB_URL }).then((settings) => {
const url = buildSearchUrl(settings.gitlabUrl, info.selectionText.trim());
browser.tabs.create({ url });
});
});