-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
116 lines (99 loc) · 3.79 KB
/
background.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
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === 'install') {
chrome.tabs.create({ url: 'onboarding.html' });
}
});
function ext() {
chrome.action.disable();
chrome.declarativeContent.onPageChanged.removeRules(undefined, () => {
let rule = {
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { hostEquals: 'github.com' },
}),
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { hostEquals: 'gitlab.com' },
}),
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { hostEquals: 'bitbucket.org' },
}),
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { hostEquals: "dev.azure.com" },
}),
],
actions: [new chrome.declarativeContent.ShowAction()],
};
let rules = [rule];
chrome.declarativeContent.onPageChanged.addRules(rules);
});
}
chrome.runtime.onStartup.addListener(() => {
ext();
});
chrome.action.onClicked.addListener((tab) => {
console.log('Extension icon clicked');
chrome.storage.sync.get(
{
protocol: 'HTTPS',
application: 'VS Code',
},
function (items) {
console.log('Storage items:', items);
chrome.tabs.query({ active: true, lastFocusedWindow: true }, (tabs) => {
console.log('Active tab:', tabs[0]);
const url = new URL(tabs[0].url);
const path = url.pathname.split('/');
let openApp = '';
if (items.application == 'VS Code') {
openApp = 'vscode';
} else if (items.application == 'VS Code Insiders') {
openApp = 'vscode-insiders';
} else if (items.application == 'VSCodium') {
openApp = 'vscodium';
} else if (items.application == 'VSCodium Insiders') {
openApp = 'vscodium-insiders';
}
console.log('Open app:', openApp);
if (path[1] !== undefined && path[2] !== undefined) {
let vscodeUrl = '';
if (items.protocol == 'HTTPS') {
vscodeUrl = openApp + '://vscode.git/clone?url=https://' + url.hostname + '/' + path[1] + '/' + path[2] + '.git';
console.log('VS Code URL:', vscodeUrl);
} else if (items.protocol == 'SSH') {
vscodeUrl = openApp + '://vscode.git/clone?url=git@' + url.hostname + ':' + path[1] + '/' + path[2] + '.git';
console.log('VS Code URL:', vscodeUrl);
}
// Workaround to be able to launch custom app URIs like cursor://, vscode://, vscode-insiders:// etc.
const newTabUrl = chrome.runtime.getURL('open.html') + `?url=${encodeURIComponent(vscodeUrl)}`;
chrome.tabs.create({ url: newTabUrl });
}
});
},
);
});
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.vscodeUrl) {
chrome.tabs.update({ url: request.vscodeUrl });
}
});
// New listener for storage changes
chrome.storage.onChanged.addListener(function (changes, namespace) {
for (let key in changes) {
let storageChange = changes[key];
console.log('Storage key "%s" in namespace "%s" changed. Old value was "%s", new value is "%s".', key, namespace, storageChange.oldValue, storageChange.newValue);
}
});
// let vscodeTabId = null;
// chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
// if (request.vscodeUrl) {
// chrome.tabs.create({ url: request.vscodeUrl }, (tab) => {
// vscodeTabId = tab.id;
// });
// }
// });
// chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
// if (tabId === vscodeTabId && changeInfo.status === 'complete') {
// chrome.tabs.remove(tabId);
// vscodeTabId = null;
// }
// });