mirror of
https://github.com/electron/electron.git
synced 2026-04-10 03:01:51 -04:00
fix: chrome.action API registration (#40500)
This commit is contained in:
@@ -898,6 +898,65 @@ describe('chrome extensions', () => {
|
||||
});
|
||||
});
|
||||
|
||||
// chrome.action is not supported in Electron. These tests only ensure
|
||||
// it does not explode.
|
||||
describe('chrome.action', () => {
|
||||
let customSession: Session;
|
||||
let w = null as unknown as BrowserWindow;
|
||||
|
||||
before(async () => {
|
||||
customSession = session.fromPartition(`persist:${uuid.v4()}`);
|
||||
await customSession.loadExtension(path.join(fixtures, 'extensions', 'chrome-action-fail'));
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
w = new BrowserWindow({
|
||||
show: false,
|
||||
webPreferences: {
|
||||
session: customSession
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(closeAllWindows);
|
||||
|
||||
it('isEnabled', async () => {
|
||||
await w.loadURL(url);
|
||||
|
||||
const message = { method: 'isEnabled' };
|
||||
w.webContents.executeJavaScript(`window.postMessage('${JSON.stringify(message)}', '*')`);
|
||||
|
||||
const [, , responseString] = await once(w.webContents, 'console-message');
|
||||
|
||||
const response = JSON.parse(responseString);
|
||||
expect(response).to.equal(false);
|
||||
});
|
||||
|
||||
it('setIcon', async () => {
|
||||
await w.loadURL(url);
|
||||
|
||||
const message = { method: 'setIcon' };
|
||||
w.webContents.executeJavaScript(`window.postMessage('${JSON.stringify(message)}', '*')`);
|
||||
|
||||
const [, , responseString] = await once(w.webContents, 'console-message');
|
||||
|
||||
const response = JSON.parse(responseString);
|
||||
expect(response).to.equal(null);
|
||||
});
|
||||
|
||||
it('getBadgeText', async () => {
|
||||
await w.loadURL(url);
|
||||
|
||||
const message = { method: 'getBadgeText' };
|
||||
w.webContents.executeJavaScript(`window.postMessage('${JSON.stringify(message)}', '*')`);
|
||||
|
||||
const [, , responseString] = await once(w.webContents, 'console-message');
|
||||
|
||||
const response = JSON.parse(responseString);
|
||||
expect(response).to.equal('');
|
||||
});
|
||||
});
|
||||
|
||||
describe('chrome.tabs', () => {
|
||||
let customSession: Session;
|
||||
let w = null as unknown as BrowserWindow;
|
||||
|
||||
28
spec/fixtures/extensions/chrome-action-fail/background.js
vendored
Normal file
28
spec/fixtures/extensions/chrome-action-fail/background.js
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
/* global chrome */
|
||||
|
||||
const handleRequest = async (request, sender, sendResponse) => {
|
||||
const { method } = request;
|
||||
const tabId = sender.tab.id;
|
||||
|
||||
switch (method) {
|
||||
case 'isEnabled': {
|
||||
chrome.action.isEnabled(tabId).then(sendResponse);
|
||||
break;
|
||||
}
|
||||
|
||||
case 'setIcon': {
|
||||
chrome.action.setIcon({ tabId, imageData: {} }).then(sendResponse);
|
||||
break;
|
||||
}
|
||||
|
||||
case 'getBadgeText': {
|
||||
chrome.action.getBadgeText({ tabId }).then(sendResponse);
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
||||
handleRequest(request, sender, sendResponse);
|
||||
return true;
|
||||
});
|
||||
30
spec/fixtures/extensions/chrome-action-fail/main.js
vendored
Normal file
30
spec/fixtures/extensions/chrome-action-fail/main.js
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
/* global chrome */
|
||||
|
||||
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
||||
sendResponse(request);
|
||||
});
|
||||
|
||||
const testMap = {
|
||||
isEnabled () {
|
||||
chrome.runtime.sendMessage({ method: 'isEnabled' }, response => {
|
||||
console.log(JSON.stringify(response));
|
||||
});
|
||||
},
|
||||
setIcon () {
|
||||
chrome.runtime.sendMessage({ method: 'setIcon' }, response => {
|
||||
console.log(JSON.stringify(response));
|
||||
});
|
||||
},
|
||||
getBadgeText () {
|
||||
chrome.runtime.sendMessage({ method: 'getBadgeText' }, response => {
|
||||
console.log(JSON.stringify(response));
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const dispatchTest = (event) => {
|
||||
const { method, args = [] } = JSON.parse(event.data);
|
||||
testMap[method](...args);
|
||||
};
|
||||
|
||||
window.addEventListener('message', dispatchTest, false);
|
||||
19
spec/fixtures/extensions/chrome-action-fail/manifest.json
vendored
Normal file
19
spec/fixtures/extensions/chrome-action-fail/manifest.json
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"name": "Action popup demo",
|
||||
"version": "1.0",
|
||||
"manifest_version": 3,
|
||||
"background": {
|
||||
"service_worker": "background.js"
|
||||
},
|
||||
"content_scripts": [
|
||||
{
|
||||
"matches": ["<all_urls>"],
|
||||
"js": ["main.js"],
|
||||
"run_at": "document_start"
|
||||
}
|
||||
],
|
||||
"action": {
|
||||
"default_title": "Click Me",
|
||||
"default_popup": "popup.html"
|
||||
}
|
||||
}
|
||||
9
spec/fixtures/extensions/chrome-action-fail/popup.html
vendored
Normal file
9
spec/fixtures/extensions/chrome-action-fail/popup.html
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
<html>
|
||||
|
||||
<body>
|
||||
<script type="text/javascript" charset="utf-8">
|
||||
console.log('b');
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
Reference in New Issue
Block a user