blob: 411f3eb1fc7e866445d20e79588d5ff612f7b83a (
plain)
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
|
(in-package :extreload)
(defvar *wg* (wait-group:make-wait-group))
(defvar *devtools-root-call-id* (make-instance 'call-id))
(defvar *reloaded-count* 0)
(defvar *last-session-id* "")
(opts:define-opts
(:name :socket-url
:description "DevTools protocol WebSocket URL"
:long "socket-url"
:arg-parser #'identity
:meta-var "SOCKET_URL")
(:name :reload-current-tab
:description "pass this to reload the active Chrome tab"
:long "reload-current-tab")
(:name :help
:description "print this help menu"
:short #\h
:long "help")
(:name :version
:description "show the program version"
:short #\V
:long "version"))
(defun main ()
(handler-bind ((error
#'(lambda (e)
;; TODO: generic function for this and `handle-option-error`
(format *error-output* "error: ~a~%" e)
(opts:exit 69))))
(let ((config (parse-options)))
;; Store the WebSocket client as a global.
(defvar *client* (ws-client config))
;; TODO: error if no `socket-url`
(with-websocket-connection (*client*)
(wsd:on :message *client*
#'(lambda (message)
(ws-on-message
message
(extension-ids config)
(reload-current-tab config))))
; (wsd:on :message *client* #'(lambda (message) (ws-on-message message)))
;; TODO: Maybe defvar *config* and store client in the config
(websocket-send *client* (target-get-targets-msg
(next-call-id *devtools-root-call-id*)))
(wait-group:wait *wg*)))))
(defun ws-on-message (message extension-ids reload-current-tab)
(let* ((response (jsown:parse message))
(targets (parse-get-targets-response response)))
(if targets
(reload-extensions
(extension-targets targets)
extension-ids))
;; TODO: How to know it's the last message so we only reload the current tab once?
(if (target-attached-to-target-msg-p response)
;; TODO: Need a waitgroup:add for each occurrence of extension in extension-ids
(reload-extension (json-obj-get
(json-obj-get response "params")
"sessionId")))
;; TODO: only if config.reload-current-tab
;; If last session ID corresponds,
; Response: (OBJ (id . 2)
; (result OBJ (sessionId . C24A99CA53CBD76EB68BCBD0D172A4E7)))
(when reload-current-tab
; (when (and (= (or (json-obj-get response "id") -1) 1)
(when (and
(= *reloaded-count*
;; TODO: Might not work because there could be multiple instances of a single extension ID I think
;; Yes, extension-ids could be less than *reloaded-count*. But we could use the count of extension-targets
(length extension-ids))
(string= (json-obj-get
(json-obj-get response "result")
"sessionId")
*last-session-id*))
; (let ((current-call-id (json-obj-get response "id")))
; (when (and current-call-id
; (= current-call-id
; (id *devtools-root-call-id*)))
;
(sleep 1)
(reload-tab (json-obj-get
(json-obj-get response "result")
"sessionId"))))
(format t "Response: ~a~%" response)
(format t "~a~%" *wg*)
(wait-group:done *wg*)))
(defun json-obj-get (obj key)
(handler-case
(jsown:val obj key)
(simple-error (e)
(let ((s (format nil "~A" e)))
(if (search "not available" s)
nil)))))
;;; TODO: Rename to attach-extensions
(defun reload-extensions (targets extension-ids)
(labels ((requested-extension-p (target)
(find-if
#'(lambda (id)
(uiop:string-prefix-p
(concatenate 'string "chrome-extension://" id)
(json-obj-get target "url")))
extension-ids)))
(dolist (extension (filter #'requested-extension-p targets))
(attach-to-target extension))))
(defun attach-to-target (extension)
(let ((target-id (json-obj-get extension "targetId")))
(websocket-send *client*
(target-attach-to-target-msg
(next-call-id *devtools-root-call-id*)
target-id))))
(defun reload-extension (session-id)
;; Use call ID "1" as this is the first message sent to the attached target.
(format t "reloading EXTENSION~%")
(setf *last-session-id* session-id)
(websocket-send *client*
(runtime-evaluate-msg 1 session-id "chrome.runtime.reload()"))
(incf *reloaded-count*))
(defun reload-tab (session-id)
;; Use call ID "2" as this will always be sent after a `reload-extension`
;; message.
(format t "reloading NOW~%")
(websocket-send
*client*
(runtime-evaluate-msg 2 session-id "chrome.tabs.reload()")))
(defun extension-targets (targets)
(labels ((extensionp (target)
(string= (json-obj-get target "type")
"background_page")))
(filter #'extensionp targets)))
(defun websocket-send (client data)
(wsd:send client data)
(wait-group:add *wg*))
|