Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Apr 28, 2020. It is now read-only.

Commit 862904b

Browse files
authored
Merge pull request #243 from cdr/firefox-extension
Firefox extension
2 parents 61d8d38 + f84b707 commit 862904b

14 files changed

+357
-179
lines changed

chrome.go renamed to extension.go

Lines changed: 107 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -92,59 +92,44 @@ func handleRun(w http.ResponseWriter, r *http.Request) {
9292
}
9393
}
9494

95-
type chromeExtInstall struct{}
95+
type installExtHostCmd struct{}
9696

97-
func (c *chromeExtInstall) Spec() cli.CommandSpec {
97+
func (c *installExtHostCmd) Spec() cli.CommandSpec {
9898
return cli.CommandSpec{
99-
Name: "install-for-chrome-ext",
100-
Desc: `Installs the chrome native message host manifest.
101-
This allows the sail chrome extension to manage sail.`,
99+
Name: "install-ext-host",
100+
Desc: `Installs the native message host manifest into Chrome and Firefox.
101+
This allows the sail extension to manage sail.`,
102102
}
103103
}
104104

105-
func (c *chromeExtInstall) Run(fl *flag.FlagSet) {
106-
nativeHostDirs, err := nativeMessageHostManifestDirectories()
105+
func (c *installExtHostCmd) Run(fl *flag.FlagSet) {
106+
binPath, err := os.Executable()
107107
if err != nil {
108-
flog.Fatal("failed to get native message host manifest directory: %v", err)
108+
flog.Fatal("failed to get sail binary location")
109109
}
110110

111-
for _, dir := range nativeHostDirs {
112-
if dir == "" {
113-
continue
114-
}
115-
116-
err = os.MkdirAll(dir, 0755)
117-
if err != nil {
118-
flog.Fatal("failed to ensure manifest directory exists: %v", err)
119-
}
120-
err = writeNativeHostManifest(dir)
121-
if err != nil {
122-
flog.Fatal("failed to write native messaging host manifest: %v", err)
123-
}
111+
nativeHostDirsChrome, err := nativeMessageHostManifestDirectoriesChrome()
112+
if err != nil {
113+
flog.Fatal("failed to get chrome native message host manifest directory: %v", err)
124114
}
125-
}
126-
127-
func writeNativeHostManifest(dir string) error {
128-
binPath, err := os.Executable()
115+
err = installManifests(nativeHostDirsChrome, "com.coder.sail.json", chromeManifest(binPath))
129116
if err != nil {
130-
return err
117+
flog.Fatal("failed to write chrome manifest files: %v", err)
131118
}
132119

133-
manifest := fmt.Sprintf(`{
134-
"name": "com.coder.sail",
135-
"description": "sail message host",
136-
"path": "%v",
137-
"type": "stdio",
138-
"allowed_origins": [
139-
"chrome-extension://deeepphleikpinikcbjplcgojfhkcmna/"
140-
]
141-
}`, binPath)
120+
nativeHostDirsFirefox, err := nativeMessageHostManifestDirectoriesFirefox()
121+
if err != nil {
122+
flog.Fatal("failed to get firefox native message host manifest directory: %v", err)
123+
}
124+
err = installManifests(nativeHostDirsFirefox, "com.coder.sail.json", firefoxManifest(binPath))
125+
if err != nil {
126+
flog.Fatal("failed to write firefox manifest files: %v", err)
127+
}
142128

143-
dst := path.Join(dir, "com.coder.sail.json")
144-
return ioutil.WriteFile(dst, []byte(manifest), 0644)
129+
flog.Info("Successfully installed manifests.")
145130
}
146131

147-
func nativeMessageHostManifestDirectories() ([]string, error) {
132+
func nativeMessageHostManifestDirectoriesChrome() ([]string, error) {
148133
homeDir, err := os.UserHomeDir()
149134
if err != nil {
150135
return nil, xerrors.Errorf("failed to get user home dir: %w", err)
@@ -178,3 +163,87 @@ func nativeMessageHostManifestDirectories() ([]string, error) {
178163
chromeCanaryDir,
179164
}, nil
180165
}
166+
167+
func chromeManifest(binPath string) string {
168+
return fmt.Sprintf(`{
169+
"name": "com.coder.sail",
170+
"description": "sail message host",
171+
"path": "%v",
172+
"type": "stdio",
173+
"allowed_origins": [
174+
"chrome-extension://deeepphleikpinikcbjplcgojfhkcmna/"
175+
]
176+
}`, binPath)
177+
}
178+
179+
func nativeMessageHostManifestDirectoriesFirefox() ([]string, error) {
180+
homeDir, err := os.UserHomeDir()
181+
if err != nil {
182+
return nil, xerrors.Errorf("failed to get user home dir: %w", err)
183+
}
184+
185+
var firefoxDir string
186+
187+
switch runtime.GOOS {
188+
case "linux":
189+
firefoxDir = path.Join(homeDir, ".mozilla", "native-messaging-hosts")
190+
case "darwin":
191+
firefoxDir = path.Join(homeDir, "Library", "Application Support", "Mozilla", "NativeMessagingHosts")
192+
default:
193+
return nil, xerrors.Errorf("unsupported os %q", runtime.GOOS)
194+
}
195+
196+
return []string{
197+
firefoxDir,
198+
}, nil
199+
}
200+
201+
func firefoxManifest(binPath string) string {
202+
return fmt.Sprintf(`{
203+
"name": "com.coder.sail",
204+
"description": "sail message host",
205+
"path": "%v",
206+
"type": "stdio",
207+
"allowed_extensions": [
208+
209+
]
210+
}`, binPath)
211+
}
212+
213+
func installManifests(nativeHostDirs []string, file string, content string) error {
214+
data := []byte(content)
215+
216+
for _, dir := range nativeHostDirs {
217+
if dir == "" {
218+
continue
219+
}
220+
221+
err := os.MkdirAll(dir, 0755)
222+
if err != nil {
223+
return xerrors.Errorf("failed to ensure manifest directory exists: %w", err)
224+
}
225+
226+
dst := path.Join(dir, file)
227+
err = ioutil.WriteFile(dst, data, 0644)
228+
if err != nil {
229+
return xerrors.Errorf("failed to write native messaging host manifest: %w", err)
230+
}
231+
}
232+
233+
return nil
234+
}
235+
236+
type chromeExtInstallCmd struct{
237+
cmd *installExtHostCmd
238+
}
239+
240+
func (c *chromeExtInstallCmd) Spec() cli.CommandSpec {
241+
return cli.CommandSpec{
242+
Name: "install-for-chrome-ext",
243+
Desc: "DEPRECATED: alias of install-ext-host.",
244+
}
245+
}
246+
247+
func (c *chromeExtInstallCmd) Run(fl *flag.FlagSet) {
248+
c.cmd.Run(fl)
249+
}

extension/.gitignore

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
node_modules
2-
out
3-
*.zip
1+
*.xpi
2+
*.zip
3+
node_modules/
4+
out/
5+
packed-extensions/
6+
web-ext-artifacts/

extension/manifest.json

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@
66
"author": "Coder",
77
"description": "Work in immutable, pre-configured development environments.",
88

9+
"browser_specific_settings": {
10+
"gecko": {
11+
12+
"strict_min_version": "55.0"
13+
}
14+
},
15+
916
"background": {
1017
"scripts": [
11-
"out/background.js"
18+
"background.js"
1219
],
1320
"persistent": false
1421
},
@@ -18,7 +25,7 @@
1825
"https://*/*"
1926
],
2027
"js": [
21-
"out/content.js"
28+
"content.js"
2229
]
2330
}
2431
],
@@ -28,7 +35,10 @@
2835
"storage",
2936
"tabs"
3037
],
31-
"options_page": "out/config.html",
38+
"icons": {
39+
"128": "logo128.png"
40+
},
41+
"options_page": "config.html",
3242
"icons": {
3343
"128": "logo128.png"
3444
},

extension/pack.sh

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22

3-
zip -R extension manifest.json out/* logo128.png
3+
set -e
4+
5+
cd $(dirname "$0")
6+
7+
VERSION=$(jq -r ".version" ./manifest.json)
8+
SRC_DIR="./out"
9+
OUTPUT_DIR="./packed-extensions"
10+
11+
mkdir -p "$OUTPUT_DIR"
12+
13+
# Firefox extension (done first because web-ext verifies manifest)
14+
if [ -z "$WEB_EXT_API_KEY" ]; then
15+
web-ext build --source-dir="$SRC_DIR" --artifacts-dir="$OUTPUT_DIR" --overwrite-dest
16+
mv "$OUTPUT_DIR/sail-$VERSION.zip" "$OUTPUT_DIR/sail-$VERSION.firefox.zip"
17+
else
18+
# Requires $WEB_EXT_API_KEY and $WEB_EXT_API_SECRET from addons.mozilla.org.
19+
web-ext sign --source-dir="$SRC_DIR" --artifacts-dir="$OUTPUT_DIR" --overwrite-dest
20+
mv "$OUTPUT_DIR/sail-$VERSION.xpi" "$OUTPUT_DIR/sail-$VERSION.firefox.xpi"
21+
fi
22+
23+
# Chrome extension
24+
rm "$OUTPUT_DIR/sail-$VERSION.chrome.zip" || true
25+
zip -R "$OUTPUT_DIR/sail-$VERSION.chrome.zip" "$SRC_DIR/*"

0 commit comments

Comments
 (0)