Skip to content

Commit 477c3a7

Browse files
Update GitHub.com selector (#88)
1 parent 173fa03 commit 477c3a7

File tree

4 files changed

+31
-21
lines changed

4 files changed

+31
-21
lines changed

src/button/button-contributions.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ export interface ButtonContributionParams {
4848
},
4949

5050
/**
51-
* A regular expression that is used to match the current URL. This is making the selection faster and also can help to disambiguate.
51+
* Either a regular expression that is used to match the current URL or a function expected to return a boolean. This is making the selection faster and also can help to disambiguate.
5252
*/
53-
match?: RegExp,
53+
match?: RegExp | (() => boolean),
5454

5555
/**
5656
* The application that is supported by this button contribution.
@@ -167,24 +167,27 @@ export const buttonContributions: ButtonContributionParams[] = [
167167
// GitHub
168168
{
169169
id: "new-repo",
170-
match: /^https?:\/\/([^/]+)\/([^/]+)\/([^/]+)(\/(tree\/.*)?)?$/,
171170
exampleUrls: [
172-
// disabled testing, becuase the new layout doesn't show as an anonymous user
171+
// disabled testing, because the new layout doesn't show as an anonymous user
173172
// "https://github.com/svenefftinge/browser-extension-test",
174173
// "https://github.com/svenefftinge/browser-extension-test/tree/my-branch",
175174
],
176-
selector: "#repository-details-container > ul > li:nth-child(6)",
177-
containerElement: createElement("div", {
175+
selector: "#repository-details-container > ul",
176+
containerElement: createElement("li", {
178177
}),
179178
application: "github",
180179
manipulations: [
181180
{
182181
// make the code button secondary
183-
element: "#repository-details-container > ul > li:nth-child(5) > get-repo > details > summary",
182+
element: "#repository-details-container > ul > li > get-repo > details > summary",
184183
remove: "Button--primary",
185184
add: "Button--secondary"
186185
}
187186
],
187+
match: () => {
188+
const regex = /^https?:\/\/([^/]+)\/([^/]+)\/([^/]+)(\/(tree\/.*)?)?$/;
189+
return document.querySelector("div.file-navigation") === null && regex.test(window.location.href);
190+
}
188191
},
189192
{
190193
id: "commit",

src/contents/button.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class ButtonContributionManager {
5151
const isActive = this.isActive(contribution);
5252
if (isActive) {
5353
if (this.active?.contribution.id === contribution.id &&
54-
this.active?.anchor?.isConnected) {
54+
this.active?.anchor?.isConnected) {
5555
// do nothing
5656
return null;
5757
} else {
@@ -79,9 +79,12 @@ class ButtonContributionManager {
7979
}
8080

8181
private isActive(contrib: ButtonContributionParams) {
82-
if (contrib.match && !contrib.match.test(window.location.href)) {
82+
if (typeof contrib.match === "function" && !contrib.match()) {
83+
return false;
84+
} else if (typeof contrib.match === "object" && !contrib.match.test(window.location.href)) {
8385
return false;
8486
}
87+
8588
const parent = this.lookupElement(contrib.selector);
8689
if (parent === null) {
8790
return false;

test/src/button-contributions-copy.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ export interface ButtonContributionParams {
4848
},
4949

5050
/**
51-
* A regular expression that is used to match the current URL. This is making the selection faster and also can help to disambiguate.
51+
* Either a regular expression that is used to match the current URL or a function expected to return a boolean. This is making the selection faster and also can help to disambiguate.
5252
*/
53-
match?: RegExp,
53+
match?: RegExp | (() => boolean),
5454

5555
/**
5656
* The application that is supported by this button contribution.
@@ -167,24 +167,27 @@ export const buttonContributions: ButtonContributionParams[] = [
167167
// GitHub
168168
{
169169
id: "new-repo",
170-
match: /^https?:\/\/([^/]+)\/([^/]+)\/([^/]+)(\/(tree\/.*)?)?$/,
171170
exampleUrls: [
172-
// disabled testing, becuase the new layout doesn't show as an anonymous user
171+
// disabled testing, because the new layout doesn't show as an anonymous user
173172
// "https://github.com/svenefftinge/browser-extension-test",
174173
// "https://github.com/svenefftinge/browser-extension-test/tree/my-branch",
175174
],
176-
selector: "#repository-details-container > ul > li:nth-child(6)",
177-
containerElement: createElement("div", {
175+
selector: "#repository-details-container > ul",
176+
containerElement: createElement("li", {
178177
}),
179178
application: "github",
180179
manipulations: [
181180
{
182181
// make the code button secondary
183-
element: "#repository-details-container > ul > li:nth-child(5) > get-repo > details > summary",
182+
element: "#repository-details-container > ul > li > get-repo > details > summary",
184183
remove: "Button--primary",
185184
add: "Button--secondary"
186185
}
187186
],
187+
match: () => {
188+
const regex = /^https?:\/\/([^/]+)\/([^/]+)\/([^/]+)(\/(tree\/.*)?)?$/;
189+
return document.querySelector("div.file-navigation") === null && regex.test(window.location.href);
190+
}
188191
},
189192
{
190193
id: "commit",

test/src/button-contributions.spec.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ 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() {
6+
describe("Query Selector Tests", function () {
77
let browser: Browser;
88
let page: Page;
99

10-
before(async function() {
10+
before(async function () {
1111
browser = await puppeteer.launch({
1212
headless: "new",
1313
});
1414
page = await browser.newPage();
1515
});
1616

17-
after(async function() {
17+
after(async function () {
1818
await browser.close();
1919
});
2020

@@ -30,14 +30,15 @@ describe("Query Selector Tests", function() {
3030
await page.goto(url);
3131
let foundMatch = false;
3232
for (const contr of buttonContributions) {
33-
if (contr.match && !contr.match.test(url)) {
33+
if (typeof contr.match === "object" && !contr.match.test(url)) {
3434
continue;
3535
}
3636
const element = await resolveSelector(page, contr.selector);
3737
if (contr.id === id) {
3838
expect(element, `Expected '${id}' to match on ${url}`).to.not.be.null;
3939
foundMatch = true;
4040
} else {
41+
if (contr.exampleUrls.length === 0) return true;
4142
expect(element, `Did not expect '${contr.id}' to match on ${url}`).to.be.null;
4243
}
4344
}
@@ -46,7 +47,7 @@ describe("Query Selector Tests", function() {
4647

4748
for (const contribs of buttonContributions) {
4849
for (const url of contribs.exampleUrls) {
49-
it("url ("+url+") should only match '" + contribs.id +"'" , async function() {
50+
it(`url (${url}) should only match '${contribs.id}'`, async function () {
5051
await testContribution(url, contribs.id);
5152
}).timeout(5000);
5253
}

0 commit comments

Comments
 (0)