Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Catch content script not being loaded yet #429

Merged
merged 3 commits into from
Jun 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions src/code/_background.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,26 @@ function sendToDevToolsForTab(tabId, message) {
}
}

// If the content script hasn't started yet (e.g. on browser load, restoring
// many tabs), ignore an error when trying to talk to it. It'll talk to us.
//
// I tried avoiding sending to tabs whose status was not 'complete' but that
// resulted in messages not being sent even when the content script was ready.
function wrappedSendToTab(id, message) {
browser.tabs.sendMessage(id, message, () => browser.runtime.lastError)
}

function updateGUIs(tabId, url) {
if (isContentScriptablePage(url)) {
debugLog('updateGUIs(): requesting landmarks and toggle state')
browser.tabs.sendMessage(tabId, { name: 'get-landmarks' })
browser.tabs.sendMessage(tabId, { name: 'get-toggle-state' })
debugLog(`update UI for ${tabId}: requesting info`)
wrappedSendToTab(tabId, { name: 'get-landmarks' })
wrappedSendToTab(tabId, { name: 'get-toggle-state' })
} else {
debugLog('updateGUIs(): non-scriptable page')
debugLog(`update UI for ${tabId}: non-scriptable page`)
if (BROWSER === 'firefox' || BROWSER === 'opera') {
browser.runtime.sendMessage({ name: 'landmarks', data: null }, function() {
if (browser.runtime.lastError) {
// noop
}
})
browser.runtime.sendMessage(
{ name: 'landmarks', data: null }, () =>
browser.runtime.lastError) // noop
}
// DevTools panel doesn't need updating, as it maintains state
}
Expand Down Expand Up @@ -254,7 +261,7 @@ browser.webNavigation.onHistoryStateUpdated.addListener(function(details) {
if (details.frameId > 0) return
if (isContentScriptablePage(details.url)) { // TODO: check needed?
debugLog(`tab ${details.tabId} history - ${details.url}`)
browser.tabs.sendMessage(details.tabId, { name: 'trigger-refresh' })
wrappedSendToTab(details.tabId, { name: 'trigger-refresh' })
}
})

Expand Down