Skip to content

Commit

Permalink
Inject GK buttons into current tabs on startup and on install
Browse files Browse the repository at this point in the history
  • Loading branch information
jdgarcia committed May 30, 2024
1 parent f87146d commit d111cdc
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
58 changes: 44 additions & 14 deletions src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { injectionScope as inject_azureDevops } from './hosts/azureDevops';
import { injectionScope as inject_bitbucket } from './hosts/bitbucket';
import { injectionScope as inject_github } from './hosts/github';
import { injectionScope as inject_gitlab } from './hosts/gitlab';
import { refreshPermissions } from './permissions-helper';
import { domainToMatchPattern, refreshPermissions } from './permissions-helper';
import { getEnterpriseConnections, GKDotDevUrl, PermissionsGrantedMessage, PopupInitMessage } from './shared';
import type { CacheContext } from './types';

Expand All @@ -23,12 +23,7 @@ const DefaultInjectionDomains: InjectionDomains = {
};

webNavigation.onDOMContentLoaded.addListener(async details => {
const { injectionDomains } = (await storage.session.get('injectionDomains')) as {
injectionDomains?: InjectionDomains;
};
if (!injectionDomains) {
return;
}
const injectionDomains = await getInjectionDomains();

const injectionFn = getInjectionFn(details.url, injectionDomains);
if (injectionFn) {
Expand Down Expand Up @@ -64,6 +59,48 @@ runtime.onMessage.addListener(async msg => {
return undefined;
});

runtime.onInstalled.addListener(injectIntoCurrentTabs);
runtime.onStartup.addListener(injectIntoCurrentTabs);

async function injectIntoCurrentTabs() {
const injectionDomains = await getInjectionDomains();
const allDomains = Object.values<string[]>(injectionDomains as any).flat();

const currentTabs = await tabs.query({
url: allDomains.map(domainToMatchPattern),
status: 'complete',
discarded: false,
});
currentTabs.forEach(tab => {
if (tab.id && tab.url) {
const injectionFn = getInjectionFn(tab.url, injectionDomains);
if (injectionFn) {
void scripting.executeScript({
target: { tabId: tab.id },
func: injectionFn,
args: [tab.url, GKDotDevUrl],
});
}
}
});
}

async function getInjectionDomains() {
let { injectionDomains } = (await storage.session.get('injectionDomains')) as {
injectionDomains?: InjectionDomains;
};
if (!injectionDomains) {
const context: CacheContext = {};
// This removes unneded permissions
await refreshPermissions(context);

injectionDomains = await computeInjectionDomains(context);
await storage.session.set({ injectionDomains: injectionDomains });
}

return injectionDomains;
}

async function computeInjectionDomains(context: CacheContext) {
const injectionDomains = structuredClone(DefaultInjectionDomains);
const enterpriseConnections = await getEnterpriseConnections(context);
Expand Down Expand Up @@ -111,13 +148,6 @@ function getInjectionFn(
async function main() {
// The fetchUser function also updates the extension icon if the user is logged in
await fetchUser();

const context: CacheContext = {};
// This removes unneded permissions
await refreshPermissions(context);

const injectionDomains = await computeInjectionDomains(context);
void storage.session.set({ injectionDomains: injectionDomains });
}

void main();
2 changes: 1 addition & 1 deletion src/permissions-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { permissions } from 'webextension-polyfill';
import { arrayDifference, CloudProviders, getEnterpriseConnections } from './shared';
import type { CacheContext } from './types';

function domainToMatchPattern(domain: string): string {
export function domainToMatchPattern(domain: string): string {
return `*://*.${domain}/*`;
}

Expand Down

0 comments on commit d111cdc

Please sign in to comment.