fix: chrome.action API registration (#40500)

This commit is contained in:
Shelley Vohr
2023-11-13 08:27:18 +01:00
committed by GitHub
parent cf5f0419f1
commit 7981d955b8
11 changed files with 536 additions and 1 deletions

View File

@@ -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;

View 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;
});

View 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);

View 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"
}
}

View File

@@ -0,0 +1,9 @@
<html>
<body>
<script type="text/javascript" charset="utf-8">
console.log('b');
</script>
</body>
</html>