Skip to content

Commit d59273e

Browse files
Performance improvements (#89)
1 parent 477c3a7 commit d59273e

File tree

4 files changed

+119
-2
lines changed

4 files changed

+119
-2
lines changed

src/button/button-contributions.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,31 @@
66

77
export type SupportedApplication = "github" | "gitlab" | "bitbucket-server" | "bitbucket";
88

9+
const resolveMetaAppName = (head: HTMLHeadElement): string | undefined => {
10+
const metaApplication = head.querySelector("meta[name=application-name]");
11+
const ogApplication = head.querySelector("meta[property='og:site_name']");
12+
13+
if (metaApplication) {
14+
return metaApplication.getAttribute("content") || undefined;
15+
} else if (ogApplication) {
16+
return ogApplication.getAttribute("content") || undefined;
17+
}
18+
19+
return undefined;
20+
}
21+
22+
/**
23+
* Provides a fast check to see if the current URL is on a supported site.
24+
*/
25+
export const isSiteSuitable = (): boolean => {
26+
const appName = resolveMetaAppName(document.head);
27+
if (!appName) {
28+
return false;
29+
}
30+
const allowedApps = ["GitHub", "GitLab", "Bitbucket"];
31+
return allowedApps.includes(appName);
32+
}
33+
934
export interface ButtonContributionParams {
1035
/**
1136
* A unique id for the button contribution. Used to identify the button in the UI.

src/contents/button.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { PlasmoCSConfig, PlasmoGetInlineAnchor } from "plasmo";
22
import cssText from "data-text:../button/button.css"
3-
import { buttonContributions, type ButtonContributionParams } from "../button/button-contributions";
3+
import { buttonContributions, type ButtonContributionParams, isSiteSuitable } from "../button/button-contributions";
44
import { GitpodButton } from "../button/button";
55
import { type ReactElement } from "react";
66
import React from "react";
@@ -23,7 +23,16 @@ class ButtonContributionManager {
2323
anchor: HTMLElement,
2424
}
2525

26+
_disabled = false;
27+
2628
constructor(private contributions: ButtonContributionParams[]) {
29+
if (!this._disabled) {
30+
const isSuitable = isSiteSuitable();
31+
if (!isSuitable) {
32+
this._disabled = true;
33+
}
34+
}
35+
2736
for (const contribution of this.contributions) {
2837
const containerId = this.getContainerId(contribution);
2938
if (!this.buttons.has(containerId)) {
@@ -47,6 +56,10 @@ class ButtonContributionManager {
4756
}
4857

4958
public getInlineAnchor(): HTMLElement | null {
59+
if (this._disabled) {
60+
return null;
61+
}
62+
5063
for (const contribution of this.contributions) {
5164
const isActive = this.isActive(contribution);
5265
if (isActive) {
@@ -84,7 +97,6 @@ class ButtonContributionManager {
8497
} else if (typeof contrib.match === "object" && !contrib.match.test(window.location.href)) {
8598
return false;
8699
}
87-
88100
const parent = this.lookupElement(contrib.selector);
89101
if (parent === null) {
90102
return false;

test/src/button-contributions-copy.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,31 @@
66

77
export type SupportedApplication = "github" | "gitlab" | "bitbucket-server" | "bitbucket";
88

9+
const resolveMetaAppName = (head: HTMLHeadElement): string | undefined => {
10+
const metaApplication = head.querySelector("meta[name=application-name]");
11+
const ogApplication = head.querySelector("meta[property='og:site_name']");
12+
13+
if (metaApplication) {
14+
return metaApplication.getAttribute("content") || undefined;
15+
} else if (ogApplication) {
16+
return ogApplication.getAttribute("content") || undefined;
17+
}
18+
19+
return undefined;
20+
}
21+
22+
/**
23+
* Provides a fast check to see if the current URL is on a supported site.
24+
*/
25+
export const isSiteSuitable = (): boolean => {
26+
const appName = resolveMetaAppName(document.head);
27+
if (!appName) {
28+
return false;
29+
}
30+
const allowedApps = ["GitHub", "GitLab", "Bitbucket"];
31+
return allowedApps.includes(appName);
32+
}
33+
934
export interface ButtonContributionParams {
1035
/**
1136
* A unique id for the button contribution. Used to identify the button in the UI.

test/src/button-contributions.spec.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,61 @@ import { expect } from "chai";
33
import { describe, it, before, after } from 'mocha';
44
import { buttonContributions } from "./button-contributions-copy.js";
55

6+
describe("Query Selector Tests", function () {
7+
let browser: Browser;
8+
let page: Page;
9+
10+
before(async function () {
11+
browser = await puppeteer.launch({
12+
headless: "new",
13+
});
14+
page = await browser.newPage();
15+
});
16+
17+
after(async function () {
18+
await browser.close();
19+
});
20+
21+
async function testHost() {
22+
const all = buttonContributions.flatMap((x) => x.exampleUrls);
23+
for (const url of all) {
24+
it(`should detect the platform for ${url}`, async function () {
25+
await page.goto(url);
26+
27+
const foundMatch = await page.evaluate(() => {
28+
const resolveMetaAppName = (head: HTMLHeadElement): string | undefined => {
29+
const metaApplication = head.querySelector("meta[name=application-name]");
30+
const ogApplication = head.querySelector("meta[property='og:site_name']");
31+
32+
if (metaApplication) {
33+
return metaApplication.getAttribute("content") || undefined;
34+
} else if (ogApplication) {
35+
return ogApplication.getAttribute("content") || undefined;
36+
}
37+
38+
return undefined;
39+
}
40+
41+
const isSiteSuitable = (): boolean => {
42+
const appName = resolveMetaAppName(document.head);
43+
if (!appName) {
44+
return false;
45+
}
46+
const allowedApps = ["GitHub", "GitLab", "Bitbucket"];
47+
return allowedApps.includes(appName);
48+
}
49+
50+
return isSiteSuitable();
51+
});
52+
expect(foundMatch, `Expected to find a match for '${url}'`).to.be.true;
53+
}).timeout(30_000);
54+
55+
}
56+
}
57+
58+
testHost();
59+
});
60+
661
describe("Query Selector Tests", function () {
762
let browser: Browser;
863
let page: Page;

0 commit comments

Comments
 (0)