@@ -28,34 +28,54 @@ import { localStorageInfo, sessionStorageInfo } from 'lo_event/lo_event/metadata
2828// } /* and other async logic */);
2929// websocketLogger( callback );
3030
31+ // Track which tabs currently have an active content script and the state of
32+ // our logger so that we only initialize logging when needed.
33+ const activeContentTabs = new Set ( ) ;
34+ let loEventActive = false ;
35+ let loggers = [ ] ;
36+ const manifestVersion = chrome . runtime . getManifest ( ) . version ;
37+
38+
3139// We are not sure if this should be done within `websocketLogger()`'s `init`
3240// or one level up.
33- const loggers = [
34- consoleLogger ( ) ,
35- websocketLogger ( WEBSOCKET_SERVER_URL )
36- ]
37-
38- loEvent . init (
39- 'org.mitros.writing_analytics' ,
40- '0.01' ,
41- loggers ,
42- {
43- debugLevel : loEventDebug . LEVEL . SIMPLE ,
44- metadata : [
45- browserInfo ( ) ,
46- chromeAuth ( ) ,
47- localStorageInfo ( ) ,
48- sessionStorageInfo ( ) ,
49- ]
50- }
51- ) ;
52- loEvent . go ( ) ;
41+ function startLogger ( ) {
42+ if ( loEventActive ) return ;
43+ loggers = [
44+ consoleLogger ( ) ,
45+ websocketLogger ( WEBSOCKET_SERVER_URL )
46+ ] ;
47+ loEvent . init (
48+ 'org.mitros.writing_analytics' ,
49+ manifestVersion ,
50+ loggers ,
51+ {
52+ debugLevel : loEventDebug . LEVEL . SIMPLE ,
53+ // TODO document what we have currently and what we want
54+ metadata : [
55+ browserInfo ( ) ,
56+ chromeAuth ( ) ,
57+ localStorageInfo ( ) ,
58+ sessionStorageInfo ( ) ,
59+ ]
60+ }
61+ ) ;
62+ loEvent . go ( ) ;
63+ loEventActive = true ;
64+ loEvent . logEvent ( 'extension_loaded' , { } ) ;
65+ logFromServiceWorker ( 'Extension loaded' ) ;
66+ }
67+
68+ function stopLogger ( ) {
69+ if ( ! loEventActive ) return ;
70+ loEvent . logEvent ( 'terminate' , { } ) ;
71+ loEventActive = false ;
72+ }
5373
5474// Function to serve as replacement for
5575// chrome.extension.getBackgroundPage().console.log(event); because it is not allowed in V3
5676// It logs the event to the console for debugging.
5777function logFromServiceWorker ( event ) {
58- console . log ( event ) ;
78+ console . log ( event ) ;
5979}
6080
6181function this_a_google_docs_save ( request ) {
@@ -69,7 +89,7 @@ function this_a_google_docs_save(request) {
6989 went from a conservative regexp to a liberal one. We should
7090 confirm this never catches extra requests, though.
7191 */
72- if ( request . url . match ( / .* : \/ \/ d o c s \. g o o g l e \. c o m \/ d o c u m e n t \/ ( .* ) \/ s a v e / i) ) {
92+ if ( request . url . match ( / .* : \/ \/ d o c s \. g o o g l e \. c o m \/ d o c u m e n t \/ ( .* ) \/ s a v e / i) ) {
7393 return true ;
7494 }
7595 return false ;
@@ -85,7 +105,7 @@ function this_a_google_docs_bind(request) {
85105
86106 https://stackoverflow.com/questions/6831916/is-it-possible-to-monitor-http-traffic-in-chrome-using-an-extension#6832018
87107 */
88- if ( request . url . match ( / .* : \/ \/ d o c s \. g o o g l e \. c o m \/ d o c u m e n t \/ ( .* ) \/ b i n d / i) ) {
108+ if ( request . url . match ( / .* : \/ \/ d o c s \. g o o g l e \. c o m \/ d o c u m e n t \/ ( .* ) \/ b i n d / i) ) {
89109 return true ;
90110 }
91111 return false ;
@@ -106,10 +126,29 @@ chrome.storage.sync.get(['process_server'], function(result) {
106126
107127// Listen for the keystroke messages from the page script and forward to the server.
108128chrome . runtime . onMessage . addListener (
109- function ( request , sender , sendResponse ) {
110- //chrome.extension.getBackgroundPage().console.log("Got message");
111- //chrome.extension.getBackgroundPage().console.log(request);
112- //console.log(sender);
129+ function ( request , sender , sendResponse ) {
130+ // Lifecycle messages from content scripts manage the logger state
131+ if ( request ?. type === 'content_script_ready' ) {
132+ if ( sender . tab ?. id !== undefined ) {
133+ activeContentTabs . add ( sender . tab . id ) ;
134+ if ( ! loEventActive ) {
135+ startLogger ( ) ;
136+ }
137+ }
138+ return ;
139+ } else if ( request ?. type === 'content_script_unloading' ) {
140+ if ( sender . tab ?. id !== undefined ) {
141+ activeContentTabs . delete ( sender . tab . id ) ;
142+ if ( activeContentTabs . size === 0 ) {
143+ stopLogger ( ) ;
144+ }
145+ }
146+ return ;
147+ }
148+ // Forward analytics events only when the logger is active
149+ if ( ! loEventActive ) {
150+ return ;
151+ }
113152 request [ 'wa_source' ] = 'client_page' ;
114153 loEvent . logEvent ( request [ 'event' ] , request ) ;
115154 }
@@ -142,55 +181,59 @@ chrome.webRequest.onBeforeRequest.addListener(
142181 especially new pages. They do inject a lot of noise, though, and
143182 from there, being able to easily ignore these is nice.
144183 */
145- function ( request ) {
184+ function ( request ) {
185+ // No logger availaber
186+ if ( ! loEventActive ) {
187+ return ;
188+ }
146189 //chrome.extension.getBackgroundPage().console.log("Web request url:"+request.url);
147190 var formdata = { } ;
148191 let event ;
149- if ( request . requestBody ) {
192+ if ( request . requestBody ) {
150193 formdata = request . requestBody . formData ;
151194 }
152- if ( ! formdata ) {
195+ if ( ! formdata ) {
153196 formdata = { } ;
154197 }
155- if ( RAW_DEBUG ) {
198+ if ( RAW_DEBUG ) {
156199 loEvent . logEvent ( 'raw_http_request' , {
157- 'url' : request . url ,
200+ 'url' : request . url ,
158201 'form_data' : formdata
159202 } ) ;
160203 }
161204
162- if ( this_a_google_docs_save ( request ) ) {
205+ if ( this_a_google_docs_save ( request ) ) {
163206 //chrome.extension.getBackgroundPage().console.log("Google Docs bundles "+request.url);
164207 try {
165208 /* We should think through which time stamps we should log. These are all subtly
166209 different: browser event versus request timestamp, as well as user time zone
167210 versus GMT. */
168211 event = {
169- 'doc_id' : googledocs_id_from_url ( request . url ) ,
212+ 'doc_id' : googledocs_id_from_url ( request . url ) ,
170213 'url' : request . url ,
171214 'bundles' : JSON . parse ( formdata . bundles ) ,
172215 'rev' : formdata . rev ,
173216 'timestamp' : parseInt ( request . timeStamp , 10 )
174217 } ;
175218 logFromServiceWorker ( event ) ;
176219 loEvent . logEvent ( 'google_docs_save' , event ) ;
177- } catch ( err ) {
220+ } catch ( err ) {
178221 /*
179222 Oddball events, like text selections.
180223 */
181224 event = {
182- 'doc_id' : googledocs_id_from_url ( request . url ) ,
225+ 'doc_id' : googledocs_id_from_url ( request . url ) ,
183226 'url' : request . url ,
184227 'formdata' : formdata ,
185228 'rev' : formdata . rev ,
186229 'timestamp' : parseInt ( request . timeStamp , 10 )
187230 } ;
188231 loEvent . logEvent ( 'google_docs_save_extra' , event ) ;
189232 }
190- } else if ( this_a_google_docs_bind ( request ) ) {
233+ } else if ( this_a_google_docs_bind ( request ) ) {
191234 logFromServiceWorker ( request ) ;
192235 } else {
193- logFromServiceWorker ( "Not a save or bind: " + request . url ) ;
236+ logFromServiceWorker ( "Not a save or bind: " + request . url ) ;
194237 }
195238 } ,
196239 { urls : [ "*://docs.google.com/*" ] } ,
@@ -202,10 +245,10 @@ chrome.webRequest.onBeforeRequest.addListener(
202245chrome . runtime . onInstalled . addListener ( reinjectContentScripts ) ;
203246async function reinjectContentScripts ( ) {
204247 for ( const contentScript of chrome . runtime . getManifest ( ) . content_scripts ) {
205- for ( const tab of await chrome . tabs . query ( { url : contentScript . matches } ) ) {
248+ for ( const tab of await chrome . tabs . query ( { url : contentScript . matches } ) ) {
206249 // re-inject content script
207250 await chrome . scripting . executeScript ( {
208- target : { tabId : tab . id , allFrames : true } ,
251+ target : { tabId : tab . id , allFrames : true } ,
209252 files : contentScript . js ,
210253 } , function ( ) {
211254 if ( ! chrome . runtime . lastError ) {
@@ -215,10 +258,3 @@ async function reinjectContentScripts() {
215258 }
216259 }
217260}
218-
219- // Let the server know we've loaded.
220- loEvent . logEvent ( "extension_loaded" , { } ) ;
221-
222- // And let the console know we've loaded
223- // chrome.extension.getBackgroundPage().console.log("Loaded"); remove
224- logFromServiceWorker ( "Loaded" ) ;
0 commit comments