Forward will/did navigate events to extensions

This commit is contained in:
Kevin Sawicki 2016-06-16 12:07:59 -07:00
parent 567622c126
commit 65abaee299
3 changed files with 52 additions and 11 deletions

View file

@ -97,17 +97,42 @@ const removeBackgroundPages = function (manifest) {
delete backgroundPages[manifest.extensionId] delete backgroundPages[manifest.extensionId]
} }
// Dispatch tabs events. const sendToBackgroundPages = function (...args) {
const hookWebContentsForTabEvents = function (webContents) {
const tabId = webContents.id
for (const page of objectValues(backgroundPages)) { for (const page of objectValues(backgroundPages)) {
page.webContents.sendToAll('CHROME_TABS_ONCREATED', tabId) page.webContents.sendToAll(...args)
} }
}
// Dispatch web contents events to Chrome APIs
const hookWebContentsEvents = function (webContents) {
const tabId = webContents.id
sendToBackgroundPages('CHROME_TABS_ONCREATED')
webContents.on('will-navigate', (event, url) => {
sendToBackgroundPages('CHROME_WEBNAVIGATION_ONBEFORENAVIGATE', {
frameId: 0,
parentFrameId: -1,
processId: webContents.getId(),
tabId: tabId,
timeStamp: Date.now(),
url: url
})
})
webContents.on('did-navigate', (event, url) => {
sendToBackgroundPages('CHROME_WEBNAVIGATION_ONCOMPLETED', {
frameId: 0,
parentFrameId: -1,
processId: webContents.getId(),
tabId: tabId,
timeStamp: Date.now(),
url: url
})
})
webContents.once('destroyed', () => { webContents.once('destroyed', () => {
for (const page of objectValues(backgroundPages)) { sendToBackgroundPages('CHROME_TABS_ONREMOVED', tabId)
page.webContents.sendToAll('CHROME_TABS_ONREMOVED', tabId)
}
}) })
} }
@ -245,7 +270,7 @@ const loadDevToolsExtensions = function (win, manifests) {
app.on('web-contents-created', function (event, webContents) { app.on('web-contents-created', function (event, webContents) {
if (!isWindowOrWebView(webContents)) return if (!isWindowOrWebView(webContents)) return
hookWebContentsForTabEvents(webContents) hookWebContentsEvents(webContents)
webContents.on('devtools-opened', function () { webContents.on('devtools-opened', function () {
loadDevToolsExtensions(webContents, objectValues(manifestMap)) loadDevToolsExtensions(webContents, objectValues(manifestMap))
}) })

View file

@ -174,5 +174,5 @@ exports.injectTo = function (extensionId, isBackgroundPage, context) {
} }
chrome.i18n = require('./extensions/i18n').setup(extensionId) chrome.i18n = require('./extensions/i18n').setup(extensionId)
chrome.webNavigation = require('./extensions/web-navigation') chrome.webNavigation = require('./extensions/web-navigation').setup()
} }

View file

@ -1,5 +1,21 @@
const Event = require('./event') const Event = require('./event')
const {ipcRenderer} = require('electron')
exports.onBeforeNavigate = new Event() class WebNavigation {
constructor () {
this.onBeforeNavigate = new Event()
this.onCompleted = new Event()
exports.onCompleted = new Event() ipcRenderer.on('CHROME_WEBNAVIGATION_ONBEFORENAVIGATE', (event, details) => {
this.onBeforeNavigate.emit(details)
})
ipcRenderer.on('CHROME_WEBNAVIGATION_ONCOMPLETED', (event, details) => {
this.onCompleted.emit(details)
})
}
}
exports.setup = () => {
return new WebNavigation()
}