Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix captureVisibleTab function #10

Merged
merged 1 commit into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions modules/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -997,12 +997,21 @@ export const warmupTab = async tabId => {
*/
export const captureVisibleTab = async (windowId, opt) => {
const info = await runtime.getBrowserInfo();
const version = parseFloat(info.version);
const isGranted = (version >= 126 && await isPermissionGranted({
permissions: ['activeTab']
})) || await isPermissionGranted({
permissions: ['<all_urls>']
});
let isGranted;
if (info.vender === 'Mozilla') {
const browserVersion = parseFloat(info.version);
isGranted = (browserVersion >= 126 && await isPermissionGranted({
permissions: ['activeTab']
})) || await isPermissionGranted({
origins: ['<all_urls>']
});
} else {
isGranted = await isPermissionGranted({
permissions: ['activeTab']
}) || await isPermissionGranted({
origins: ['<all_urls>']
})
}
let url;
if (isGranted) {
if (!Number.isInteger(windowId)) {
Expand Down
79 changes: 60 additions & 19 deletions test/browser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2546,21 +2546,11 @@ describe('browser', () => {

it('should not call function if permission is not granted', async () => {
browser.runtime.getBrowserInfo.resolves({
version: '125.0.1'
});
browser.permissions.contains.resolves(false);
const i = browser.tabs.captureVisibleTab.callCount;
await func();
assert.strictEqual(browser.tabs.captureVisibleTab.callCount, i,
'not called');
});

it('should not call function if permission is not granted', async () => {
browser.runtime.getBrowserInfo.resolves({
vender: 'Mozilla',
version: '125.0.1'
});
browser.permissions.contains.withArgs({
permissions: ['<all_urls>']
origins: ['<all_urls>']
}).resolves(false);
const i = browser.tabs.captureVisibleTab.callCount;
await func();
Expand All @@ -2570,13 +2560,11 @@ describe('browser', () => {

it('should call function', async () => {
browser.runtime.getBrowserInfo.resolves({
vender: 'Mozilla',
version: '125.0.1'
});
browser.permissions.contains.withArgs({
permissions: ['activeTab']
}).resolves(false);
browser.permissions.contains.withArgs({
permissions: ['<all_urls>']
origins: ['<all_urls>']
}).resolves(true);
const i = browser.tabs.captureVisibleTab.callCount;
const windowId = browser.windows.WINDOW_ID_CURRENT;
Expand All @@ -2590,15 +2578,33 @@ describe('browser', () => {
assert.strictEqual(res, url, 'result');
});

it('should not call function if permission is not granted', async () => {
browser.runtime.getBrowserInfo.resolves({
vender: 'Mozilla',
version: '126.0.a1'
});
browser.permissions.contains.withArgs({
permissions: ['activeTab']
}).resolves(false);
browser.permissions.contains.withArgs({
origins: ['<all_urls>']
}).resolves(false);
const i = browser.tabs.captureVisibleTab.callCount;
await func();
assert.strictEqual(browser.tabs.captureVisibleTab.callCount, i,
'not called');
});

it('should call function', async () => {
browser.runtime.getBrowserInfo.resolves({
vender: 'Mozilla',
version: '126.0a1'
});
browser.permissions.contains.withArgs({
permissions: ['activeTab']
}).resolves(false);
browser.permissions.contains.withArgs({
permissions: ['<all_urls>']
origins: ['<all_urls>']
}).resolves(true);
const i = browser.tabs.captureVisibleTab.callCount;
const windowId = browser.windows.WINDOW_ID_CURRENT;
Expand All @@ -2614,11 +2620,15 @@ describe('browser', () => {

it('should call function', async () => {
browser.runtime.getBrowserInfo.resolves({
vender: 'Mozilla',
version: '126.0a1'
});
browser.permissions.contains.withArgs({
permissions: ['activeTab']
}).resolves(true);
browser.permissions.contains.withArgs({
origins: ['<all_urls>']
}).resolves(false);
const i = browser.tabs.captureVisibleTab.callCount;
const windowId = browser.windows.WINDOW_ID_CURRENT;
const url = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='
Expand All @@ -2631,10 +2641,34 @@ describe('browser', () => {
assert.strictEqual(res, url, 'result');
});

it('should not call function if permission is not granted', async () => {
browser.runtime.getBrowserInfo.resolves({
vender: 'Foo',
version: '126.0.1'
});
browser.permissions.contains.withArgs({
permissions: ['activeTab']
}).resolves(false);
browser.permissions.contains.withArgs({
origins: ['<all_urls>']
}).resolves(false);
const i = browser.tabs.captureVisibleTab.callCount;
await func();
assert.strictEqual(browser.tabs.captureVisibleTab.callCount, i,
'not called');
});

it('should call function', async () => {
browser.runtime.getBrowserInfo.resolves({
version: '126.0a1'
vender: 'Foo',
version: '126.0.1'
});
browser.permissions.contains.withArgs({
permissions: ['activeTab']
}).resolves(true);
browser.permissions.contains.withArgs({
origins: ['<all_urls>']
}).resolves(false);
const i = browser.tabs.captureVisibleTab.callCount;
const windowId = browser.windows.WINDOW_ID_CURRENT;
const url = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='
Expand All @@ -2649,8 +2683,15 @@ describe('browser', () => {

it('should call function', async () => {
browser.runtime.getBrowserInfo.resolves({
version: '126.0a1'
vender: 'Foo',
version: '126.0.1'
});
browser.permissions.contains.withArgs({
permissions: ['activeTab']
}).resolves(false);
browser.permissions.contains.withArgs({
origins: ['<all_urls>']
}).resolves(true);
const i = browser.tabs.captureVisibleTab.callCount;
const windowId = browser.windows.WINDOW_ID_CURRENT;
const url = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='
Expand Down