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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
// Package swextreload enables reloading Chrome extensions.
package swextreload
import (
"context"
"errors"
"fmt"
"log"
"strings"
"time"
"github.com/chromedp/cdproto/target"
"github.com/chromedp/chromedp"
)
// isDebug controls whether debug printing is enabled.
var isDebug = false
// SetDebugOn turns on debug printing.
func SetDebugOn() {
isDebug = true
}
// Reload reloads the extensions in extensionIDs.
func Reload(
url string,
extensionIDs []string,
shouldReloadTab bool,
) error {
var err error
allocatorContext, _ := chromedp.NewRemoteAllocator(
context.Background(),
url,
)
allocatorContext, cancel := context.WithTimeout(
allocatorContext,
5*time.Second,
)
defer cancel()
ctx, cancel := chromedp.NewContext(allocatorContext)
defer cancel()
targets, err := chromedp.Targets(ctx)
if err != nil {
return fmt.Errorf("swextreload: can't get targets: %v", err)
}
logDebugf("Targets: %#v", targets)
for _, extensionID := range extensionIDs {
err = reloadExtension(
ctx,
targets,
extensionID,
shouldReloadTab,
)
if err != nil {
return err
}
}
if shouldReloadTab {
extensionURL := "chrome-extension://" + extensionIDs[0] + "/"
var firstExtensionTarget *target.Info
for _, target := range targets {
if strings.HasPrefix(target.URL, extensionURL) {
firstExtensionTarget = target
logDebugf(
"firstExtensionTarget %s: %#v",
extensionURL,
firstExtensionTarget,
)
break
}
}
if firstExtensionTarget == nil {
// TODO: continue loop until target != null
return errors.New("swextreload: can't reload tab, no target available")
}
// In Manifest V3, we need to wait until the service worker reinstalls
// before we can re-attach to it.
if !isExtensionManifestV2(firstExtensionTarget) {
time.Sleep(200 * time.Millisecond)
}
err = reloadTab(
allocatorContext,
extensionIDs[0],
firstExtensionTarget,
)
if err != nil {
return err
}
}
return nil
}
// reloadExtension reloads the extension extensionID. If shouldReloadTab is
// true, also reload the current tab.
func reloadExtension(
ctx context.Context,
targets []*target.Info,
extensionID string,
shouldReloadTab bool,
) error {
extensionURL := "chrome-extension://" + extensionID + "/"
for _, target := range targets {
if strings.HasPrefix(target.URL, extensionURL) {
logDebugf("Target: %#v", target)
targetCtx, cancel := chromedp.NewContext(ctx, chromedp.WithTargetID(target.TargetID))
defer cancel()
logDebugf("Connected to target '%s'", target.TargetID)
var runtimeResp []byte
err := chromedp.Run(
targetCtx,
chromedp.Evaluate(`chrome.runtime.reload();`, nil),
)
if err != nil {
return fmt.Errorf(
"swextreload: error reloading extension '%s': %v",
extensionID,
err,
)
}
logDebugf("Reloaded extension")
logDebugf("Runtime: %v", string(runtimeResp))
}
}
return nil
}
func reloadTab(
ctx context.Context,
extensionID string,
reloadTarget *target.Info,
) error {
// Don't cancel the context. Otherwise, the background page DevTools
// window closes.
ctx, cancel := chromedp.NewContext(ctx)
isMV2 := isExtensionManifestV2(reloadTarget)
logDebugf("Reload tab (Manifest V2: %t)", isMV2)
// If the extension is Manifest V3, its `targetId` reset after we reloaded
// the extension from the service worker, presumably because it was
// reinstalled. In that case, we need to get targets again, find the new
// `targetId`, and connect to it.
//
// If the extension is Manifest V2, we can just reconnect to the existing
// target.
if !isMV2 {
targets, err := chromedp.Targets(ctx)
if err != nil {
return fmt.Errorf(
"swextreload: can't get targets for '%s' tab reload: %v",
extensionID,
err,
)
}
logDebugf("Targets: %#v", targets)
extensionURL := "chrome-extension://" + extensionID + "/"
var targetID target.ID
for _, target := range targets {
if strings.HasPrefix(target.URL, extensionURL) {
logDebugf("Target: %#v", target)
targetID = target.TargetID
break
}
}
ctx, cancel = chromedp.NewContext(ctx, chromedp.WithTargetID(targetID))
defer cancel()
} else {
logDebugf("Connecting to target %s", reloadTarget.TargetID)
// Don't cancel the context. Otherwise, the background page DevTools
// window closes.
ctx, _ = chromedp.NewContext(
ctx,
chromedp.WithTargetID(reloadTarget.TargetID),
)
}
var tabsResp []byte
err := chromedp.Run(
ctx,
chromedp.Evaluate(`chrome.tabs.reload();`, nil),
)
if err != nil {
return fmt.Errorf(
"swextreload: error reloading tab '%s': %v",
extensionID,
err,
)
}
logDebugf("Tabs: %v", string(tabsResp))
return nil
}
// isExtensionManifestV2 returns true if target is a Manifest V2 extension.
func isExtensionManifestV2(target *target.Info) bool {
return target.Type == "background_page"
}
// logDebugf prints a debug log if isDebug is on.
func logDebugf(format string, v ...any) {
if !isDebug {
return
}
log.Printf(format, v...)
}
|