From d9ff4ccbd3b42ffee3660c611bc005faec13b90f Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Mon, 8 Feb 2021 23:56:36 +0100 Subject: Sort of found working reload current tab implementation Tried using manually-incremented `*reloaded-count*` but that didn't quite work as I didn't have the right conditions to start the reload. Then tried setting up the condition such that we reload when the response comes back from the extension reload message. We can tell this when we get a `result` response that includes a `sessionId` field. To execute the reload a single time, store the most recent session ID, and compare against that. Using conditions on both the reloaded count (which needs to be changed to handle multiple copies of the same extension) and the last session ID in the message contents, we have enough to set up the reload in the tab. Added the `sleep` call back in because otherwise I got this error: reloading NOW Response: (OBJ (id . 2) (result OBJ (sessionId . 106D182E44C641B22EC65E9F6458B245))) # Response: (OBJ (id . 1) (result OBJ (result OBJ (type . object) (subtype . error) (className . TypeError) (description . TypeError: Cannot read property 'reload' of undefined at :1:16) (objectId . 8548451452974044825.19.1)) (exceptionDetails OBJ (exceptionId . 2) (text . Uncaught) (lineNumber . 0) (columnNumber . 15) (scriptId . 147) (exception OBJ (type . object) (subtype . error) (className . TypeError) (description . TypeError: Cannot read property 'reload' of undefined at :1:16) (objectId . 8548451452974044825.19.2)))) (sessionId . 106D182E44C641B22EC65E9F6458B245)) # Response: (OBJ (id . 2) (result OBJ (result OBJ (type . object) (subtype . error) (className . TypeError) (description . TypeError: Cannot read property 'reload' of undefined at :1:13) (objectId . 8548451452974044825.19.3)) (exceptionDetails OBJ (exceptionId . 3) (text . Uncaught) (lineNumber . 0) (columnNumber . 12) (scriptId . 156) (exception OBJ (type . object) (subtype . error) (className . TypeError) (description . TypeError: Cannot read property 'reload' of undefined at :1:13) (objectId . 8548451452974044825.19.4)))) (sessionId . 106D182E44C641B22EC65E9F6458B245)) That tells us that the tab reload message was sent to the extension's background page before it had a chance to fully reload. We thus need to wait until the extension is fully reloaded before being able to send the tab reload message. Don't like the sleep call here. Would be nice to have a more robust solution that didn't wait an arbitrary amount of time. Maybe we can keep sending the tab reload message until we get a response that's not an error. --- l/src/main.lisp | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) (limited to 'l/src') diff --git a/l/src/main.lisp b/l/src/main.lisp index e5688d0..411f3eb 100644 --- a/l/src/main.lisp +++ b/l/src/main.lisp @@ -2,6 +2,8 @@ (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 @@ -66,16 +68,32 @@ "sessionId"))) ;; TODO: only if config.reload-current-tab - (when reload-current-tab - (let ((current-call-id (json-obj-get response "id"))) - (when (and current-call-id - (= current-call-id - (id *devtools-root-call-id*))) +;; 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"))))) + "sessionId")))) (format t "Response: ~a~%" response) (format t "~a~%" *wg*) @@ -113,8 +131,11 @@ (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()"))) + (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` -- cgit v1.2.3