Skip to content

Commit

Permalink
fix: remove access of chrome via window
Browse files Browse the repository at this point in the history
  • Loading branch information
dylandepass committed Dec 18, 2023
1 parent 771e59f commit 3cc284d
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 31 deletions.
6 changes: 3 additions & 3 deletions src/extension/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ import {
} from './utils.js';
import checkTab from './check-tab.js';

window.chrome.action.onClicked.addListener(async ({ id }) => {
chrome.action.onClicked.addListener(async ({ id }) => {
// toggle the sidekick when the action is clicked
await toggleDisplay();
checkTab(id);
});

window.chrome.tabs.onUpdated.addListener(async (id, info) => {
chrome.tabs.onUpdated.addListener(async (id, info) => {
if (info.status === 'complete') {
checkTab(id);
}
});

window.chrome.tabs.onActivated.addListener(({ tabId: id }) => {
chrome.tabs.onActivated.addListener(({ tabId: id }) => {
checkTab(id);
});

Expand Down
2 changes: 1 addition & 1 deletion src/extension/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
let { sidekick } = window.hlx;
if (!sidekick) {
// wait for config matches
window.chrome.runtime.onMessage.addListener(async ({ configMatches = [] }, { tab }) => {
chrome.runtime.onMessage.addListener(async ({ configMatches = [] }, { tab }) => {
// only accept message from background script
if (tab) {
return;
Expand Down
12 changes: 6 additions & 6 deletions src/extension/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,24 +174,24 @@ export async function addProject(input) {
const env = await getProjectEnv(config);
if (env.unauthorized && !input.authToken) {
// defer adding project and have user sign in
const { id: loginTabId } = await window.chrome.tabs.create({
url: `https://admin.hlx.page/login/${owner}/${repo}/${ref}?extensionId=${window.chrome.runtime.id}`,
const { id: loginTabId } = await chrome.tabs.create({
url: `https://admin.hlx.page/login/${owner}/${repo}/${ref}?extensionId=${chrome.runtime.id}`,
active: true,
});
return new Promise((resolve) => {
// retry adding project after sign in
const retryAddProjectListener = async (message = {}) => {
let added = false;
if (message.authToken && owner === message.owner && repo === message.repo) {
await window.chrome.tabs.remove(loginTabId);
await chrome.tabs.remove(loginTabId);
config.authToken = message.authToken;
added = await addProject(config);
}
// clean up
window.chrome.runtime.onMessageExternal.removeListener(retryAddProjectListener);
chrome.runtime.onMessageExternal.removeListener(retryAddProjectListener);
resolve(added);
};
window.chrome.runtime.onMessageExternal.addListener(retryAddProjectListener);
chrome.runtime.onMessageExternal.addListener(retryAddProjectListener);
});
}
let project = await getProject(config);
Expand Down Expand Up @@ -227,7 +227,7 @@ export async function deleteProject(project) {
const i = projects.indexOf(handle);
if (i >= 0) {
// delete admin auth header rule
window.chrome.runtime.sendMessage({ deleteAuthToken: { owner, repo } });
chrome.runtime.sendMessage({ deleteAuthToken: { owner, repo } });
// delete the project entry
await removeConfig('sync', handle);
// remove project entry from index
Expand Down
1 change: 0 additions & 1 deletion src/extension/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@ export {};
declare global {
interface Window {
hlx: any;
chrome: any;
}
}
10 changes: 5 additions & 5 deletions src/extension/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const GH_URL = 'https://github.com/';
*/
export async function loadScript(path) {
return new Promise((resolve) => {
const src = window.chrome.runtime.getURL(path);
const src = chrome.runtime.getURL(path);
if (!document.querySelector(`script[src="${src}"]`)) {
const script = document.createElement('script');
script.type = 'module';
Expand Down Expand Up @@ -117,7 +117,7 @@ export async function getConfigMatches(configs, tabUrl) {
* @returns {Promise<*>} The configuration
*/
export async function getConfig(area, prop) {
const cfg = await window.chrome.storage[area].get(prop);
const cfg = await chrome.storage[area].get(prop);
return cfg?.[prop];
}

Expand All @@ -128,7 +128,7 @@ export async function getConfig(area, prop) {
* @returns {Promise<void>}
*/
export async function setConfig(area, obj) {
return window.chrome.storage[area].set(obj);
return chrome.storage[area].set(obj);
}

/**
Expand All @@ -138,7 +138,7 @@ export async function setConfig(area, obj) {
* @returns {Promise<void>}
*/
export async function removeConfig(area, prop) {
return window.chrome.storage[area].remove(prop);
return chrome.storage[area].remove(prop);
}

/**
Expand All @@ -147,7 +147,7 @@ export async function removeConfig(area, prop) {
* @returns {Promise<void>}
*/
export async function clearConfig(area) {
return window.chrome.storage[area].clear();
return chrome.storage[area].clear();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/wtr/check-tab.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import sinon from 'sinon';
import chromeMock from './mocks/chrome.js';
import checkTab from '../../src/extension/check-tab.js';

window.chrome = chromeMock;
chrome = chromeMock;

describe('Test checkTab', () => {
const sandbox = sinon.createSandbox();
Expand Down
12 changes: 6 additions & 6 deletions test/wtr/project.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
deleteProject,
} from '../../src/extension/project.js';

window.chrome = chromeMock;
chrome = chromeMock;
window.fetch = fetchMock;

describe('Test utils', () => {
Expand All @@ -42,7 +42,7 @@ describe('Test utils', () => {
});

it('getProject', async () => {
const spy = sandbox.spy(window.chrome.storage.sync, 'get');
const spy = sandbox.spy(chrome.storage.sync, 'get');
// get project without handle
let project = await getProject();
expect(project).to.be.undefined;
Expand All @@ -57,7 +57,7 @@ describe('Test utils', () => {
}).timeout(5000);

it('getProjects', async () => {
const spy = sandbox.spy(window.chrome.storage.sync, 'get');
const spy = sandbox.spy(chrome.storage.sync, 'get');
await getProjects();
expect(spy.calledWith('hlxSidekickProjects')).to.be.true;
expect(spy.calledWith('adobe/blog')).to.be.true;
Expand Down Expand Up @@ -102,7 +102,7 @@ describe('Test utils', () => {
});

it('addProject', async () => {
const spy = sandbox.spy(window.chrome.storage.sync, 'set');
const spy = sandbox.spy(chrome.storage.sync, 'set');
// add project
const added = await addProject({
giturl: 'https://github.com/test/project',
Expand All @@ -127,7 +127,7 @@ describe('Test utils', () => {
});

it('updateProject', async () => {
const spy = sandbox.spy(window.chrome.storage.sync, 'set');
const spy = sandbox.spy(chrome.storage.sync, 'set');
const project = {
owner: 'test',
repo: 'project',
Expand All @@ -146,7 +146,7 @@ describe('Test utils', () => {
});

it('deleteProject', async () => {
const spy = sandbox.spy(window.chrome.storage.sync, 'set');
const spy = sandbox.spy(chrome.storage.sync, 'set');
// delete project without handle
let deleted = await deleteProject({ owner: 'adobe', repo: 'blog' });
expect(deleted).to.be.true;
Expand Down
16 changes: 8 additions & 8 deletions test/wtr/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const CONFIGS = [
},
];

window.chrome = chromeMock;
chrome = chromeMock;

describe('Test utils', () => {
const sandbox = sinon.createSandbox();
Expand Down Expand Up @@ -132,46 +132,46 @@ describe('Test utils', () => {
});

it('getConfig', async () => {
const spy = sandbox.spy(window.chrome.storage.local, 'get');
const spy = sandbox.spy(chrome.storage.local, 'get');
await getConfig('local', 'test');
expect(spy.calledWith('test')).to.be.true;
});

it('setConfig', async () => {
const spy = sandbox.spy(window.chrome.storage.local, 'set');
const spy = sandbox.spy(chrome.storage.local, 'set');
const obj = { foo: 'bar' };
await setConfig('local', obj);
expect(spy.calledWith(obj)).to.be.true;
});

it('removeConfig', async () => {
const spy = sandbox.spy(window.chrome.storage.local, 'remove');
const spy = sandbox.spy(chrome.storage.local, 'remove');
await removeConfig('local', 'foo');
expect(spy.calledWith('foo')).to.be.true;
});

it('clearConfig', async () => {
const spy = sandbox.spy(window.chrome.storage.local, 'clear');
const spy = sandbox.spy(chrome.storage.local, 'clear');
await clearConfig('local');
expect(spy.called).to.be.true;
});

it('getDisplay', async () => {
const spy = sandbox.spy(window.chrome.storage.local, 'get');
const spy = sandbox.spy(chrome.storage.local, 'get');
await getDisplay();
expect(spy.calledWith('hlxSidekickDisplay')).to.be.true;
});

it('setDisplay', async () => {
const spy = sandbox.spy(window.chrome.storage.local, 'set');
const spy = sandbox.spy(chrome.storage.local, 'set');
await setDisplay(true);
expect(spy.calledWith({
hlxSidekickDisplay: true,
})).to.be.true;
});

it('toggleDisplay', async () => {
const spy = sandbox.spy(window.chrome.storage.local, 'set');
const spy = sandbox.spy(chrome.storage.local, 'set');
const display = await toggleDisplay();
expect(spy.calledWith({
hlxSidekickDisplay: false,
Expand Down

0 comments on commit 3cc284d

Please sign in to comment.