Skip to content

Commit

Permalink
Inject on webNavigation.onCommitted()
Browse files Browse the repository at this point in the history
We inject if the following conditions are met:
  1. WDP pref is enabled
  2. Not loading a chrome:// resource
  3. Page is not loaded from bfcache (avoids double inject)
  • Loading branch information
Kevin Kuehler committed Sep 22, 2021
1 parent 5991191 commit 3e21a79
Showing 1 changed file with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ if (App !== undefined) {

// Dynamically inject WDP content script so that users with the pref disabled
// don't need to pay the loading cost on each page.
chrome.tabs.onUpdated.addListener((tabId: number, changeInfo: chrome.tabs.TabChangeInfo, tab: chrome.tabs.Tab) => {
if (tab.id) {
const urlMatches = (): boolean => {
const url = tab.url || tab.pendingUrl || ''
return url ? !/chrome:\/\//.test(url) : false
}
// We inject on `complete` since multiple `loading` events can fire.
if (APP.isRunning && (changeInfo.status === 'complete') && urlMatches()) {
chrome.tabs.executeScript(tab.id, {
file: 'bundles/web-discovery-content-script.bundle.js',
matchAboutBlank: false,
runAt: 'document_start'
}, () => { /* Deliberately left blank */ })
}
chrome.webNavigation.onCommitted.addListener((details: chrome.webNavigation.WebNavigationTransitionCallbackDetails) => {
// We skip injecting on bfcache load since it will cause us to double
// inject.
const isBfCache = details.transitionQualifiers.includes('forward_back')
if (APP.isRunning && !isBfCache && !/chrome:\/\//.test(details.url)) {
chrome.tabs.executeScript(details.tabId, {
file: 'bundles/web-discovery-content-script.bundle.js',
matchAboutBlank: false,
runAt: 'document_end'
}, () => {
if (chrome.runtime.lastError) {
console.warn('[web-discovery-project] Could not inject script:', chrome.runtime.lastError)
}
})
}
})

Expand Down

0 comments on commit 3e21a79

Please sign in to comment.