mirror of
https://github.com/electron/electron.git
synced 2026-04-10 03:01:51 -04:00
fix: update chrome.tabs for Manifest v3 (#39360)
fix: update chrome.tabs for Manifest v3 Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
This commit is contained in:
@@ -354,7 +354,7 @@ describe('chrome extensions', () => {
|
||||
const message = { method: 'executeScript', args: ['1 + 2'] };
|
||||
w.webContents.executeJavaScript(`window.postMessage('${JSON.stringify(message)}', '*')`);
|
||||
|
||||
const [,, responseString] = await once(w.webContents, 'console-message');
|
||||
const [, , responseString] = await once(w.webContents, 'console-message');
|
||||
const response = JSON.parse(responseString);
|
||||
|
||||
expect(response).to.equal(3);
|
||||
@@ -816,5 +816,121 @@ describe('chrome extensions', () => {
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('chrome.tabs', () => {
|
||||
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', 'tabs-api-async'));
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
w = new BrowserWindow({
|
||||
show: false,
|
||||
webPreferences: {
|
||||
session: customSession,
|
||||
nodeIntegration: true
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(closeAllWindows);
|
||||
|
||||
it('getZoom', async () => {
|
||||
await w.loadURL(url);
|
||||
|
||||
const message = { method: 'getZoom' };
|
||||
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(1);
|
||||
});
|
||||
|
||||
it('setZoom', async () => {
|
||||
await w.loadURL(url);
|
||||
|
||||
const message = { method: 'setZoom', args: [2] };
|
||||
w.webContents.executeJavaScript(`window.postMessage('${JSON.stringify(message)}', '*')`);
|
||||
|
||||
const [,, responseString] = await once(w.webContents, 'console-message');
|
||||
|
||||
const response = JSON.parse(responseString);
|
||||
expect(response).to.deep.equal(2);
|
||||
});
|
||||
|
||||
it('getZoomSettings', async () => {
|
||||
await w.loadURL(url);
|
||||
|
||||
const message = { method: 'getZoomSettings' };
|
||||
w.webContents.executeJavaScript(`window.postMessage('${JSON.stringify(message)}', '*')`);
|
||||
|
||||
const [,, responseString] = await once(w.webContents, 'console-message');
|
||||
|
||||
const response = JSON.parse(responseString);
|
||||
expect(response).to.deep.equal({
|
||||
defaultZoomFactor: 1,
|
||||
mode: 'automatic',
|
||||
scope: 'per-origin'
|
||||
});
|
||||
});
|
||||
|
||||
it('setZoomSettings', async () => {
|
||||
await w.loadURL(url);
|
||||
|
||||
const message = { method: 'setZoomSettings', args: [{ mode: 'disabled' }] };
|
||||
w.webContents.executeJavaScript(`window.postMessage('${JSON.stringify(message)}', '*')`);
|
||||
|
||||
const [,, responseString] = await once(w.webContents, 'console-message');
|
||||
|
||||
const response = JSON.parse(responseString);
|
||||
expect(response).to.deep.equal({
|
||||
defaultZoomFactor: 1,
|
||||
mode: 'disabled',
|
||||
scope: 'per-tab'
|
||||
});
|
||||
});
|
||||
|
||||
it('get', async () => {
|
||||
await w.loadURL(url);
|
||||
|
||||
const message = { method: 'get' };
|
||||
w.webContents.executeJavaScript(`window.postMessage('${JSON.stringify(message)}', '*')`);
|
||||
|
||||
const [,, responseString] = await once(w.webContents, 'console-message');
|
||||
|
||||
const response = JSON.parse(responseString);
|
||||
expect(response).to.have.property('active').that.is.a('boolean');
|
||||
expect(response).to.have.property('autoDiscardable').that.is.a('boolean');
|
||||
expect(response).to.have.property('discarded').that.is.a('boolean');
|
||||
expect(response).to.have.property('groupId').that.is.a('number');
|
||||
expect(response).to.have.property('highlighted').that.is.a('boolean');
|
||||
expect(response).to.have.property('id').that.is.a('number');
|
||||
expect(response).to.have.property('incognito').that.is.a('boolean');
|
||||
expect(response).to.have.property('index').that.is.a('number');
|
||||
expect(response).to.have.property('pinned').that.is.a('boolean');
|
||||
expect(response).to.have.property('selected').that.is.a('boolean');
|
||||
expect(response).to.have.property('url').that.is.a('string');
|
||||
expect(response).to.have.property('windowId').that.is.a('number');
|
||||
});
|
||||
|
||||
it('reload', async () => {
|
||||
await w.loadURL(url);
|
||||
|
||||
const message = { method: 'reload' };
|
||||
w.webContents.executeJavaScript(`window.postMessage('${JSON.stringify(message)}', '*')`);
|
||||
|
||||
const consoleMessage = once(w.webContents, 'console-message');
|
||||
const finish = once(w.webContents, 'did-finish-load');
|
||||
|
||||
await Promise.all([consoleMessage, finish]).then(([[,, responseString]]) => {
|
||||
const response = JSON.parse(responseString);
|
||||
expect(response.status).to.equal('reloaded');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
1
spec/fixtures/extensions/chrome-api/main.js
vendored
1
spec/fixtures/extensions/chrome-api/main.js
vendored
@@ -49,4 +49,5 @@ const dispatchTest = (event) => {
|
||||
const { method, args = [] } = JSON.parse(event.data);
|
||||
testMap[method](...args);
|
||||
};
|
||||
|
||||
window.addEventListener('message', dispatchTest, false);
|
||||
|
||||
52
spec/fixtures/extensions/tabs-api-async/background.js
vendored
Normal file
52
spec/fixtures/extensions/tabs-api-async/background.js
vendored
Normal file
@@ -0,0 +1,52 @@
|
||||
/* global chrome */
|
||||
|
||||
const handleRequest = (request, sender, sendResponse) => {
|
||||
const { method, args = [] } = request;
|
||||
const tabId = sender.tab.id;
|
||||
|
||||
switch (method) {
|
||||
case 'getZoom': {
|
||||
chrome.tabs.getZoom(tabId).then(sendResponse);
|
||||
break;
|
||||
}
|
||||
|
||||
case 'setZoom': {
|
||||
const [zoom] = args;
|
||||
chrome.tabs.setZoom(tabId, zoom).then(async () => {
|
||||
const updatedZoom = await chrome.tabs.getZoom(tabId);
|
||||
sendResponse(updatedZoom);
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
case 'getZoomSettings': {
|
||||
chrome.tabs.getZoomSettings(tabId).then(sendResponse);
|
||||
break;
|
||||
}
|
||||
|
||||
case 'setZoomSettings': {
|
||||
const [settings] = args;
|
||||
chrome.tabs.setZoomSettings(tabId, { mode: settings.mode }).then(async () => {
|
||||
const zoomSettings = await chrome.tabs.getZoomSettings(tabId);
|
||||
sendResponse(zoomSettings);
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
case 'get': {
|
||||
chrome.tabs.get(tabId).then(sendResponse);
|
||||
break;
|
||||
}
|
||||
|
||||
case 'reload': {
|
||||
chrome.tabs.reload(tabId).then(() => {
|
||||
sendResponse({ status: 'reloaded' });
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
||||
handleRequest(request, sender, sendResponse);
|
||||
return true;
|
||||
});
|
||||
45
spec/fixtures/extensions/tabs-api-async/main.js
vendored
Normal file
45
spec/fixtures/extensions/tabs-api-async/main.js
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
/* global chrome */
|
||||
|
||||
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
||||
sendResponse(request);
|
||||
});
|
||||
|
||||
const testMap = {
|
||||
getZoomSettings () {
|
||||
chrome.runtime.sendMessage({ method: 'getZoomSettings' }, response => {
|
||||
console.log(JSON.stringify(response));
|
||||
});
|
||||
},
|
||||
setZoomSettings (settings) {
|
||||
chrome.runtime.sendMessage({ method: 'setZoomSettings', args: [settings] }, response => {
|
||||
console.log(JSON.stringify(response));
|
||||
});
|
||||
},
|
||||
getZoom () {
|
||||
chrome.runtime.sendMessage({ method: 'getZoom', args: [] }, response => {
|
||||
console.log(JSON.stringify(response));
|
||||
});
|
||||
},
|
||||
setZoom (zoom) {
|
||||
chrome.runtime.sendMessage({ method: 'setZoom', args: [zoom] }, response => {
|
||||
console.log(JSON.stringify(response));
|
||||
});
|
||||
},
|
||||
get () {
|
||||
chrome.runtime.sendMessage({ method: 'get' }, response => {
|
||||
console.log(JSON.stringify(response));
|
||||
});
|
||||
},
|
||||
reload () {
|
||||
chrome.runtime.sendMessage({ method: 'reload' }, response => {
|
||||
console.log(JSON.stringify(response));
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const dispatchTest = (event) => {
|
||||
const { method, args = [] } = JSON.parse(event.data);
|
||||
testMap[method](...args);
|
||||
};
|
||||
|
||||
window.addEventListener('message', dispatchTest, false);
|
||||
15
spec/fixtures/extensions/tabs-api-async/manifest.json
vendored
Normal file
15
spec/fixtures/extensions/tabs-api-async/manifest.json
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "tabs-api-async",
|
||||
"version": "1.0",
|
||||
"content_scripts": [
|
||||
{
|
||||
"matches": [ "<all_urls>"],
|
||||
"js": ["main.js"],
|
||||
"run_at": "document_start"
|
||||
}
|
||||
],
|
||||
"background": {
|
||||
"service_worker": "background.js"
|
||||
},
|
||||
"manifest_version": 3
|
||||
}
|
||||
Reference in New Issue
Block a user