Merge pull request #6090 from electron/web-navigation-api
Add webNavigation APIs to background pages
This commit is contained in:
commit
9a22aba308
5 changed files with 84 additions and 33 deletions
|
@ -63,8 +63,10 @@
|
||||||
'lib/renderer/api/remote.js',
|
'lib/renderer/api/remote.js',
|
||||||
'lib/renderer/api/screen.js',
|
'lib/renderer/api/screen.js',
|
||||||
'lib/renderer/api/web-frame.js',
|
'lib/renderer/api/web-frame.js',
|
||||||
|
'lib/renderer/extensions/event.js',
|
||||||
'lib/renderer/extensions/i18n.js',
|
'lib/renderer/extensions/i18n.js',
|
||||||
'lib/renderer/extensions/storage.js',
|
'lib/renderer/extensions/storage.js',
|
||||||
|
'lib/renderer/extensions/web-navigation.js',
|
||||||
],
|
],
|
||||||
'js2c_sources': [
|
'js2c_sources': [
|
||||||
'lib/common/asar.js',
|
'lib/common/asar.js',
|
||||||
|
|
|
@ -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))
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,31 +1,9 @@
|
||||||
const {ipcRenderer} = require('electron')
|
const {ipcRenderer} = require('electron')
|
||||||
|
const Event = require('./extensions/event')
|
||||||
const url = require('url')
|
const url = require('url')
|
||||||
|
|
||||||
let nextId = 0
|
let nextId = 0
|
||||||
|
|
||||||
class Event {
|
|
||||||
constructor () {
|
|
||||||
this.listeners = []
|
|
||||||
}
|
|
||||||
|
|
||||||
addListener (callback) {
|
|
||||||
this.listeners.push(callback)
|
|
||||||
}
|
|
||||||
|
|
||||||
removeListener (callback) {
|
|
||||||
const index = this.listeners.indexOf(callback)
|
|
||||||
if (index !== -1) {
|
|
||||||
this.listeners.splice(index, 1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
emit (...args) {
|
|
||||||
for (const listener of this.listeners) {
|
|
||||||
listener(...args)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class Tab {
|
class Tab {
|
||||||
constructor (tabId) {
|
constructor (tabId) {
|
||||||
this.id = tabId
|
this.id = tabId
|
||||||
|
@ -183,7 +161,7 @@ exports.injectTo = function (extensionId, isBackgroundPage, context) {
|
||||||
onMessage: chrome.runtime.onMessage
|
onMessage: chrome.runtime.onMessage
|
||||||
}
|
}
|
||||||
|
|
||||||
chrome.storage = require('./extensions/storage.js')
|
chrome.storage = require('./extensions/storage')
|
||||||
|
|
||||||
chrome.pageAction = {
|
chrome.pageAction = {
|
||||||
show () {},
|
show () {},
|
||||||
|
@ -195,5 +173,6 @@ exports.injectTo = function (extensionId, isBackgroundPage, context) {
|
||||||
getPopup () {}
|
getPopup () {}
|
||||||
}
|
}
|
||||||
|
|
||||||
chrome.i18n = require('./extensions/i18n.js').setup(extensionId)
|
chrome.i18n = require('./extensions/i18n').setup(extensionId)
|
||||||
|
chrome.webNavigation = require('./extensions/web-navigation').setup()
|
||||||
}
|
}
|
||||||
|
|
24
lib/renderer/extensions/event.js
Normal file
24
lib/renderer/extensions/event.js
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
class Event {
|
||||||
|
constructor () {
|
||||||
|
this.listeners = []
|
||||||
|
}
|
||||||
|
|
||||||
|
addListener (callback) {
|
||||||
|
this.listeners.push(callback)
|
||||||
|
}
|
||||||
|
|
||||||
|
removeListener (callback) {
|
||||||
|
const index = this.listeners.indexOf(callback)
|
||||||
|
if (index !== -1) {
|
||||||
|
this.listeners.splice(index, 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
emit (...args) {
|
||||||
|
for (const listener of this.listeners) {
|
||||||
|
listener(...args)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Event
|
21
lib/renderer/extensions/web-navigation.js
Normal file
21
lib/renderer/extensions/web-navigation.js
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
const Event = require('./event')
|
||||||
|
const {ipcRenderer} = require('electron')
|
||||||
|
|
||||||
|
class WebNavigation {
|
||||||
|
constructor () {
|
||||||
|
this.onBeforeNavigate = new Event()
|
||||||
|
this.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()
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue