Skip to content

Commit

Permalink
fix: Ensure pages loaded in the foreground tab are monitored for chan…
Browse files Browse the repository at this point in the history
…ges (#460)

PR #428 introduced a bug whereby the MutationObserver wouldn't be wired up until the page became visible (i.e. either the user moves to it for the first time, if it was loaded in a background tab, or the user moves away from and back to it, if it was loaded in the foreground tab). This was discovered and reported by @cbou in #458.

Fixes #458
  • Loading branch information
matatk authored Nov 30, 2021
1 parent 116251d commit 9216a38
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions src/code/_content.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const observerReconnectionGrace = 2e3 // wait after page becomes visible again
let observerReconnectionScanTimer = null
let observer = null
let landmarksFinder = landmarksFinderStandard // just in case
let haveScannedForLandmarks = false


//
Expand Down Expand Up @@ -98,7 +97,6 @@ function messageHandler(message) {
findLandmarksAndSend(
// TODO: this willl send the non-mutation message twice
msr.incrementNonMutationScans, msr.sendAllUpdates)
// haveScannedForLandmarks will be set to true now anyway
break
case 'devtools-state':
if (message.state === 'open') {
Expand All @@ -113,7 +111,7 @@ function messageHandler(message) {
throw Error(`Invalid DevTools state "${message.state}".`)
}
if (!document.hidden) {
debugSend('doc visible; also scanning')
debugSend('doc visible; scanning')
findLandmarks(noop, noop)
}
break
Expand All @@ -127,8 +125,8 @@ function messageHandler(message) {

function doUpdateOutdatedResults() {
let outOfDate = false
if (observerReconnectionScanTimer !== null && !haveScannedForLandmarks) {
debugSend('out-of-date: no scan yet + waiting to observe')
if (observerReconnectionScanTimer !== null) {
debugSend('out-of-date: was awaiting reconnection; re-observing now')
cancelObserverReconnectionScan()
observeMutations()
outOfDate = true
Expand Down Expand Up @@ -185,7 +183,6 @@ function findLandmarks(counterIncrementFunction, updateSendFunction) {

const start = performance.now()
landmarksFinder.find()
if (!haveScannedForLandmarks) haveScannedForLandmarks = true
msr.setLastScanDuration(performance.now() - start)

counterIncrementFunction()
Expand Down Expand Up @@ -273,7 +270,6 @@ function createMutationObserver() {
}

function observeMutations() {
debugSend('observing mutations')
observer.observe(document, {
attributes: true,
childList: true,
Expand All @@ -286,7 +282,6 @@ function observeMutations() {

function cancelObserverReconnectionScan() {
if (observerReconnectionScanTimer) {
debugSend('cancelling scheduled observing and scan')
clearTimeout(observerReconnectionScanTimer)
observerReconnectionScanTimer = null
}
Expand All @@ -296,12 +291,10 @@ function reflectPageVisibility() {
debugSend((document.hidden ? 'hidden' : 'shown') + ' ' + window.location)
if (document.hidden) {
cancelObserverReconnectionScan()
debugSend('disconnecting from observer')
observer.disconnect()
} else {
debugSend('starting reconnection timer')
observerReconnectionScanTimer = setTimeout(function() {
debugSend('performing scheduled observing and scan')
debugSend('page remained visible: observing and scanning')
findLandmarksAndSend(
msr.incrementNonMutationScans, noop) // it will send anyway
observeMutations()
Expand Down Expand Up @@ -345,9 +338,14 @@ function startUpTasks() {
})

createMutationObserver()
if (!document.hidden) {
debugSend('document visible at startup; observing')
observeMutations()
}

// Requesting the DevTools' state will eventually cause the correct
// scanner to be set, and the document to be scanned, if visible.
// Requesting the DevTools' state will eventually cause the correct scanner
// to be set, the observer to be hooked up, and the document to be scanned,
// if visible.
browser.runtime.sendMessage({ name: 'get-devtools-state' })
}

Expand Down

0 comments on commit 9216a38

Please sign in to comment.