-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
71 lines (64 loc) · 1.96 KB
/
Copy pathbackground.js
File metadata and controls
71 lines (64 loc) · 1.96 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
// Locator Vim - Background Service Worker
// Handles native messaging with the host application
const NATIVE_HOST_NAME = 'com.locatorvim.host';
/**
* Listen for messages from content script
*/
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'openFile') {
chrome.storage.sync.get(['kittyPath', 'opener'], result => {
const { kittyPath, opener } = result;
openFileInVim(message.file, message.line, message.column, {
kittyPath,
opener,
})
.then(result => sendResponse(result))
.catch(error => sendResponse({ success: false, error: error.message }));
});
// Keep channel open for async response
return true;
}
});
/**
* Open file in Vim via native messaging
*/
async function openFileInVim(file, line, column, settings) {
const { kittyPath, opener } = settings;
return new Promise((resolve, reject) => {
try {
const port = chrome.runtime.connectNative(NATIVE_HOST_NAME);
port.onMessage.addListener(response => {
if (response.success) {
resolve({ success: true });
} else {
resolve({ success: false, error: response.error || 'Unknown error' });
}
port.disconnect();
});
port.onDisconnect.addListener(() => {
const error = chrome.runtime.lastError;
if (error) {
console.error('[Locator Vim] Native host error:', error.message);
resolve({
success: false,
error: `Native host error: ${error.message}. Make sure the native host is installed.`,
});
}
});
port.postMessage({
action: 'openFile',
file: file,
line: line,
column: column,
kittyPath,
opener,
});
} catch (error) {
reject(error);
}
});
}
// Handle extension icon click
chrome.action.onClicked.addListener(tab => {
chrome.tabs.sendMessage(tab.id, { type: 'toggle', enabled: true });
});