feat: add chrome.runtime.getManifest (#16891)
* feat: add chrome.runtime.getManifest * Add test for chrome.runtime.getManifest * Use IPC utils for getManifest internals
This commit is contained in:
parent
46f818b0c3
commit
8f6a543398
5 changed files with 89 additions and 0 deletions
|
@ -169,6 +169,14 @@ ipcMainInternal.on('CHROME_RUNTIME_CONNECT', function (event, extensionId, conne
|
|||
page.webContents._sendInternalToAll(`CHROME_RUNTIME_ONCONNECT_${extensionId}`, event.sender.id, portId, connectInfo)
|
||||
})
|
||||
|
||||
ipcMainUtils.handleSync('CHROME_EXTENSION_MANIFEST', function (event, extensionId) {
|
||||
const manifest = manifestMap[extensionId]
|
||||
if (!manifest) {
|
||||
throw new Error(`Invalid extensionId: ${extensionId}`)
|
||||
}
|
||||
return manifest
|
||||
})
|
||||
|
||||
let resultID = 1
|
||||
ipcMainInternal.on('CHROME_RUNTIME_SENDMESSAGE', function (event, extensionId, message, originResultID) {
|
||||
const page = backgroundPages[extensionId]
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
'use strict'
|
||||
|
||||
const ipcRenderer = require('@electron/internal/renderer/ipc-renderer-internal')
|
||||
const ipcRendererUtils = require('@electron/internal/renderer/ipc-renderer-internal-utils')
|
||||
const Event = require('@electron/internal/renderer/extensions/event')
|
||||
const url = require('url')
|
||||
|
||||
|
@ -93,6 +94,11 @@ exports.injectTo = function (extensionId, isBackgroundPage, context) {
|
|||
})
|
||||
},
|
||||
|
||||
getManifest: function () {
|
||||
const manifest = ipcRendererUtils.invokeSync('CHROME_EXTENSION_MANIFEST', extensionId)
|
||||
return manifest
|
||||
},
|
||||
|
||||
connect (...args) {
|
||||
if (isBackgroundPage) {
|
||||
console.error('chrome.runtime.connect is not supported in background page')
|
||||
|
|
48
spec/chrome-api-spec.js
Normal file
48
spec/chrome-api-spec.js
Normal file
|
@ -0,0 +1,48 @@
|
|||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
|
||||
const { expect } = require('chai')
|
||||
const { remote } = require('electron')
|
||||
|
||||
const { closeWindow } = require('./window-helpers')
|
||||
const { emittedOnce } = require('./events-helpers')
|
||||
|
||||
const { BrowserWindow } = remote
|
||||
|
||||
describe('chrome api', () => {
|
||||
const fixtures = path.resolve(__dirname, 'fixtures')
|
||||
let w
|
||||
|
||||
before(() => {
|
||||
BrowserWindow.addExtension(path.join(fixtures, 'extensions/chrome-api'))
|
||||
})
|
||||
|
||||
after(() => {
|
||||
BrowserWindow.removeExtension('chrome-api')
|
||||
})
|
||||
|
||||
beforeEach(() => {
|
||||
w = new BrowserWindow({
|
||||
show: false
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(() => closeWindow(w).then(() => { w = null }))
|
||||
|
||||
it('runtime.getManifest returns extension manifest', async () => {
|
||||
const actualManifest = (() => {
|
||||
const data = fs.readFileSync(path.join(fixtures, 'extensions/chrome-api/manifest.json'), 'utf-8')
|
||||
return JSON.parse(data)
|
||||
})()
|
||||
|
||||
w.loadURL('about:blank')
|
||||
|
||||
const p = emittedOnce(w.webContents, 'console-message')
|
||||
w.webContents.executeJavaScript(`window.postMessage('getManifest', '*')`)
|
||||
const [,, manifestString] = await p
|
||||
const manifest = JSON.parse(manifestString)
|
||||
|
||||
expect(manifest.name).to.equal(actualManifest.name)
|
||||
expect(manifest.content_scripts.length).to.equal(actualManifest.content_scripts.length)
|
||||
})
|
||||
})
|
15
spec/fixtures/extensions/chrome-api/main.js
vendored
Normal file
15
spec/fixtures/extensions/chrome-api/main.js
vendored
Normal file
|
@ -0,0 +1,15 @@
|
|||
/* global chrome */
|
||||
|
||||
const testMap = {
|
||||
getManifest () {
|
||||
const manifest = chrome.runtime.getManifest()
|
||||
console.log(JSON.stringify(manifest))
|
||||
}
|
||||
}
|
||||
|
||||
const dispatchTest = (event) => {
|
||||
const testName = event.data
|
||||
const test = testMap[testName]
|
||||
test()
|
||||
}
|
||||
window.addEventListener('message', dispatchTest, false)
|
12
spec/fixtures/extensions/chrome-api/manifest.json
vendored
Normal file
12
spec/fixtures/extensions/chrome-api/manifest.json
vendored
Normal file
|
@ -0,0 +1,12 @@
|
|||
{
|
||||
"name": "chrome-api",
|
||||
"version": "1.0",
|
||||
"content_scripts": [
|
||||
{
|
||||
"matches": ["<all_urls>"],
|
||||
"js": ["main.js"],
|
||||
"run_at": "document_start"
|
||||
}
|
||||
],
|
||||
"manifest_version": 2
|
||||
}
|
Loading…
Reference in a new issue