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

Re-inject site scripts when background process wakes up from idle #40

Merged
merged 4 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 20 additions & 7 deletions src/background.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import type { WebNavigation } from 'webextension-polyfill';
import { runtime, scripting, tabs, webNavigation } from 'webextension-polyfill';
import { fetchUser } from './gkApi';
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 Down Expand Up @@ -68,18 +67,32 @@ async function addInjectionListener(context: CacheContext) {
const allDomains = Object.values<string[]>(injectionDomains as any).flat();

// note: This is a closure over injectionDomains
const injectScript = (details: WebNavigation.OnDOMContentLoadedDetailsType) => {
const injectScript = (tabId: number, tabUrl: string) => {
void scripting.executeScript({
target: { tabId: details.tabId },
target: { tabId: tabId },
// injectImmediately: true,
func: getInjectionFn(details.url, injectionDomains),
args: [details.url, GKDotDevUrl],
func: getInjectionFn(tabUrl, injectionDomains),
args: [tabUrl, GKDotDevUrl],
});
};

webNavigation.onDOMContentLoaded.addListener(injectScript, {
webNavigation.onDOMContentLoaded.addListener(details => injectScript(details.tabId, details.url), {
url: allDomains.map(domain => ({ hostContains: domain })),
});

// Immediately inject into the currently open compatible tabs. This is needed because when the background
// script is idle, its event listeners are not active. Opening a compatible tab will cause the background
// script to awaken and setup the event listeners again, but the tab will load before that happens.
const currentTabs = await tabs.query({
gitkrakel marked this conversation as resolved.
Show resolved Hide resolved
url: allDomains.map(domainToMatchPattern),
status: 'complete', // only query tabs that have finished loading
discarded: false, // discarded tabs will reload when focused so we don't need to inject into them now
});
currentTabs.forEach(tab => {
if (tab.id && tab.url) {
injectScript(tab.id, tab.url);
}
});
}

function urlHostHasDomain(url: URL, domains: string[]): boolean {
Expand Down
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
Loading