-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
62 lines (54 loc) · 1.69 KB
/
background.js
File metadata and controls
62 lines (54 loc) · 1.69 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
'use strict'
// port to native host process
var nativePort = null
// associates a git url to the port to the corresponding page
var contentPorts = {}
const connect = () => {
if (!nativePort) {
var hostName = 'org.hfdom.chrome_github_cloner'
console.log('connecting to native messaging host:', hostName)
nativePort = chrome.runtime.connectNative(hostName)
nativePort.onMessage.addListener(onNativeMessage)
nativePort.onDisconnect.addListener(onDisconnected)
}
}
// Message can be an object
const sendNativeMessage = (msg) => {
connect()
nativePort.postMessage(msg)
console.log('to host:', msg)
}
const onNativeMessage = (msg) => {
console.log('from host:', msg)
const contentPort = contentPorts[msg.target]
if (contentPort) {
contentPort.postMessage(msg.data)
} else {
console.error(`no contentPort for ${msg.target}`)
}
}
const onDisconnected = () => {
console.log('disconnected from host: ' + chrome.runtime.lastError.message)
nativePort = null
}
chrome.runtime.onConnect.addListener((contentPort) => {
// console.log('from sender:', contentPort.sender)
console.assert(contentPort.name === 'hello')
contentPort.onMessage.addListener(function (msg) {
console.log('from content:', msg)
const url = msg['url']
contentPorts[url] = contentPort
chrome.storage.sync.get('ideCommand', (data) => {
const ideCommand = data.ideCommand
sendNativeMessage({ url: url, ideCommand: ideCommand })
})
})
})
chrome.runtime.onInstalled.addListener(function () {
const defaultOptions = {
ideCommand: 'tmux.clj chrome-github-cloner "%s"',
}
chrome.storage.sync.set(defaultOptions, function () {
console.log('default options set')
})
})